开发者

All ANTLR grammars produce error "no viable alternative at input '<EOF>'"

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜