ANTLR中的MediaWiki解析:处理令牌 [英] Mediawiki parsing in ANTLR: processing ' tokens

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

问题描述

我正在尝试编写一种语法来解析媒体Wiki的Wiki语法,然后克里奥尔语语法(不幸的是,

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解析器,并且不将open-和close-tag匹配为粗体和斜体,而是调用了一个类似这个:

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_ITALICBOLDITALIC之一时,您会翻转一些布尔值标志.

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天全站免登陆