[antlr-interest] Binary Expression Problem!

Sebastian Kaliszewski Sebastian.Kaliszewski at softax.com.pl
Tue Jun 20 03:09:03 PDT 2006


Sebastian Kaliszewski wrote:
>>> The Grammar below is structured in a way that it
>>> should observes the perority of relational operators.
>>> But in this Grammar one problem persists i.e. a Binary
>>> Expression should be comprised of two operands where
>>> as from the Grammar it is obvivous that it may come up
>>> with a single operand too. Could any body help me in
>>> this regard to overcome the single operand issue
>>> without loosing the operator's perority.
>>> e.g. the Grammar permits: "Hello" where as the
>>> required expression is "Hello" = "Hello" without
>>> loosing the operator's perority.
>>>
>>> BinaryExp: EqualityExp;
>>>
>>> EqualityExp: RelationalExp ((Not_Equal|Equal)
>>> RelationalExp)*;
>>>
>>> RelationalExp: additiveExp ((GT|GTE|LT|LTE)
>>> additiveExp)*;
>>>
>>> additiveExp: MultipicativeExp
>>> ((PLUS|MINUS)MultipicativeExp)*;
>>>
>>> MultipicativeExp: SimpleExp ((MUL|DIV|MOD)
>>> SimpleExp)*;
>>>
>>> SimpleExp: STRING|CONSTANTVALUE;
>>>
>  >
> 
>> Try:
>>
>> EqualityExp: RelationalExp ((Not_Equal|Equal) RelationalExp)+
>>
>> I forget if antlr supports + though, i usually only use ? * :)
> 
> 
> I'm affraid it's not what Muhammad wanted. I.e. this will only accept 
> "hello" = "hello" but will not accept things like 10+40.
> 
> Something like this might be the sollution (although I find that 
> restriction somewhat strange)
> 
> Binary: Relational ((EQ|NEQ) Relational)+ | BiRelational;
> 
> BiRelational: Additive ((GT|GTE|LT|LTE) Additive)+ | BiAdditive;
> 
> Relational: Additive ((GT|GTE|LT|LTE) Additive)*;
> 
> BiAdditive: Multiplicative ((PLUS|MINUS) Multiplicative)+|BiMultiplicative;
> 
> Additive: Multiplicative ((PLUS|MINUS) Multiplicative)*;
> 
> BiMultiplicative: Simple ((MUL|DIV|MOD) Simple)+;
> 
> Multiplicative: Simple ((MUL|DIV|MOD) Simple)*;
> 
> Simple: STRING|CONSTANTVALUE;

Replying to myself

One probbaly needs to set predicates, like:

BiRelational: (Additive (GT|GTE|LT|LTE))=> Additive ((GT|GTE|LT|LTE) 
Additive)+ | BiAdditive;

To resolve ambiguities. But that's probably the way it could be acheivied.

rgds
-- 
Sebastian Kaliszewski


More information about the antlr-interest mailing list