Ian Burris
Ian Burris

Reputation: 6515

Can't figure out what is causing a shift/reduce errors in yacc

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?

Upvotes: 4

Views: 259

Answers (3)

user34537
user34537

Reputation:

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

Upvotes: 0

sarnold
sarnold

Reputation: 104020

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?

Upvotes: 0

Zifre
Zifre

Reputation: 26998

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.

Upvotes: 2

Related Questions