ANTLR 生成空条件 [英] ANTLR generating empty conditions

查看:21
本文介绍了ANTLR 生成空条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习使用 ANTLR,但在这种情况下我无法弄清楚我的代码有什么问题.我希望这对于有一定经验的人来说真的很容易.这是语法(真的很短).

I'm trying to learn to use ANTLR, but I cannot figure out what's wrong with my code in this case. I hope this will be really easy for anyone with some experience with it. This is the grammar (really short).

grammar SmallTest;

@header {
package parseTest;
import java.util.ArrayList;
}

prog returns [ArrayList<ArrayList<String>> all]
    :(stat { if ($all == null)
               $all = new ArrayList<ArrayList<String>>();
             $all.add($stat.res);
           } )+
    ;

stat returns [ArrayList<String> res]
    :(element  { if ($res == null)
                   $res = new ArrayList<String>();
                 $res.add($element.text);
               } )+ NEWLINE
    |   NEWLINE
    ;

element: ('a'..'z'|'A'..'Z')+ ;
NEWLINE:'\r'? '\n' ;

问题是当我生成 Java 代码时有一些空的 if 条件,并且编译器因此显示错误,我可以手动编辑它,但这可能会更糟.我想这有什么问题.

The problem is that when I generate the Java code there are some empty if conditions, and the compiler displays an error because of that, I could edit that manually, but that would probably be much worse. I guess something is wrong in this.

对不起,这一定是非常愚蠢的,但我的示例与站点中的示例非常相似,我无法再想出一种方法来区分差异.

Sorry for asking, this has to be really stupid, but my example is so similar to those in the site that I cannot imagine a way to atomize the differences any more.

非常感谢.

推荐答案

你应该把你的列表的初始化放在规则的 @init { ... } 块中,它在之前被执行规则中的任何内容都匹配.

You should put the initialization of your lists inside the @init { ... } block of the rules, which get executed before anything in the rule is matched.

此外,您的 element 规则不应该是解析器规则,而应该是词法分析器规则(它应该以大写开头!).

Also, your element rule should not be a parser rule, but a lexer rule instead (it should start with a capital!).

解析器的入口点,prog 规则,应该以 EOF 标记结束,否则解析器可能会在所有标记被正确处理之前停止.

And the entry point of your parser, the prog rule, should end with the EOF token otherwise the parser might stop before all tokens are handled properly.

最后,@header { ... } 部分只适用于解析器(它是 @parser::header { ... }),您还需要将包声明添加到词法分析器中.

Finally, the @header { ... } section only applies to the parser (it is a short-hand for @parser::header { ... }), you need to add the package declaration to the lexer as well.

工作演示:

grammar SmallTest;

@header {
package parseTest;
import java.util.ArrayList;
}

@lexer::header {
package parseTest;
}

prog returns [ArrayList<ArrayList<String>> all]
@init {$all = new ArrayList<ArrayList<String>>();}
  :  (stat {$all.add($stat.res);})+ EOF
  ;

stat returns [ArrayList<String> res]
@init {$res = new ArrayList<String>();}
  :  (ELEMENT {$res.add($ELEMENT.text);})* NEWLINE
  ;

ELEMENT : ('a'..'z'|'A'..'Z')+ ;
NEWLINE : '\r'? '\n' ;
SPACE   : ' ' {skip();};

Main.java

package parseTest;

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    SmallTestLexer lexer = new SmallTestLexer(new ANTLRStringStream("a bb ccc\ndddd eeeee\n"));
    SmallTestParser parser = new SmallTestParser(new CommonTokenStream(lexer));
    System.out.println(parser.prog());
  }
}

要运行这一切,请执行以下操作:

And to run it all, do:

java -cp antlr-3.3.jar org.antlr.Tool parseTest/SmallTest.g 
javac -cp .:antlr-3.3.jar parseTest/*.java
java -cp .:antlr-3.3.jar parseTest.Main

产生:

[[a, bb, ccc], [dddd, eeeee]]

这篇关于ANTLR 生成空条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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