Mediawiki parsing in ANTLR: processing ' tokens

狂风中的少年 提交于 2019-12-11 23:13:51

问题


I'm trying to write a grammar to parse Media wiki's wiki syntax, and after this the Creole syntax too (unfortunately an existing Creole grammar doesn't work in Antlr 3).

My issue right now is being able to capture a bold rule when I'm already inside an italic rule, or visa versa. For example

'' this text is bold '''now it's italic''' and just bold again''

I've got a lot of help from this question but I'm stuck. The goal is to produce HTML inside the grammar using actions, or possibly an AST - I'm not sure which is best yet.


回答1:


As an exercise, I created a MediaWiki parser as well and didn't match open- and close-tags for bold and italic, but rather invoked a toggle like this:

grammar MediaWiki;

options {
  output=AST;
  backtrack=true;
  memoize=true;
}

...

// entry point of the parser
parse
  :  atom+ EOF -> ^(ROOT atom+)
  ;

atom
  :  formatToggle
  |  horizontalRule
  |  header
  |  link
  |  list
  |  preFormattedText
  |  table
  |  ...
  |  any
  ;

formatToggle
  :  SQt SQt SQt SQt SQt -> BOLD_ITALIC
  |  SQt SQt SQt         -> BOLD
  |  SQt SQt             -> ITALIC
  ;

...

SQt
  :  '\''
  ; 

And then during the translation of the MediaWiki format (to HTML?), you keep flip some boolean flags when you encounter one of BOLD_ITALIC, BOLD or ITALIC.

I haven't tested my grammar properly yet, so I'm not going to post the entire grammar here.

Good luck!



来源:https://stackoverflow.com/questions/5442025/mediawiki-parsing-in-antlr-processing-tokens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!