[ANTLR:多个输入

问题描述 投票:1回答:1

我正在尝试将ANTLR用于属性语法。这是我的驱动程序代码:

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;

@SuppressWarnings({ "deprecation", "unused" })
public class mymain {
    public static void main(String[] args) {
        try {
            ANTLRInputStream input = new ANTLRFileStream("/home/vkmanojk/eclipse-workspace/17040/src/input");
            statementLexer lexer = new statementLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            statementParser parser = new statementParser(tokens);
            System.out.println(parser.expression());
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

此程序仅对input文件中的单个输入起作用。如何更改main函数,使其对input文件中的多个输入起作用?

java file input antlr handler
1个回答
0
投票

如果您的输入文件包含多个expression,只需添加一个匹配1个或多个expression的解析器规则:

expressions
 : expression ( NEW_LINE expression )*
 ;

// Your existing expression rule
expression
 : ...
 ;

NEW_LINE
 : '\r'? '\n'
 ;
© www.soinside.com 2019 - 2024. All rights reserved.