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

Kjell Nilsson kjell at oops.se
Mon Oct 15 01:25:57 PDT 2001


Thank you very much.

I was thinking about adding a rule in the parser after I sent this
question to you. But now with your explanation I see it more
clear.  Also the ? (option subrule) was probably something I
left in there in my atempt to get the parser to accept an operator with
both one or two expressions.

Well I have tried your solution now and it works great. Thanks once
more.

Also I would like to thank everybody (Terrence + ?) who made this Antlr, 
it is
an outstanding piece of open software.

--kjell


On måndag, oktober 15, 2001, at 09:13  AM, N.V.S. Karunakar wrote:

> 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/
>
>

 

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



More information about the antlr-interest mailing list