help-bison
[Top][All Lists]
Advanced

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

Re: How to ignore a token if it doesn't fit in with a rule?


From: John P. Hartmann
Subject: Re: How to ignore a token if it doesn't fit in with a rule?
Date: Sat, 23 Jul 2011 13:55:54 +0200

Well, controlling the scanner from the parser is never going to work
in general as the scanner can be way ahead.  I'm afraid you'll have to
write a parallel "parser" using exclusive star conditions.  However
this "parser" can be vastly simplified as it only has to figure out
whether to return a comment.

If that becomes cumbersome, there is something wrong with the grammar
(or your understanding of same).

For example, the REXX language uses blank as both an operator and a
normal token delimiter.  So the scanner needs to keep track of
expressions at least to know when it expects an operator.  The
alternative would lead to an enormous number of shift/reduce conflicts
(which all would be resolved by shifting), but you'll likely regret
this as you are bound to miss a shift/reduce conflict that you need to
resolve.

On 23 July 2011 12:43, Hans Aberg <address@hidden> wrote:
> On 23 Jul 2011, at 02:40, uclacasey wrote:
>
>> I'm writing a program that handles comments as well as a few other things. If
>> a comment is in a specific place, then my program does something.
>>
>> Flex passes a token upon finding a comment, and Bison then looks to see if
>> that token fits into a particular rule. If it does, then it takes an action
>> associated with that rule.
>>
>> Here's the thing: the input I'm receiving might actually have comments in
>> the wrong places. In this case, I just want to ignore the comment rather
>> than flagging an error.
>>
>> My question:
>> How can I use a token if it fits into a rule, but ignore it if it doesn't?
>> Can I make a token "optional"?
>
> You can turn start conditions on/off by having a global variable which is set 
> in the parser actions. Then the first thing the lexer does is checking this 
> variable, setting the correct start condition.
>
> You might check the Usenet newsgroup comp.compilers for other suggestions.
>
> Hans
>
>
>
> _______________________________________________
> address@hidden https://lists.gnu.org/mailman/listinfo/help-bison
>



reply via email to

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