找不到符号,在编译Java代码时。

huangapple go评论73阅读模式
英文:

Cannot Find Symbol when compiling a java code

问题

Compiling this using cmd : javac Test.java. However compilation fails, saying it cant find symbol parser.prog(). Any ideas?

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();
    }
}
英文:

Compiling this using cmd : javac Test.java. However compilation fails, saying it cant find symbol parser.prog(). Any ideas?

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

在您的 T.g4 语法文件(或 T.g)中,您还必须有一个名为 prog 的解析规则:

grammar T;

prog
 : ...
 ;

...

从您生成的解析器中可以看出,您有一个类似于以下的解析规则:

filter
 : expression EOF
 ;

请改用以下方式:

// 从规则 prog 开始解析
parser.filter();
英文:

In your T.g4 grammar (or T.g), you must also have a parser rule named prog:

grammar T;

prog
 : ...
 ;

...

Looking at your generated parser, I see you have a parser rule like this:

filter
 : expression EOF
 ;

Use that instead:

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

huangapple
  • 本文由 发表于 2020年3月4日 01:19:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/60512470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定