grammatica-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Grammatica-users] token priority?


From: Per Cederberg
Subject: Re: [Grammatica-users] token priority?
Date: Mon, 20 Nov 2006 23:48:57 +0100
User-agent: Thunderbird 1.5.0.8 (Macintosh/20061025)

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
address@hidden
http://lists.nongnu.org/mailman/listinfo/grammatica-users




reply via email to

[Prev in Thread] Current Thread [Next in Thread]