[antlr-interest] grammar works in antlrworks debug mode but not eclipse

John B. Brodie jbb at acm.org
Fri Nov 11 06:21:59 PST 2011


Greetings!

On 11/11/2011 03:00 AM, D. Frej wrote:
> The grammar is rather stupid and simple
... snipped ...

> It is executed with the following Java code
> 
> ANTLRStringStream input = new ANTLRStringStream("15 / 7.25 +4");
> ExpressionLexer lex = new ExpressionLexer(input);
> CommonTokenStream tokens = new CommonTokenStream(lex);
> 
> ExpressionParser g = new ExpressionParser(tokens); //, 49100, null);
> try {
>      ExpressionParser.content_return r = g.content();
> 
>      CommonTree ctree = (CommonTree) r.getTree();
>      System.out.println("ctree.type|" + ctree.getType() + "|" + 
> ExpressionParser.tokenNames[ctree.getType()] + "|");
> } catch (RecognitionException e) {
>      e.printStackTrace();
> }
> 
> Yet here the input of "15 / 7.25 +4" does not work. Eclipse outputs 
> "ctree.type|0|<invalid>|". 

it actually does parse correctly. you are not printing the result
properly. the result of the parse is a tree and yet you try to print it
as if it were a token.

use ctree.toStringTree() in your println instead.

> When I use simply "15" as input, I get the - 
> correct - output "ctree.type|7|INT|".
> 

because the tree in this case is simply a single token.




i do not use ANTLRWorks so can not speak to your experiences with that tool.

hope this helps...
   -jbb



More information about the antlr-interest mailing list