help-bison
[Top][All Lists]
Advanced

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

Re: are there user defined infix operators?


From: Hans Åberg
Subject: Re: are there user defined infix operators?
Date: Thu, 8 Nov 2018 23:37:59 +0100

> On 8 Nov 2018, at 22:34, Uxio Prego <address@hidden> wrote:
> 
>> Take a simple example, a + b*c #, where # is the end marker. First put the a 
>> on the value stack, and the + on the operator stack, and then the b on the 
>> value stack. When the * comes by, it has higher precedence than the + on top 
>> of the operator stack, so it must be stacked. Then the c comes by, so put it 
>> on the value stack. Finally the end marker #, which has lower precedence 
>> than *, so let * operate on the value stack, and put back its value, b*c. 
>> Next is the +, and # has lower precedence, so + operates on the value stack, 
>> computing a + (b*c), which is put back onto the value stack. Then the 
>> operator stack empty, so the process is finished, and the value stack has 
>> the value.
>> [...]
> 
> The example and explanation are worth a thousand words,
> thank you very much. So I use a simple grammar like that, and
> the stack data structures, and if necessary feed the lexer back
> with data from the parser once the user requests some infix
> operators.

It is only if you want to have a prefix and an infix or postfix operator with 
the same name, like operator- or operator++ in C++, that there is a need for 
handshake between the lexer and the parser, and it suffices with a boolean 
value that tells whether the token last seen is a prefix operator. Initially 
set to false, the prefix operators set it to true in the parser, and all other 
expression tokens set it to false. Then, when the lexer sees an operator that 
can be both a prefix and an infix or postfix, it uses this value to 
disambiguate. I leave it to you to figure out the cases, it is not that hard, 
just a bit fiddly. :-)





reply via email to

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