我正在尝试在我的 cup 文件中定义我的语法,但出现以下错误。
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) INT
under symbol INT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) CHAR
under symbol CHAR
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) BOOL
under symbol BOOL
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) FLOAT
under symbol FLOAT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) INT
under symbol INT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) CHAR
under symbol CHAR
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) BOOL
under symbol BOOL
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) FLOAT
under symbol FLOAT
Resolved in favor of shifting.
以下是我的 cup 文件中定义的语法。
program ::= CLASS ID:i CURLBRACKETBEGIN memberdecls:m CURLBRACKETEND
{: RESULT = new Program(i,m); :};
memberdecls ::= fielddecls:fds methoddecls:m
{: RESULT = new Memberdecls(fds,m); :};
fielddecls ::= fielddecl:f fielddecls:fds
{: RESULT = new Fielddecls(f,fds); :}
|
{: RESULT = new Fielddecls(); :};
methoddecls ::= methoddecl:md methoddecls:mds
{: RESULT = new Methoddecls(md, mds); :}
|
{: RESULT = new Methoddecls(); :};
methoddecl ::= type:t ID:i PARENTHESISBEGIN argdecls:a PARENTHESISEND CURLBRACKETBEGIN fielddecls:f stmts:st CURLBRACKETEND optionalsemi:s
{: RESULT = new Methoddecl(t,i,a,f,st,s); :}
|
VOID ID:i PARENTHESISBEGIN argdecls:a PARENTHESISEND CURLBRACKETBEGIN fielddecls:f stmts:st CURLBRACKETEND optionalsemi:s
{: RESULT = new Methoddecl(i,a,f,st,s); :}
;
type ::= INT
{: RESULT = new Type("int"); :}
|
CHAR
{: RESULT = new Type("char"); :}
|
BOOL
{: RESULT = new Type("bool"); :}
|
FLOAT
{: RESULT = new Type("float"); :};
optionalsemi ::= SEMI
{: RESULT = new Optionalsemi(";"); :}
|
{: RESULT = new Optionalsemi(); :};
fielddecl ::= FINAL type:t ID:i optionalexpr:oe SEMI
{: RESULT = new Fielddecl("final", t, i, oe); :}
|
type:t ID:i optionalexpr:oe SEMI
{: RESULT = new Fielddecl(t,i,oe); :}
|
type:t ID:i SQUAREBRACKETBEGIN INTLIT:intl SQUAREBRACKETEND
我相信这与 Fielddecls 如何指代 Type 非终结符有关。但是,我不确定如何消除它。如果可能的话,有人可以提供一些指导来消除这些转变减少错误吗?