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

查看:148
本文介绍了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代码时,如果条件满足,则会有一些空的内容,并且由于这个原因,编译器会显示错误,我可以手动对其进行编辑,但这可能会更糟.我想这是有问题的.

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