[antlr-interest] Dont know how to allow Unary MINUS

N.V.S. Karunakar karunakar at objectsymphony.com
Mon Oct 15 00:13:23 PDT 2001


Hi,

   syntactic predicate recognizes the unary minus if its sub-tree is
constructed by the parser. your parser is not constructing the unary minus
sub-tree. a good place to add unary minus to your parser is atom.

change

> atom: unsignedNumber | ID | EEE | PII | LPAREN! assignExpr RPAREN! ;

to

atom: (MINUS^)? (unsignedNumber | ID | EEE | PII | LPAREN! assignExpr
RPAREN!);


> // Here comes my try to implement unary minus
>          	|	( #(MINUS a=expr b=expr) )=> #( MINUS
> a=expr b=expr ) {r =
> a-b;} | #( MINUS a=expr ) {r = -a;}
> //

   now, the above syntactic predicate recognizes and processes unary minus.
one more comment on the following rule in your grammar. the subrule (PLUS^ |
MINUS^) is not optional really. you may remove the "?" from "(PLUS^ |
MINUS^)?". I attach the grammar file for your reference.

> // Doing the arithmetic
> addExpr: multExpr (
>      pm: (PLUS^ | MINUS^)?
>      me: multExpr
>      exception catch [ RecognitionException ex ]
>      {
>                      System.out.println("Caught error in addExpr");
>                      reportError(ex.toString());
>      } )* ;

hope this helps,
karunakar

 

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: calc.g
Type: application/octet-stream
Size: 4403 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20011015/8111bd82/calc.obj


More information about the antlr-interest mailing list