我在为 if/else 开发语法规则时遇到问题......如果有人能解释我做错了什么,那就太好了
/* Terminal symbols */
%token <string> TINTEGER
%token <token> TLBRACE TRBRACE TSEMI TLPAREN TRPAREN
%token <token> TMAIN TROTATE TFORWARD THUMAN TZOMBIE TATTACK TIF TELSE
/* Statements */
%type <block> main_loop block
%type <statement> statement rotate forward if else attack insideStatement
/* Expressions */
%type <numeric> numeric
%type <boolean> bool
%%
main_loop : TMAIN TLBRACE block TRBRACE { std::cout << "Main entry point found!" << std::endl; }
;
block : statement { std::cout << "Single statement" << std::endl; }
| block statement {std::cout<<"Multiple statements" <<std::endl;}
;
statement : rotate TSEMI
| forward TSEMI
| if
| attack TSEMI
;
if : TIF TLPAREN bool TRPAREN insideStatement
| TIF TLPAREN bool TRPAREN else
| statement
;
else :TELSE TLBRACE block TRBRACE{std::cout << "Else" << std::endl;}
;
insideStatement : TLBRACE block TRBRACE else
;
它可以编译,但我真的不明白 if/else 如何在 Bison 上工作。