I am needing to parse a small expression language (and, or, not, parens change precedence) so picked ANTLR for the task, I made good progress (ANTLRWorks is very nice for a newbie). I used some Getting Starting references from the antlr website and then found two blog posts that are perfect fit for what I am trying to accomplish:
http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx http://www.alittlemadness.com/2006/06/05/antlr-by-example-part-1-the-language
The problem I am having is no matter what input I put I always get the error:
line 1:29 no viable alternative at input 'EOF'
So as part of my troubleshooting I decided to try a grammar I knew was good and generated a lexer/parser from the ECalc.g grammar found at the first link. To my surprise I got the same error when using that grammar! I am bamboozled. The only changes I made to the grammar were to make it generate Java code and took out some CSharp code in the @members section.
Here is my tester class:
public class ECalcTester {
private final static Logger logger = Logger.getLogger(ECalcTester.class);
public static void main(String[] args) {
BasicConfigurator.configure();
ECalcLexer lex = new ECalcLexer(new ANTLRStringStream("false || not (false and true)"));
Token token;
while (true) {
token = lex.nextToken();
if (token.getType() == Token.EOF) {
break;
}
System.out.println("Token: ‘" + token.getText() + "’");
}
CommonTokenStream tokens = new CommonTokenStream(lex);
lex.nextToken();
ECalcParser parser = new ECalcParser(tokens);
try {
logger.debug(parser.expression().getTree());
} catch (org.antlr.runtime.RecognitionException e) {
logger.error("Exception ", e);
}
}
Here is the output:
Token: ‘false’
Token: ‘ ’
Token: ‘||’
Token: ‘ ’
Token: ‘not’
Token: ‘ ’
Token: ‘(’
Token: ‘false’
Token: ‘ ’
Token: ‘and’
Token: ‘ ’
Token: ‘true’
Token: ‘)’
line 1:29 no viable alternative at input '<EOF>'
0 [main] DEBUG ECalcTester - <unexpected: [@0,29:29='<EOF>',<-1>,1:29], resync=>
If I can figure out why this occurs in a grammar that should be good I should be able to figure out why the same thing happens in my grammar (very similar concept).
Can anyone offer any insight?