help-bison
[Top][All Lists]
Advanced

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

Question regarding short-circuited operators.


From: Eric Poulsen
Subject: Question regarding short-circuited operators.
Date: 25 Feb 2002 17:07:36 -0800

Hello all.  Before I ask: 

I've searched through the *entire* archive of this list. 
I've RTF Bison Manual. 
I've searched the web extensively for both Yacc and Bison. 
I've read through the grammar file for ANSI C. 
I've asked every programmer I know (Answer: "what's Bison?") 

The Problem: 

I'm creating an expression parser that supports variables.  Consider
this expression: 

x = 5 || y = 3 

After evaluating this simple statement, variable 'x' should be 5, and
variable 'y' should be undefined, if you take into account
short-circuiting of logical operators the way C, and several other
languages implement this. 

The Question (in a nutshell): 

How do you implement short-circuiting when using Bison (preferrably
elegantly)? 

Things I've tried: 

Throwing a YYERROR and using error recovery to skip to a token that
resets the short circuiting. For example '(a = 1 || b=2) && c=3' should
result in 'a' and 'c' equal to 1 and 3 (respectively), while b would be
undefined.  I've had limited success with this in that I can make it
skip the rest of the shorted section, but the value that caused the
short circuit is lost. 

Using a routine (called with a mid-rule action) to call yylex, and
'throw away' tokens until it finds a token it can resume at. 
Unfortunately, YYBACKUP doesn't work in this particular situation;
look-ahead, because of the look-ahead.

Things I am considering: 

Setting a flag so that yylex itself will skip tokens until it finds a
'continue' token, and return that to the parser instead.  The downside
is that the parser is unable to detect syntax errors in portions of the
expression that are skipped by the lexer. 

Setting a flag, so that individual rule actions will simply do nothing
while it is set.  Downside: This is lame, and a lot of work. 

Thank you for your time, and I hope I haven't wasted it due to lack of
research on my part.

-- Eric






reply via email to

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