[antlr-interest] Help with simple parser?

Philippe Lavoie philippe.lavoie at cactus.ca
Tue Jan 28 09:23:30 PST 2003


I tried to solve your problem as a way to learn antlr myself. Here is
what I came up with. Perhaps someone with more experience can show me
why my solution is not good (tm).

expr
    : sumExpr 
    | LPAREN! sumExpr RPAREN!
    | ifExp;
sumExpr  : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
prodExpr : minMaxExpr ((MUL^|DIV^|MOD^) minMaxExpr)* ;
minMaxExpr:	((MIN^|MAX^) LPAREN! minMaxExpr COMMA! minMaxExpr
RPAREN!);
ifExp    : (IF^ LPAREN! ifExp COMMA! atom COMMA! atom RPAREN!);
atom     : INT | STAT | expr;

Basically, I removed the "|" part for both your minMaxExpr and ifExp. I
then explicitly stated what an expr can be.

I also found it boring that I can't do 2+3 and have to write (2+3) from
the example grammar. I'm not even sure my fix above works :) 

Let me know please.

Phil

-----Original Message-----
From: Chris Bergstresser [mailto:chris at subtlety.com] 
Sent: Saturday, January 25, 2003 11:37 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] Help with simple parser?

Hi all --

   I've got a very simple grammar, but I keep getting recursion errors.
I
don't know enough to fix it.  My rules look like:

expr     : LPAREN! sumExpr RPAREN! ;
sumExpr  : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
prodExpr : minMaxExpr ((MUL^|DIV^|MOD^) minMaxExpr)* ;
minMaxExpr:	((MIN^|MAX^) LPAREN! minMaxExpr COMMA! minMaxExpr
RPAREN!) |
ifExp;
ifExp    : (IF^ LPAREN! ifExp COMMA! atom COMMA! atom RPAREN!) | atom;
atom     : INT | STAT | expr;

   ...but I don't want the parentheses around expr to be mandatory.  I'd
also like to enforce the same number of opening as closing parentheses.
As
it stands now equations like "(if(1, 2+3, 4))" won't parse, because the
"2+3" need wrapping parenthesis.  Obviously, I'd like "if(1, 2+3, 4)" to
be
valid, as well.  I've wandered -- rather uselessly -- through most of
the
examples.  Could someone help?

-- Chris


 

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/ 



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list