OK this is kind of an odd question because what I have here works the way I want it to. What I'm doing is writing a parser for a lambda calculus expression. So an expression can be one of four things:
- variable
- constant
- (expression expression)
- (lambda variable.expression)
Now as you can see, the last two expressions have expressions within them. What I was trying to do was determine the overall expression so I can report which type it is. So for example the expression ((lambda x.(f1 x)) 100) is a combination overall. My idea was to return an END token from flex when it reached the end of file. My code looks like this:
overallexpr: combo END { printf(" The overall expression is a combination\n"); } |
constant END { printf(" The overall expression is a constant\n"); } |
VARIABLE END { printf(" The overall expression is a variable\n"); } |
l_expr END { printf(" The overall expression is a lambda expression\n"); }
;
expr: combo | constant | VARIABLE | l_expr
;
combo: LPARENS expr expr RPARENS
;
constant: FUNCTION | NUMBER
;
l_expr: LPARENS LAMBDA VARIABLE DOT expr RPARENS
;
If I put that END token after the four possibilities in overallexpr like combo END instead of just combo, it doesn't work. But the END token is received by the parser. If I print each token as it is read (with variable, function, and number values) it looks like this
LPARENS LPARENS LAMBDA VARIABLE x DOT LPARENS FUNCTION f1 VARIABLE x RPARENS RPARENS NUMBER 100 RPARENS END Sorry, Charlie
It may be hard to tell but this should work. The combination ends with the RPARENS and there's an END token right after it. But it doesn't evaluate as an overall expression. However if I take out the END tokens it seems to work every time. I always get an overall message printed, even though the productions of overallexpr and expr are exactly the same. The output is identical to the last one except it says "The overall expression is a combination" before the END token. So my question is why? Does bison always just try the earlier productions first? And why would it work without the END but not with it? Especially because you can see the END token right after it says it's a combination. I'm just trying to get a better understanding of how Bison works.