以下语法成功地生成了一个用于处理字符串的解析器'ccdunion'
。但是当我尝试使用解释器时,我有NoViableAltException
错误,为什么?
grammar SimpleCalc;
options {
k = 2;
output = AST;
backtrack = true;
}
tokens {
UNION = 'union';
}
@header {
package myPack;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
}
@lexer::header {
package myPack;
}
@members {
public static void main(String[] args) throws Exception {
File fileOut = new File("src/ARFF.arff");
FileWriter fw = new FileWriter(fileOut);
PrintWriter pw = new PrintWriter(fw);
pw.print("a ");
pw.println("b");
pw.println(12);
fw.close();
SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
SimpleCalcParser parser = new SimpleCalcParser(tokens);
try {
parser.expr();
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
expr
: filsPiquets EOF
{
System.out.println("base mono: ");
}
;
filsPiquets
: chars UNION
{
System.out.println("TXTunionII"+$chars.text);
}
;
chars
: CHAR
| . chars
;
CHAR
: .
;