编译Java代码时找不到符号

时间:2020-03-03 17:19:47

标签: java antlr antlr3

使用cmd进行编译:javac Test.java。但是,编译失败,因为它找不到符号parser.prog()。有什么想法吗?

import org.antlr.runtime.*;

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

            // Create an TLexer that feeds from that stream
            //TLexer lexer = new TLexer(new ANTLRInputStream(System.in));
            TLexer lexer = new TLexer(new ANTLRFileStream("input.txt"));

            // Create a stream of tokens fed by the lexer
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            // Create a parser that feeds off the token stream
            TParser parser = new TParser(tokens);

            // Begin parsing at rule prog
            parser.prog();
        }
    }

1 个答案:

答案 0 :(得分:0)

T.g4语法(或T.g)中,您还必须具有名为prog的解析器规则:

grammar T;

prog
 : ...
 ;

...

看看您生成的解析器,我发现您有一个解析器规则,如下所示:

filter
 : expression EOF
 ;

改用它:

// Begin parsing at rule prog
parser.filter();