[antlr-interest] Parsing (a+b)+(c+d)

Tom Edwards antlr at t0m.me.uk
Sun Aug 17 03:47:22 PDT 2008


Hi,

Sorry if this is a bit of newb question however I am pretty new to
language translation and I can't seem to find the answer to this
question. I am trying to parse the following simple expression:

(a+b)+(c+d)

With the following grammar:

grammar Vector;
options {
       output=AST;
       ASTLabelType=CommonTree;
}

prog    :       (stmt
{System.out.println($stmt.tree.toStringTree());} )+;
stmt    :       expr NEWLINE?;

expr    :       IDENTIFIER exprD
       |       '(' expr ')';
exprD   :       OP (exprD|IDENTIFIER);


OP      :       '+';

IDENTIFIER
       :       ('a'..'z'|'A'..'Z');
NEWLINE :        '\r'?'\n';

Which is based of an example I have found on the website.
The problem is that as Antlr is LL (am I right here?) you cannot have
left recursion, which is fine, however I am not sure how you can
construct something to parse the original problem. Splitting the
problem into expr and exprD is a technique I have borrowed from the
"dragon" book, however in this case it does work very well, but not
with the parenthesis.

Again, sorry if this is a silly question however it is bugging me and
I don't have any other form of support.

Kind regards and thanks for any help you can offer.

Tom




More information about the antlr-interest mailing list