【问题标题】:Useless rule in BisonBison 中的无用规则
【发布时间】:2014-07-17 12:28:09
【问题描述】:

由于某种原因,野牛拒绝了一个特定的规则,notequal_expression,请注意我刚刚开始学习整个概念所以我的思路不是那么成熟,输入文件:(错误是:“string.y包含1 个无用的非终结符和 1 个无用的规则。”)

    /* Parser for StringC */

%{

/* ------------------------------------------------------------------
   Initial code (copied verbatim to the output file)
   ------------------------------------------------------------------ */

// Includes
#include <malloc.h>  // _alloca is used by the parser
#include <string.h>  // strcpy

#include "lex.h"     // the lexer

// Some yacc (bison) defines
#define YYDEBUG 1         // Generate debug code; needed for YYERROR_VERBOSE
#define YYERROR_VERBOSE // Give a more specific parse error message 

// Error-reporting function must be defined by the caller
void Error (char *format, ...);

// Forward references
void yyerror (char *msg);

%}

/* ------------------------------------------------------------------
   Yacc declarations
   ------------------------------------------------------------------ */

/* The structure for passing value between lexer and parser */
%union {
   char *str;
}

%token ERROR_TOKEN IF ELSE PRINT INPUT ASSIGN EQUAL NOTEQUAL
%token CONCAT END_STMT OPEN_PAR CLOSE_PAR
%token BEGIN_CS END_CS 
%token <str> ID STRING BOOLEAN

/*%type <type> type simple_type cast*/

%expect 1  /* shift/reduce conflict: dangling ELSE */
           /*    declaration */
%%

/* ------------------------------------------------------------------
   Yacc grammar rules
   ------------------------------------------------------------------ */

program
      : statement_list
        ;

statement_list
      : statement_list statement
      | /* empty */
      ;

statement
      : END_STMT                    {puts ("Empty statement");}
      | expression END_STMT         {puts ("Expression statement");}
      | PRINT expression END_STMT   {puts ("Print statement");}
      | INPUT identifier END_STMT   {puts ("Input statement");}
      | if_statement                {puts ("If statement");}
      | compound_statement          {puts ("Compound statement");}
      | error END_STMT              {puts ("Error statement");}
      | notequal_expression         {puts ("Not equal statement");}
      ;

/* NOTE: This rule causes an unresolvable shift/reduce conflict;
   That's why %expect 1 was added (see above) */
if_statement
      : IF OPEN_PAR expression CLOSE_PAR statement optional_else_statement
      ;

optional_else_statement
      : ELSE statement
      | /* empty */
      ;

compound_statement
      : BEGIN_CS statement_list END_CS
      ;

expression
      : equal_expression
      | OPEN_PAR expression CLOSE_PAR
      ;

equal_expression
      : expression EQUAL assign_expression
      | assign_expression
      ;

notequal_expression
      : expression NOTEQUAL assign_expression 
      | NOTEQUAL assign_expression
      ;

assign_expression
      : identifier ASSIGN assign_expression
      | concat_expression
      ;

concat_expression
      : concat_expression CONCAT simple_expression
      | simple_expression
      ;

simple_expression
      : identifier
      | string
      ;

identifier
      : ID              {}
      ;

string
      : STRING          {}
      ;

bool
      : BOOLEAN          {}
      ;

%%
/* ------------------------------------------------------------------
   Additional code (again copied verbatim to the output file)
   ------------------------------------------------------------------ */

词法分析器:

/* Lexical analyzer for StringC */

%{

/* ------------------------------------------------------------------
   Initial code (copied verbatim to the output file)
   ------------------------------------------------------------------ */

// Includes
#include <string.h>   // strcpy, strncpy
#include <io.h>       // isatty
#ifdef MSVC
#define  isatty _isatty  // for some reason isatty is called _isatty in VC..
#endif

#define  _LEX_CPP_   // make sure our variables get created
#include "lex.h"
#include "lexsymb.h"

extern "C" int yywrap (); // the yywrap function is declared by the caller

// Forward references
void Identifier ();
void StringConstant ();
void BoolConstant ();
void EatComment ();

//// End of inititial code
%}

/* ------------------------------------------------------------------
   Some macros (standard regular expressions)
   ------------------------------------------------------------------ */

LETTER   [a-zA-Z_]
DIGIT    [0-9]
IDENT    {LETTER}({LETTER}|{DIGIT})*
STR      \"[^\"]*\"
BOOL     \(false|true)\
WSPACE   [ \t]+


/* ------------------------------------------------------------------
   The lexer rules
   ------------------------------------------------------------------ */
%%

"if"     {return IF;}
"else"   {return ELSE;}
"print"  {return PRINT;}
"input"  {return INPUT;}
"="      {return ASSIGN;}
"=="     {return EQUAL;}
"!="     {return NOTEQUAL;}                         /* Not equal to */
"+"      {return CONCAT;}
";"      {return END_STMT;}
"("      {return OPEN_PAR;}
")"      {return CLOSE_PAR;}
"{"      {return BEGIN_CS;}
"}"      {return END_CS;}
{BOOL}   {BoolConstant ();   return BOOLEAN;}
{STR}    {StringConstant (); return STRING;}
{IDENT}  {Identifier ();     return ID;}
"//"     {EatComment();}                            /* comment:    skip */
\n       {lineno++;}                                /* newline:    count lines */
{WSPACE} {}                                         /* whitespace: (do nothing) */
.        {return ERROR_TOKEN;}                      /* other char: error, illegal token */

%%

/* ------------------------------------------------------------------
   Additional code (again copied verbatim to the output file)
   ------------------------------------------------------------------ */

// The comment-skipping function: skip to end-of-line
void EatComment()  {
    char c;

   while ((c = yyinput()) != '\n' && c != 0);
    lineno++;
}

// Pass the id name
void Identifier ()  {
   yylval.str = new char[strlen(yytext)+1];
   strcpy (yylval.str, yytext);
}

// Pass the string constant
void StringConstant()  {
   int l = strlen(yytext)-2;
   yylval.str = new char[l+1];
   strncpy (yylval.str, &yytext[1], l); yylval.str[l] = 0;
}


void BoolConstant()    {
    int l = strlen(yytext)-2;
    yylval.str = new char[l+1];
    strncpy(yylval.str, &yytext[1], l); yylval.str[l] = 0;
}

【问题讨论】:

  • 对于alloca(这可能是个坏主意)使用&lt;alloca.h&gt;,对于malloc,使用&lt;stdlib.h&gt;。包含&lt;malloc.h&gt; 是个坏主意。

标签: c++ grammar bison lexer


【解决方案1】:

您确定是notequal_expression 导致了问题吗?正如我所读,未使用的非终结符和规则是

bool
      : BOOLEAN          {}
      ;

也许不是

simple_expression
      : identifier
      | string
      ;

你打算编码

simple_expression
      : identifier
      | string
      | bool
      ;

【讨论】:

  • 那我很可能是错的……我附上了另一个文件。
  • @MiguelP:当我在您发布的 .y 文件上运行 bison 时,错误消息将 bool: BOOLEAN 标识为无用规则且非终端,正如 David 所描述的那样。
【解决方案2】:

语法有两个问题。第一个是您已经看到的移位/减少冲突(并使用%expect 1 解决。我更喜欢在语法中解决它并改用%expect 0。您可以通过从@987654324 中删除ELSE 来做到这一点@list 并添加一行

%right THEN ELSE

声明右结合性。您的语言实际上没有 THEN 关键字,但这很好。然后,您可以完全删除 optional_else_statement 的规则并改写 if_statement 的规则,如下所示:

if_statement
      : IF OPEN_PAR expression CLOSE_PAR statement  %prec THEN
      | IF OPEN_PAR expression CLOSE_PAR statement ELSE statement
      ;

有些人更喜欢以这种方式解决它,而其他人则提倡%expect 1 方法。我更喜欢这种方式,但既然你有两种方法,你当然可以自己选择。

对于另一个问题,无用的规则肯定是这个:

bool
      : BOOLEAN          {}
      ;

因为非终结符 bool 在语法中的其他任何地方都没有使用。这说明了野牛报告的“1 个无用的非终结符和 1 个无用的规则”。为了能够自己识别这种东西,您可以使用

bison --report=solved -v string.y

这将创建一个string.output 文件,该文件将包含一个大但可读的报告,其中包括任何已解决的移位减少错误(例如您的 IF-ELSE 构造)以及由野牛创建的一组完整状态。在尝试解决语法问题时非常有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2020-02-16
    相关资源
    最近更新 更多