2

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?

Michael
  • 2,683
  • 28
  • 30
  • 1
    could it be that you need to reset the lexer token stream? At the moment you have moved through all the tokens and the first thing the parsers is – luketorjussen Sep 07 '11 at 14:50
  • *hangs head in shame* yes, yes I needed to call lex.reset()...sigh. I am such a newb I didn't know about that method, although knowing it was a stream that I had iterated through I should have thought about needing to reset it, pretty common concept. – Michael Sep 07 '11 at 15:00
  • The good news is that this fixes the problem I was having with this. However, this isn't going to fix the problem I am having with my grammar. I thought I had found the common problem but totally missed the mark....arrgghh. – Michael Sep 07 '11 at 15:03
  • @Michael :-) at least it's fixed now. If you post your other problem in a new question we can take a look! – luketorjussen Sep 07 '11 at 15:13
  • I have posted another question that has my real problem in it. Not sure if it is bad etiquette to link to it here, so won't. I used the same tags though. – Michael Sep 07 '11 at 16:31

1 Answers1

5

After printing out your tokens, you will be at the end of the token stream. You will need to reset the token stream by calling

lex.reset();

This will make the lexer go back to the start of the token stream so you can call your parser.

luketorjussen
  • 3,156
  • 1
  • 21
  • 38