Boris Koprinarov wrote:

Hi,

I am writing a tree grammar for Lua. In the language is defended operator precedence. The table looks like this:

  or

  and

  <     >     <=    >=    ~=    ==

  ..

  +     -

  *     /     %

  not   #     - (unary)

And here are my rules:

exp       

                :              logicalOrExpression;

 

 

// level 7 - " OR "

logicalOrExpression

                : logicalAndExpression ( 'or' logicalAndExpression )+ ->^(BINOP logicalAndExpression( OR logicalAndExpression)+ )


Do this instead:

logicalAndExpression ( OR^ logicalAndExpression )*
;


And make your 'or' a lexer token (and all the other 'literals'). You don't need a BINOP node, just the operator.

Repeat at all other levels and download say the Java grammar and crib from that.

 

likeExpression

                :              conExpression (LESS conExpression)+ -> ^(BINOP conExpression (LESS conExpression)+)

                |             conExpression (MORE conExpression)+ -> ^(BINOP conExpression (MORE conExpression)+)


When operators are the same precedence, do this (there are other forms but this is as neat as any):

:              conExpression (ops^ conExpression)*
;

ops : LESS | MORE| EQ .... etc.

;

Again, read through the Java grammar and trace out the _expression_ rule.

Jim

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to