首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

linux下的bison报错,

2013-06-25 
linux下的bison报错,求高手指教!急想用flex+bison写个简单的编译器,生成三地址中间代码。结果bison的编译就

linux下的bison报错,求高手指教!急
想用flex+bison写个简单的编译器,生成三地址中间代码。结果bison的编译就一堆警告。。。大概是说定义的文法没有用之类的。

这个是flex文件


%option noyywrap nodefault yylineno

%{
#include "compiler.h"
#include "parsing.tab.h"
%}

%%
"+" |
"-" |
"*" |
"/" |
"=" |
";" |
"(" |
")" |
"{" |
"}"{ return yytext[0];}

">"     { yylval.cmp = 1; return CMP; }
"<"     { yylval.cmp = 2; return CMP; }
"!="    { yylval.cmp = 3; return CMP; }
"=="    { yylval.cmp = 4; return CMP; }
">="    { yylval.cmp = 5; return CMP; }
"<="    { yylval.cmp = 6; return CMP; }

"if"    { return IF; }
"else"  { return ELSE; }
"while" { return WHILE; }
"do"    { return DO; }
"break" { return BREAK;}

"&&"{ return AND;}
"||"{ return OR;}

"int"{ yylval.type=1; return TYPE;}
"float"{ yylval.type=2; return TYPE;}
"bool"{ yylval.type=3; return TYPE;}

[0-9]+{ yylval.i=atoi(yytext); return INTEGER;}
[0-9]+"."[0-9]*{ yylval.d=atof(yytext); return FLOAT;}
"true" |
"false" { yylval.b=strdup(yytext); return BOOLEAN;}

[a-zA-Z][a-zA-Z0-9]*  { yylval.s = lookup(yytext); return ID; }

"//".*
[ \t\n]

.{ yyerror("error input %c\n",*yytext);}
%%




这个是bison文件,所有的警告都在这里


%{
#include <stdio.h>
#include <stdlib.h>
#include "compiler.h"
%}

%union{
struct ast *a;

double d;  
int i;
char * b;

struct symbol *s;

int cmp;
int type;
}


%token <i> INTEGER 
%token <d> FLOAT 
%token <b> BOOLEAN
%token <s> ID
%token EOL

%token IF ELSE WHILE DO BREAK
%token AND OR

%token <type> TYPE

%nonassoc <cmp> CMP
%right '='
%left '+' '-'
%left '*' '/'

%type <a> block 
%type <a> decls decl type
%type <a> stmts stmt loc bool 
%type <a> join equality expr term unary factor

%start calclist

%%

block: '{' decls stmts '}'{ $$=$3;}
;

decls: decls decl
;
decl: type ID ';'
;
type: type '[' INTEGER ']'
| TYPE { $$=newast('X',S1,NULL);}
;

stmts: stmts stmt{ $$=newast('C',$1,$2);}
;
stmt: loc '=' bool ';'{ $$=newast('=',$1,$3);}
| IF '(' bool ')' stmt { $$=newflow('I',$3,$5,NULL);}
| IF '(' bool ')' stmt ELSE stmt{ $$=newflow('I',$3,$5,$7);}


| WHILE '(' bool ')' stmt{ $$=newflow('W',$3,$5,NULL);}
| DO stmt WHILE '(' bool ')' ';'{ $$=newflow('D',$5,$2,NULL);}
| BREAK ';'{ $$=newast('B',NULL,NULL);}
| block 
;

loc: loc '[' bool ']'{ $$=newast('[',$1,$3);}
| ID{ $$=newref($1);}
;
bool: bool OR join{ $$=newast('|',$1,$3);}
| join
;
join: join AND equality{ $$=newast('&',$1,$3);}
| equality
;
equality: equality CMP expr{ $$=newcmp($2,$1,$3);}
| expr
;
expr: expr '+' term{ $$=newast('+',$1,$3);}
| expr '-' term{ $$=newast('-',$1,$3);}
| term
;
term: term '*' unary{ $$=newast('*',$1,$3);}
| term '/' unary{ $$=newast('/',$1,$3);}
| unary
;
unary: '!' unary{ $$=newast('!',$2,NULL);}
| '-' unary{ $$=newast('M',$2,NULL);}
| factor
;
factor: '(' bool ')' { $$=$2;}
| loc
| INTEGER{ $$=newint($1);}
| FLOAT{ $$=newflo($1);}
| BOOLEAN{ $$=newbol($1);}
;

calclist:
| calclist block EOL{ $2;printf("end\n");}
;
%%




警告大概如下:
parsing.y: 警告: 14 nonterminals useless in grammar
parsing.y: 警告: 36 rules useless in grammar
parsing.y:38.11-15: 警告: nonterminal useless in grammar: block
...(省略)
parsing.y:47.8-41: 警告: rule useless in grammar: block: '{' decls stmts '}'
...(省略)

求高手分析上面定义的文法是否不可以用在bison中,还是我哪里写错了。。。 Linux Flex bison
[解决办法]
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。

[解决办法]
警告说的很清楚了,   block: '{' decls stmts '}' 这个语法没有用到,  
 没有终结符

热点排行