Hi I want to do a java or c# method which will return a boolean expression true or false based on the given input. For example:
if (input matches the antlr grammar) return true; else return false; So the problem is i dont know how to check if the commontree have any mismatchetoken. I tried to traverse the tree but it doesnt give any mismatchetoken as node of the tree. Probably the problem is that the AST doesnt show the mismatched tokens only the parse tree. It could be help also if someone tell me how get the parsetree from parser?
I have done the ANTLR .g file and it works good, now I need to do the following: i have to check if the input was correct or not, I have done this but it doesnt work:
public static boolean check() {
String file = "test.txt";
ANTLRReaderStream input;
try{
input = new ANTLRReaderStream(new FileReader(file));
regExLexer lexer = new regExLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
regExParser parser = new regExParser(tokens);
CommonTree root = (CommonTree)parser.goal().getTree();
return true;}
catch{
return false;}
}
So i expect from my method to return true only when the input string is correct, and false otherwise. The way I have done it always returns true, but when the string is not correct it prints
"line 1:4 extraneous input '+' expecting EOF"
in console.