[antlr-interest] WG: Question to the tree grammer

Richard Andrysek Richard.Andrysek at rg-mechatronics.com
Mon Jan 17 06:12:08 PST 2011

Hi Christian,


this seems like a correct implementation:




      :     MINUS unary -> ^(NEGATION unary)   

      |     PLUS unary ->  unary

      |     term              




The rest strays as before, only PLUS and MINUS are new tokens.



Von: Christian Lerch [mailto:christian.lerch at km-works.eu] 
Gesendet: Montag, 17. Januar 2011 13:14
An: antlr-interest at antlr.org; Richard Andrysek
Betreff: Re: [antlr-interest] WG: Question to the tree grammer


Hi Richard,
I also had a fierce fight with unary operators, although in the context of juxtaposition (which allows to ellide the * and write e.g. 2x instead of 2*x).
Please take a look at my small but tested grammar module that addresses this problem.
It implements 5 precedence levels for
1. binary +, -        (left-assoc)
2. multiplication    (left-assoc)
3. unary +, -
4. juxtaposition interpreted as multiplication    (left-assoc)
5. exponentiation    (right-assoc)
A little bit anoying with this grammar is the fact, that the multiplication implied by juxtaposition has precedence over normal multiplication.
On the other hand this can also be viewed as a feature, cause it allows you to write e.g. 1/a b which results in the same AST as 1/(a*b)
Of course you can drop this feature altogether by simply deleting rule arithSec and substituting all references by arithPrim.

Best wishes,

    Arithmetic expression grammar 
        with 5-level operator precedence and associativity
        with handling of unary minus and unary plus
        with juxtaposition mapped to multiplication
        without bracketedExpr: 
            unnecessary parenthesis can be removed later by the code generator
parser grammar ArithExpr2Parser;
options {
    language      = Java;
    output        = AST;
    tokenVocab     = ArithExpr2Lexer;
tokens {
formulae:    arithExpr EOF
arithExpr:    arithTerm ( (PLUS^|MINUS^) arithTerm )*
arithTerm:    arithFact ( (MULT^|DIV^) arithFact )*
arithFact:    MINUS arithSec -> ^(NEG arithSec)
    |            PLUS! arithSec
    |            arithSec
arithSec:    (arithPrim -> arithPrim) 
                 (p=arithPrim -> ^(TIMES $arithSec $p))*
arithPrim:    arithAtom ( POWER^ arithPrim )?
arithAtom:    IDENT
    |            NUMBER
    |            LPAREN arithExpr RPAREN    -> ^(PAREN arithExpr)

Am 17.01.2011 12:13, schrieb Richard Andrysek: 

I want to switch from yacc. So I've looked on Videos from Scott
Stanchfield <http://vimeo.com/user566590> <http://vimeo.com/user566590>  , especially on Part9 about a
tree grammar.
I've found a problem with an unary operator. The Interpreter in Eclipse
works fine, but the grammar tree not.
It simply can't accept things like this:
-5 - - + - 4
I've attached also my files. Is there a way, how can I ask somebody
else, than me? I assume, it is something with left/right association, is
Thank you for hints
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



km-works | Ing. Christian Lerch KG

Geschäftsführer: Christian Lerch · FN: 249408 b, LG Wien · UID: ATU57985808
Tel: +43 (720) 616 400 · e-mail: christian.lerch at km-works.eu 

Die Information in dieser E-Mail-Nachricht samt Anlagen ist vertraulich. Die Kenntnisnahme und Verwendung der hier enthaltenen Informationen ist nur denjenigen Personen gestattet, an die diese Kommunikation adressiert ist und/oder die zur Kenntnisnahme und Verwendung dieser Daten ausdrücklich ermächtigt wurden. Sollten Sie diesem Personenkreis nicht angehören, werden Sie hiemit davon in Kenntnis gesetzt, dass jegliche Weiter- und Wiedergabe, Vervielfältigung, Verbreitung, Verwendung und/oder Handeln aufgrund des Inhalts dieser Informationen zu unterlassen ist. Sollten Sie diese Nachricht versehentlich erhalten haben, ersuchen wir Sie, uns über diesen Umstand zu unterrichten. Bitte löschen Sie dann anschließend diese E-Mail endgültig von Ihrem System.

All information contained in this e-mail message including attachments is privileged and confidential. The information contained herein is intended solely for the use and knowledge of the individual(s) to whom this communication is addressed and/or others authorised to receive it. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, use and/or taking action in reliance on the contents of this information is strictly prohibited. If you accidentally received this e-mail, kindly let us know about this. On having done so, please delete this e-mail permanently from your system.

More information about the antlr-interest mailing list