繁体   English   中英

我的Bison语法产生语法错误

[英]Syntax Error in my grammar production with Bison

作为测试文件,我正在使用以下代码来查看解析器是否运行良好:

/* A test program  */

void main(void){
int x;
int y;
    int z;

x= 10;
y = 20;
z =x* (x+y);
}

但是,我在第一个空值之后立即收到语法错误,而且我不太明白为什么它甚至不能到达参数部分。 如果有任何提示或看不到的提示,我将不胜感激。 我知道我还没有解决悬而未决的问题,但是对于本次测试而言,这不应该成为问题。

到目前为止,这是我的解析器:

%{

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/* external function prototypes */
extern int yylex();
extern int initLex(int ,  char **);



/* external global variables */

extern int      yydebug;
extern int      yylineno;


/* function prototypes */ 
void    yyerror(const char *);

/* global variables */

%}

/* YYSTYPE */

/* terminals */
/* Start adding token names here */
/* Your token names must match Project 1 */
/* The file cmparser.tab.h was gets generated here */

%token TOK_ERROR
%token TOK_ELSE 
%token TOK_IF
%token TOK_RETURN
%token TOK_VOID
%token TOK_INT
%token TOK_WHILE
%token TOK_PLUS
%token TOK_MINUS
%token TOK_MULT
%token TOK_DIV
%token TOK_LT
%token TOK_LE
%token TOK_GT
%token TOK_GE
%token TOK_EQ
%token TOK_NE
%token TOK_ASSIGN
%token TOK_SEMI
%token TOK_COMMA
%token TOK_LPAREN
%token TOK_RPAREN
%token TOK_LSQ
%token TOK_RSQ
%token TOK_LBRACE
%token TOK_RBRACE
%token TOK_NUM
%token TOK_ID

/* associativity and precedence */
/* specify operator precedence (taken care of by grammar) and associatity here -
-uncomment */

//%left

%left '*' '/'
%left '+' '-'
%nonassoc '<' '>' "<=" ">="
%left "==" "!=" 
%right  '=' 
%nonassoc error

/* Begin your grammar specification here */
%%


Start   : /* put your RHS for this rule here */
   Declarations
    { printf ("Declaration.\n"); }

; /* note that the rule ends with a semicolon */

Declarations    :
/* empty */
{}
| Vardeclaration Declarations
{ printf ("Var-declaration.\n");}
| Fundeclaration Declarations
{ printf ("Fun-declaration.\n");}
;

Vardeclaration  :   
Typespecifier TOK_ID ';'
{ printf ("Not an array.\n");}
| Typespecifier TOK_ID '[' TOK_NUM ']' ';'
{ printf ("An array.\n");}
;

Typespecifier   :
TOK_INT
{ printf ("Type int.\n");}
| TOK_VOID
{ printf("Type void.\n");}
;

Fundeclaration  :
Typespecifier TOK_ID '(' Params ')' Compoundstmt
{ printf ("Function Declaration.");}
;

Params  :
Paramlist
| TOK_VOID
{ printf ("Void param.\n");}
;

Paramlist   :
Paramlist ',' Param
| Param
;

Param   :
Typespecifier TOK_ID
| Typespecifier TOK_ID '[' ']'
;

Compoundstmt    :
'{' Localdeclaration Statement '}'
;

Localdeclaration    :
Vardeclaration Localdeclaration
| /* empty */
;

Statement   :
Expressionstmt Statement
| Compoundstmt Statement
| Selectionstmt Statement
| Iterationstmt Statement
| Returnstmt Statement
| /* empty */
;

Expressionstmt  :
Expression ';'
| ';'
;

Selectionstmt   :
TOK_IF '(' Expression ')' Statement
| TOK_IF '(' Expression ')' Statement TOK_ELSE Statement
;

Iterationstmt   :
TOK_WHILE '(' Expression ')' Statement
;

Returnstmt  :
TOK_RETURN ';'
| TOK_RETURN Expression ';'
;

Expression  :
Var '=' Expression
| SimpleExpression
;

Var :
TOK_ID
| TOK_ID '[' Expression ']'
;

SimpleExpression    :
AdditiveExpression Relop AdditiveExpression
| AdditiveExpression
;

Relop   :
"<="
| '<'
| '>'
| ">="
| "=="
| "!="
;

AdditiveExpression  :
AdditiveExpression Addop Term
| Term
;

Addop   :
'+'
| '-'
;

Term    :
Term Mulop Factor
| Factor
;

Mulop   :
'*'
| '/'
;

Factor  :
'(' Expression ')'
| Var
| Call
| TOK_NUM
;

Call    :
TOK_ID '(' Args ')'
;

Args    :
Arglist
| /* empty */
;

 Arglist    :
Arglist ',' Expression
| Expression
;
%%
void yyerror (char const *s) {
   fprintf (stderr, "Line %d: %s\n", yylineno, s);
}

int main(int argc, char **argv){

initLex(argc,argv);

#ifdef YYLLEXER
while (gettok() !=0) ; //gettok returns 0 on EOF
return;
#else
yyparse();

#endif

} 

您的语法看起来不错,所以我最好的猜测是您的词法分析器无法将字符串void识别为TOK_VOID ,而是返回其他内容。

尝试使用-DYYDEBUG编译解析器并设置yydebug = 1然后再调用yyparse以查看其从词法分析器中获取的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM