gpt4 book ai didi

c++ - Bison 中无用的规则

转载 作者:行者123 更新时间:2023-11-28 06:46:57 27 4
gpt4 key购买 nike

出于某种原因,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_expression 导致了问题吗?不使用的非终结符和规则,正如我阅读的那样,是

bool
: BOOLEAN {}
;

也许不是

simple_expression
: identifier
| string
;

你打算写代码

simple_expression
: identifier
| string
| bool
;

关于c++ - Bison 中无用的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com