ANTLR 中的 Mediawiki 解析:处理 ' 标记 [英] Mediawiki parsing in ANTLR: processing ' tokens

查看:27
本文介绍了ANTLR 中的 Mediawiki 解析:处理 ' 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写语法来解析 Media wiki 的 wiki 语法,在此之后克里奥尔语语法也是(不幸的是现有的克里奥尔语语法在 Antlr 3 中不起作用.

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''

我从这个问题中得到了很多帮助 但我被卡住了.目标是使用操作或可能的 AST 在语法中生成 HTML - 我不确定哪个是最好的.

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.

推荐答案

作为练习,我也创建了一个 MediaWiki 解析器,并没有匹配粗体和斜体的打开和关闭标签,而是调用了一个像这个:

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
  :  '\''
  ; 

然后在翻译 MediaWiki 格式(到 HTML?)的过程中,当您遇到 BOLD_ITALICBOLD 之一时,您会不断翻转一些布尔标志斜体.

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.

祝你好运!

这篇关于ANTLR 中的 Mediawiki 解析:处理 ' 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆