我正在尝试将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文件中的多个输入起作用?
如果您的输入文件包含多个expression
,只需添加一个匹配1个或多个expression
的解析器规则:
expressions
: expression ( NEW_LINE expression )*
;
// Your existing expression rule
expression
: ...
;
NEW_LINE
: '\r'? '\n'
;