I'm working on a project in yacc and am getting a shift/reduce error but I can't figure out why I'm getting it. I've been looking through the y.output file but am not quite sure how to read it. My y.output file exceeds the character limit on SO so I threw it on pastebin: http://pastebin.com/AQ2UtAip. Any ideas on how to fix this?
-
I'm curious, did you read the book flex & bison? – Feb 09 '11 at 01:57
-
Might i also suggest instead of using ';' at the end of each statement, use mEOS and aEOS denoting many (0 or more) or additional (1 or more) end of statements. and by defining EOS as ';' and '\n'. Really, theres no reason why new languages need ';' everywhere. I did this in my language and it works well. – Feb 09 '11 at 02:13
-
@acidzombie24 I haven't read the book flex & bison. This code was written for a compiler class I'm taking right now. We were pretty much just given a 4 page project spec, a few starter files, and told to go figure it out :) – Ian Burris Feb 12 '11 at 19:48
-
blcArmadillo: Why did you accept the other answer... its not correct either... – Feb 13 '11 at 07:34
3 Answers
I'm not entirely sure, but I think the problem is that upon seeing a T_Identifier
at the beginning of a StmtBlock
, the parser cannot determine whether it is seeing a VariableDecl
or an Expr
with only one token of lookahead. If you can change the language spec, one easy fix would be to require a keyword like var
before a variable declaration.

- 26,504
- 11
- 85
- 105
-
I agree that the problem is that T_Identifier. This is actually a problem in several languages, most notably C++. What they did for GCC was to do some trickery with the lexer and check the symbol table so they should know whether to return an identifier or a token that indicates a type. Adding a keyword like var would be another solution. – Samsdram Feb 06 '11 at 05:59
I'm not entirely sure, but I think the problem is here: VariableDeclList: VariableDeclList VariableDecl
When spotting something that might be a VariableDeclList
, it may recursively follow the VariableDeclList
until running out of stack. Try swapping the order of VariableDeclList
with VariableDecl
?

- 102,305
- 22
- 181
- 238
Try fixing or using Epsilon in a different way. I suspect that VariableDeclList gets reduced and now it doesnt know if it needs to reduce StmtList first before doing Stmt or to not reduce it and use VariableDecl. I know VariableDeclList is not the problem because you do it in both rules, however now StmtList could be reduce before knowing which rule to follow which is the problem (because not all rules reduce it at the same place/order).
state 74
38 StmtBlock: '{' VariableDeclList . StmtList '}'
39 VariableDeclList: VariableDeclList . VariableDecl
StmtList: StmtList Stmt
| Epsilon