我正在尝试构建一个if()else if()else if()else example.Is有没有办法解决这个没有%nonassoc并指定终端符号? 正如你在代码中看到的那样,我有一些flex和bison.At flex没有任何关键字elseif,只有其他和if。
Flex
keyword_if [Ii][Ff]
keyword_else [Ee][Ll][Ss][ee]
keyword_start [Ss][Tt][Aa][Rr][Tt]
keyword_then [Tt][Hh][Ee][Nn]
{keyword_if} return key_if;
{keyword_else} return key_else;
{keyword_start} return key_start;
{keyword_then} return key_then;
Bison
%token key_if
%token key_else
%token key_start
%token key_then
%start:program;
program:start commands ;
commands:command
|commands command;
command:ifcommand
|whilecommand
|forcommand;
ifcommand:key_if exp key_then commands checkelse;
checkelse:/*empty*/
|key_else commands;
适用于嵌套在其他地方的简单版本 而不喜欢这个
if a == 10 then
/* some commands */
else if a == 20 then
/*some commands*/
else if a == 30 then
/*some commands*/
else
/*some commands*/