token priority?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

token priority?

John Ledbetter
Hello,

I am working with a grammar for a simple mathematical language, and I am
having trouble implementing a unary minus sign.

In flex/bison, I would do something like...

%left PLUS MINUS
%nonassoc UNARY

// ....
somerule : expr MINUS expr
              | MINUS expr %prec UNARY


What is the equivalent construct in grammatica?

Thanks,
John.


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users
Reply | Threaded
Open this post in threaded view
|

Re: token priority?

Iain McGinniss
Hi John,

In order to implement unary expressions I used a grammar structured like
the following:

AdditiveExpression = MultiplicativeExpression AdditiveExpressionTail* ;
AdditiveExpressionTail = (ADD | SUB) MultiplicativeExpression ;

MultiplicativeExpression = UnaryExpression MultiplicativeExpressionTail* ;
MultiplicativeExpressionTail = (MUL | DIV) UnaryExpression ;

UnaryExpression = SUB ConstantOrPostfixExpression
    | ConstantOrPostfixExpression ;

Which, for a simple expression like -4 + 5 gives a parse tree of

AdditiveExpression
|->MultiplicativeExpression
   |->UnaryExpression
      |->SUB
      |->ConstantOrPostfixExpression (4)
|->AdditiveExpressionTail
   |->ADD
   |->MultiplicativeExpression
      |->UnaryExpression
         |->ConstantOrPostfixExpression (5)

It's been a while since I've touched this stuff but that's the gist of it.

Cheers,
Iain

John Ledbetter wrote:

> Hello,
>
> I am working with a grammar for a simple mathematical language, and I
> am having trouble implementing a unary minus sign.
>
> In flex/bison, I would do something like...
>
> %left PLUS MINUS
> %nonassoc UNARY
>
> // ....
> somerule : expr MINUS expr
>              | MINUS expr %prec UNARY
>
>
> What is the equivalent construct in grammatica?
>
> Thanks,
> John.
________________________________________________________________________
This email (and any attachments) is private and confidential, and is intended solely for the
addressee. If you have received this communication in error please remove it and inform us via
telephone or email. Although we take all possible steps to ensure mail and attachments
are free from malicious content, malware and viruses, we cannot accept any responsibility
whatsoever for any changes to content outwith our administrative bounds. The views represented
within this mail are solely the view of the author and do not reflect the views of the organisation
as a whole.
________________________________________________________________________
Graham Technology plc                                                http://www.grahamtechnology.com
________________________________________________________________________

_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users

Iain.McGinniss.vcf (349 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: token priority?

Per Cederberg
Thanks Iain! Great example and just the solution
I'd recommend.

Note to John: You can also check the calculator
example grammar for a similar solution.

Cheers,

/Per

Iain McGinniss wrote:

> Hi John,
>
> In order to implement unary expressions I used a grammar structured like
> the following:
>
> AdditiveExpression = MultiplicativeExpression AdditiveExpressionTail* ;
> AdditiveExpressionTail = (ADD | SUB) MultiplicativeExpression ;
>
> MultiplicativeExpression = UnaryExpression MultiplicativeExpressionTail* ;
> MultiplicativeExpressionTail = (MUL | DIV) UnaryExpression ;
>
> UnaryExpression = SUB ConstantOrPostfixExpression
>    | ConstantOrPostfixExpression ;
>
> Which, for a simple expression like -4 + 5 gives a parse tree of
>
> AdditiveExpression
> |->MultiplicativeExpression
>   |->UnaryExpression
>      |->SUB
>      |->ConstantOrPostfixExpression (4)
> |->AdditiveExpressionTail
>   |->ADD
>   |->MultiplicativeExpression
>      |->UnaryExpression
>         |->ConstantOrPostfixExpression (5)
>
> It's been a while since I've touched this stuff but that's the gist of it.
>
> Cheers,
> Iain
>
> John Ledbetter wrote:
>> Hello,
>>
>> I am working with a grammar for a simple mathematical language, and I
>> am having trouble implementing a unary minus sign.
>>
>> In flex/bison, I would do something like...
>>
>> %left PLUS MINUS
>> %nonassoc UNARY
>>
>> // ....
>> somerule : expr MINUS expr
>>              | MINUS expr %prec UNARY
>>
>>
>> What is the equivalent construct in grammatica?
>>
>> Thanks,
>> John.
>
> ________________________________________________________________________
> This email (and any attachments) is private and confidential, and is
> intended solely for the
> addressee. If you have received this communication in error please
> remove it and inform us via
> telephone or email. Although we take all possible steps to ensure mail
> and attachments
> are free from malicious content, malware and viruses, we cannot accept
> any responsibility
> whatsoever for any changes to content outwith our administrative bounds.
> The views represented
> within this mail are solely the view of the author and do not reflect
> the views of the organisation
> as a whole.
> ________________________________________________________________________
> Graham Technology plc                                                
> http://www.grahamtechnology.com
> ________________________________________________________________________
>
> _______________________________________________
> Grammatica-users mailing list
> [hidden email]
> http://lists.nongnu.org/mailman/listinfo/grammatica-users


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users