【问题标题】:Grammar Rule for flex/bison not workingflex/bison 的语法规则不起作用
【发布时间】:2015-02-11 13:47:43
【问题描述】:

所以我的目标是能够确定输入是否可以接受。以下是可接受的输入:

“u”、“d”、“l”、“r”、“n”的任意组合

**Example of valid inputs:**
udlrn
uuuuuuuuuuuu
dunrldd
dddddllll
dldnrrrrrrrrrrr

**Example of invalid inputs:**
abc
abcudlr
xudz
dclrxy

这是我的 Flex 代码

%%
"u"   return UP;
"d"   return DOWN;
"l"   return LEFT;
"r"   return RIGHT;
"n"   return NONE;
\n    return END;
%%

这是我的野牛代码

%token UP
%token DOWN
%token LEFT
%token RIGHT
%token NONE
%token END

%%
start:   directions END
         { 
             printf("\nParse complete with acceptable input\n"); 
         }
;

directions: direction
            |
            directions direction
;

direction: UP | DOWN | LEFT | RIGHT | NONE
;
%%

但是,当我提供如下输入时:

  • ldruabc

即使此输入无效,我也会收到解析完成消息。

【问题讨论】:

    标签: bison yacc lex


    【解决方案1】:

    任何与您的弹性代码中的任何模式都不匹配的内容都将被回显到标准输出并被忽略,因此按照目前的结构,任何由单行组成的输入都是可以接受的。

    您可能应该在其他弹性规则之后添加一条包罗万象的规则:

    .    return *yytext;
    

    这样,输入中的任何其他字符都将返回到解析器,在那里它会触发语法错误。

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多