Convert an extensive comment into a response:
How can I tell Bison not to evaluate the second expression if the first has already been evaluated as false?
This is your code that does the evaluation, not Bison; put the “guilt” where it belongs.
You need to determine that you are dealing with the && rule before evaluating RHS. Most likely, you need to paste the code after && and until the second int_expr , which pauses the evaluation if the first int_expr is evaluated to 0. You will also need to change all other evaluation code to check and obey the "do not evaluate" flag.
In addition, you have a Bison that parses and creates a program that you execute when the parsing is completed, and not evaluated during the analysis. This is a much larger set of changes.
Are you sure you put some code before the second int_expr? I can't seem to find a plausible way to do this. This is a good trick, but I can't find a way to actually tell Bison not to rate the second int_expr without ruining the whole evaluation.
You must write your code so that it does not evaluate when it should not be evaluated. Bison Syntax:
| int_expr '&&' {...code 1...} int_expr {...code 2...}
"Code 1" will check for $1 and arrange for an evaluation stop (set a global variable or something like that). "Code 2" will conditionally evaluate $4 (4, because "code 1" is now equal to 3 US dollars). The entire evaluation code must obey the instructions “code 1” - it must not evaluate whether “code 1” means “do not evaluate”. Or you can do what I suggested, and aselle suggested ; analyze and evaluate separately.
I aselle's second suggestion about the UNIX programming environment . There is a whole chapter on calculator development (they call it hoc for a higher order calculator), which is worth reading. Keep in mind, however, that the book was published in 1984 and prepared in advance for standard C by a good margin. There are no prototypes in C code, and (by modern standards) several freedoms are required. I have hoc6 (the latest version of hoc that they describe, as well as versions 1-3) in modern C - contact me if you want (see My profile).
This is the problem: I cannot stop evaluating in the middle of the rule, since I cannot use return (I can, but to no avail, it makes the program exit). | intExpr '&&' { if ($1 == 0) {/* turn off a flag */ } } intExpr { /* code */} | intExpr '&&' { if ($1 == 0) {/* turn off a flag */ } } intExpr { /* code */} After exiting $3 , $4 is automatically evaluated.
You can stop evaluating in the middle of the rule, but you need to code the code for the expression evaluation code to take this possibility into account. And when I said “stop evaluating”, I meant “stop doing calculations” and not “stop the parser on its tracks”. The parsing should continue; your code that calculates values ​​should only be evaluated when an evaluation is required, and not when an evaluation is not required. It could be a global flag (ugh!), Or you could have some other mechanism.
It is probably best to convert your parser into a code generator and execute the code after parsing it. Such a complication is why this is a good strategy.
@JonathanLeffler: You really are the king! This should be the answer !!!
Now this is the answer.