ANTLR 3中wikitext-to-HTML的工作示例

Dan*_*rzo 2 mediawiki antlr stringtemplate wikitext creole

我正试图在ANTLR 3中充实wiki文本到HTML的翻译,但我一直陷入困境.

你知道一个我可以检查的工作实例吗?我尝试了MediaWiki ANTLR语法和Wiki Creole语法,但是我无法让它们在ANTLR 3中生成词法分析器和解析器.

以下是我尝试使用的两个语法的链接:

我无法获得这两个中的任何一个来生成我的Java Lexer和Parser.(我使用ANTLR3作为Eclipse插件).MediaWiki需要花费很长时间来构建,然后在某些时候抛出OutOfMemory异常.另一个有错误,我不知道如何调试.

编辑:好的,我有一个非常基本的语法:

grammar wikitext;

options {
  //output = AST;
  //ASTLabelType = CommonTree;
  output = template;
  language = Java;
}

document: line (NL line?)*;

line: horizontal_line | list | heading | paragraph;

/* horizontal line */
horizontal_line: HRLINE;

/* lists */
list: unordered_list | ordered_list;

unordered_list: '*'+ content;
ordered_list: '#'+ content;

/* Headings */
heading: heading1 | heading2 | heading3 | heading4 | heading5 | heading6;
heading1: H1 plain H1;
heading2: H2 plain H2;
heading3: H3 plain H3;
heading4: H4 plain H4;
heading5: H5 plain H5;
heading6: H6 plain H6;

/* Paragraph */
paragraph: content;

content: (formatted | link)+;

/* links */
link: external_link | internal_link;

external_link: '[' external_link_uri ('|' external_link_title)? ']';
internal_link: '[[' internal_link_ref ('|' internal_link_title)? ']]' ;

external_link_uri: CHARACTER+;
external_link_title: plain;
internal_link_ref: plain;
internal_link_title: plain;

/* bold & italic */
formatted: bold_italic | bold | italic | plain;

bold_italic: BOLD_ITALIC plain BOLD_ITALIC;
bold: BOLD plain BOLD;
italic: ITALIC plain ITALIC;

/* Plain text */
plain: (CHARACTER | SPACE)+;


/**
 * LEXER RULES
 * --------------------------------------------------------------------------
 */

HRLINE: '---' '-'+;

H1: '=';
H2: '==';
H3: '===';
H4: '====';
H5: '=====';
H6: '======';

BOLD_ITALIC: '\'\'\'\'\'';
BOLD: '\'\'\'';
ITALIC: '\'\'';

NL: '\r'?'\n';

CHARACTER       :       '!' | '"' | '#' | '$' | '%' | '&'
                |       '*' | '+' | ',' | '-' | '.' | '/'
                |       ':' | ';' | '?' | '@' | '\\' | '^' | '_' | '`' | '~'
                |       '0'..'9' | 'A'..'Z' |'a'..'z' 
                |       '\u0080'..'\u7fff'
                |       '(' | ')'
                |       '\'' | '<' | '>' | '=' | '[' | ']' | '|' 
                ;

SPACE: ' ' | '\t';
Run Code Online (Sandbox Code Playgroud)

虽然如何输出HTML,但我不清楚.我一直在研究StringTemplate,但我不明白如何构建我的模板.具体来说,哪个模板在语法中的位置.你能用一个简短的例子来帮助我吗?

Bar*_*ers 5

好的,在你的编辑后,我有几个建议.

就像我在评论中所说的那样,为这种语言编写语法几乎是不可能的.至少,一次尝试这样做,就是这样.我看到这个工作的唯一方法是使用多个解析器执行此操作,其中第一个"解析阶段"将非常"粗略地"解析wiki-source.例如:a table将被标记为:TABLE : '{|' .* '|}'然后您将创建另一个正确解析此表的解析器.在一个解析器中执行此操作将导致您的解析器规则IMO中存在相当多的歧义.

关于发布HTML代码,执行此操作的"正确"方法确实是使用StringTemplate,但考虑到您对ANTLR本身不熟悉,我会保持简单.您可以在解析器类中创建一个StringBuilder属性,该属性将在您解析源文件时收集所有HTML代码.您可以使用{和包装代码来嵌入ANTLR规则中的代码}.

这是一个快速演示:

grammar T;

@parser::members {

  // an attribute that is only available in your 
  // parser (so only in parser rules!)
  protected StringBuilder htmlBuilder = new StringBuilder();
}

// Parser rules
parse
  :  atom+ EOF
  ;

atom
  :  header
  |  Any    {htmlBuilder.append($Any.text);} // append the text from 'Any' token
  ;

header
  :  H3 h3Content H3 {htmlBuilder.append("<h3>" + $h3Content.text + "</h3>");}
  |  H2 h2Content H2 {htmlBuilder.append("<h2>" + $h2Content.text + "</h2>");}
  |  H1 h1Content H1 {htmlBuilder.append("<h1>" + $h1Content.text + "</h1>");}
  ;

h3Content : ~H3*; // match any token except H3, zero or more times
h2Content : ~H2*; //        "               H2          "
h1Content : ~H1*; //        "               H1          "

// Lexer rules    
H3 : '===';
H2 : '==';
H1 : '=';

// Fall through rule: if non of the above 
// lexer rules matched, this one will.
Any
  :  .
  ;
Run Code Online (Sandbox Code Playgroud)

从该语法中,您生成一个解析器和词法分析器:

java -cp antlr-3.2.jar org.antlr.Tool T.g
Run Code Online (Sandbox Code Playgroud)

然后创建一个小类来测试你的解析器:

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {

        // the source to be parsed
        String source = 
                "= header 1 =             \n"+
                "                         \n"+
                "some text here           \n"+
                "                         \n"+
                "=== header level 3 ===   \n"+
                "                         \n"+
                "and some more text         ";

        ANTLRStringStream in = new ANTLRStringStream(source);
        TLexer lexer = new TLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TParser parser = new TParser(tokens);

        // invoke the start-rule in your parser
        parser.parse();

        // print the contents of your parser's StringBuilder
        System.out.println(parser.htmlBuilder);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后编译所有源文件:

javac -cp antlr-3.2.jar *.java
Run Code Online (Sandbox Code Playgroud)

最后,运行你的主类

// *nix & MacOS
java -cp .:antlr-3.2.jar Main

// Windows
java -cp .;antlr-3.2.jar Main
Run Code Online (Sandbox Code Playgroud)

这将打印以下内容到控制台:

<h1> header 1 </h1>             

some text here           

<h3> header level 3 </h3>   

and some more text  
Run Code Online (Sandbox Code Playgroud)

但是,再一次,如果你可以自由选择一种不同的语言进行解析,我会这样做而忘记解析这个可怕的Wiki.

无论如何,无论你做什么:祝你好运!