簡體   English   中英

野牛的無用規則

[英]Useless rule in Bison

出於某種原因,野牛拒絕了一條特定的規則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;
}

您確定是引起問題的notequal_expressionnotequal_expression嗎? 在我閱讀時,未使用的非終結符和規則是

bool
      : BOOLEAN          {}
      ;

也許代替

simple_expression
      : identifier
      | string
      ;

你打算編碼

simple_expression
      : identifier
      | string
      | bool
      ;

語法有兩個問題。 第一個是您已經看到的移位/減少沖突(並已用%expect 1 。我更喜歡在語法中解決它,而改用%expect 0 。您可以通過從%token列表中刪除ELSE來實現,添加一條線

%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 正如bison所報告的,這同時解釋了“ 1個無用的非終結規則和1個無用的規則”。 為了能夠自己識別這種事情,您可以使用

bison --report=solved -v string.y

這將創建一個string.output文件,該文件將包含一個大型但可讀的報告,包括任何已解決的移位減少錯誤(例如IF-ELSE構造)以及由bison創建的完整狀態集。 在嘗試對語法問題進行故障排除時非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM