[antlr-interest] Re: Perority Enforcing using Antlr Grammar

Muhammad Masoom Alam doublemalam at yahoo.co.uk
Sun Jun 18 22:47:19 PDT 2006


Dear All,

If i evaluate the following expression, How the AST
will be created. 
  6 * 4 + 3 + 9

I start the first plus as root and go further:

    *
   / \
  6   +
     / \
    4   +
       / \
      3   9

if i evaluated the tree like this, addition takes
place before the mulitiplication which is not desired.

If any one can help me, how this tree will be
evaluated.

Thanks in advance.
MA


--- Anthony Youngman
<Anthony.Youngman at eca-international.com> wrote:

> To expand this ...
> 
> Firstly there should be plenty of examples in
> various grammars but basically the idea is to bind
> the highest priority stuff first, then the lower
> stuff. Using standard mathematical priorities...
> 
> highestexpr: ID ( ( "*" | "/" ) ID )*;
> midexpr: highestexpr ( ( "+" | "-" ) highestexpr );*
> expr: midexpr ( ( "AND" | "OR" ) midexpr );*
> 
> To explain what it's doing, "highestexpr" grabs all
> your variable names, and binds them to the highest
> operators (here arithmetical multiply and divide).
> "midexpr" then comes along and binds all the
> "highestexpr"s to the add and subtract. Finally
> "expr" comes along and binds all the "midexpr" to
> your lowest operators, "AND" and "OR". Just add more
> levels for more levels of priority.
> 
> As an example, let's take
> 
> 6 * 4 AND 3 + 9
> ID * ID AND ID + ID
> highestexpr AND highestexpr + highestexpr
> midexpr AND midexpr
> expr
> 
> Cheers,
> Wol
> 
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf
> Of Koehne Kai
> Sent: 16 June 2006 09:33
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Re: Perority Enforcing
> using Antlr Grammar
> 
> Hi,
>  
> try something like
>  
> expr1: expr2 ("OR" expr2)*;
> expr2: expr3 ("AND" expr3)*;
> expr3: ID | "(" expr1 ")";
>  
> Regards,
>  
> Kai Koehne
> 
>  
> ________________________________
> 
> Von: antlr-interest-bounces at antlr.org im Auftrag von
> Muhammad Masoom Alam
> Gesendet: Fr 16.06.2006 08:42
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] Perority Enforcing using
> Antlr Grammar
> 
> 
> 
> Hi all,
> 
>  sorry if my question is a stupid one, as i am a new
> bie. How to enforce perority rules using Antlr.
> 
> e.g. I have the following conditions
> 
>   (Predicate1 And (Predicate2 Or Predicate3))
> 
> In the above, Grammar, paranthesis are used to group
> and enforce perority, but how this will be done
> using
> the Antlr Grammar.
> 
> Thanks in Advance.
> MA
> 
> Muhammad Masoom Alam
> Security Engineering Universität Innsbruck
> Institut für Informatik Technikerstraße 13  A - 6020
> Innsbruck
> Tel.: ++43 - 512 - 507 6462
> http://qe-informatik.uibk.ac.at/
> 
> 
>                
>
___________________________________________________________
> Copy addresses and emails from any email account to
> Yahoo! Mail - quick, easy and free.
> http://uk.docs.yahoo.com/trueswitch2.html
> 
> 
> 
> *
>
************************************************************************
> *
> 
> This transmission is intended for the named
> recipient only. It may contain private and
> confidential information. If this has come to you in
> error you must not act on anything disclosed in it,
> nor must you copy it, modify it, disseminate it in
> any way, or show it to anyone. Please e-mail the
> sender to inform us of the transmission error or
> telephone ECA International immediately and delete
> the e-mail from your information system.
> 
> Telephone numbers for ECA International offices are:
> Sydney +61 (0)2 8272 5300, Hong Kong + 852 2121
> 2388, London +44 (0)20 7351 5000 and New York +1 212
> 582 2333.
> 
> *
>
************************************************************************
> *
> 



		
___________________________________________________________ 
Copy addresses and emails from any email account to Yahoo! Mail - quick, easy and free. http://uk.docs.yahoo.com/trueswitch2.html


More information about the antlr-interest mailing list