I am using YACC to do my compiler homework project. I found that my program could not get the syntax tree. So I printed it all out to see what is happening. According to my result, it seems that ClassDecl does not reduce to ClassDeclList here. But I can’t understand why… can anyone help me out?
The sample input is:
program ex11;
class ab {
}
It printed out as:
programXXXX ex11ID
semicon abID
RBRACEnum
ClassBody ClassDecl ClassDecl1 Error!
The first three lines are messages I printed from my LEX file, to ensure that the characters are recognized correctly.
According to the information, the parser successfully reduces {} to ClassBody and class ab {} to ClassDecl. And then it does not reduce to ClassDeclList, is it because I am writing a left recursive grammar here?
This is the part of my YACC rule base for the inference:
Program: PROGRAMnum IDnum SEMInum ClassDeclList
{printf("program"); $$ = MakeTree(ProgramOp,$4, MakeLeaf(IDNode,$2)); printtree($$,0);};
ClassDeclList: ClassDecl
{printf("ClassDeclList1");$$ = MakeTree(ClassOp,NullExp(),$1); printf("ClassDeclListend");};
|ClassDecl ClassDeclList
{printf("ClassDeclList2");$$ = MakeTree(ClassOp,$2,$1); printf("ClassDeclList");};
ClassDecl: CLASSnum IDnum ClassBody
{printf("ClassDecl");$$=MakeTree(ClassDefOp,$3,MakeLeaf(IDNode,$2)); printf("ClassDecl1");};
Have you tried
instead of
?
I remember this fixing many problems when I used to use CUP.