在野牛中无用的规则

时间:2014-07-17 12:28:10

标签: c++ grammar bison lexer

由于某种原因,野牛拒绝一个特定的规则,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;
}

2 个答案:

答案 0 :(得分:1)

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

bool
      : BOOLEAN          {}
      ;

也许不是

simple_expression
      : identifier
      | string
      ;

您打算编码

simple_expression
      : identifier
      | string
      | bool
      ;

答案 1 :(得分:0)

语法有两个问题。第一个是您已经看到的转移/减少冲突(并使用%expect 1解决。我更喜欢在语法中解决它并改为使用%expect 0。您可以通过删除{来自ELSE列表并添加一行

的{1}}
%token

宣布正确的关联性。您的语言实际上没有%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 {} ; 未在语法中的任何其他位置使用。这解释了&#34; 1无用的非终结和1无用的规则&#34;据野牛报道。为了能够为自己识别此类事物,您可以使用

bool

这将创建一个bison --report=solved -v string.y 文件,其中包含一个大而可读的报告,包括任何已解决的shift-reduce错误(例如您的IF-ELSE构造)以及由bison创建的一组完整状态。在尝试解决语法问题时非常有用。