[antlr-interest] Adding parenthesis to the calc grammer

Bryan Ewbank ewbank at gmail.com
Sun Feb 20 17:54:52 PST 2005


Replace this:
     18 
     19 atom:   INT
     20     ;
     21 
With something like this:

     atom : LPAREN! expr RPAREN!
          | INT
          ;

The "!'s discard the parens while keeping the expression in the right
place, which allows the TreeParser to work correctly -- since there's
no additional node types in the tree.

If you want, you can keep the LPAREN for tracking:

     atom : LPAREN^ expr RPAREN!
          | INT
          ;

This requires changes to TreeParser as well:

     :   #(PLUS a=expr b=expr)   {r = a+b;}
     |   #(STAR a=expr b=expr)   {r = a*b;}
     |   #(LPAREN r=expr)


On Sun, 20 Feb 2005 13:42:47 +0200, Eran Werner <eran at onigma.com> wrote:
> I am trying to extends the calc grammar example to support parenthesis. 
> I would like expressions with unnecessary parenthesis to be supported as
> well. Any tips?


More information about the antlr-interest mailing list