Syntax YACC和LEX,在行尾出现语法错误,can';我不明白为什么
这是我的YACCSyntax YACC和LEX,在行尾出现语法错误,can';我不明白为什么,syntax,compiler-errors,bison,yacc,lex,Syntax,Compiler Errors,Bison,Yacc,Lex,这是我的YACC %{ #include <stdio.h> #include <ctype.h> #include "lex.yy.c" void yyerror (s) /* Called by yyparse on error */ char *s; { printf ("%s\n", s); } %} %start program %union{ int value; char * string; } %token &
%{
#include <stdio.h>
#include <ctype.h>
#include "lex.yy.c"
void yyerror (s) /* Called by yyparse on error */
char *s;
{
printf ("%s\n", s);
}
%}
%start program
%union{
int value;
char * string;
}
%token <value> NUM INT VOID WHILE IF THEN ELSE READ WRITE RETURN LE GE EQ NE
%token <string> ID
%token <value> INTEGER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%%
program : decllist
{fprintf(stderr, "program");}
;
decllist : dec
{fprintf(stderr, "\n dec");}
| dec decllist
;
dec : vardec
{fprintf(stderr, "vardec");}
| fundec
{fprintf(stderr, "YEAH");}
;
typespec : INT
| VOID
;
vardec : typespec ID ';'
{fprintf(stderr, "yep");}
| typespec ID '[' NUM ']' ';'
{fprintf(stderr, "again");}
;
fundec : typespec ID '(' params ')' compoundstmt
;
params : VOID
| paramlist
;
paramlist : param
| param ',' paramlist
;
param : typespec ID
| typespec ID '['']'
;
compoundstmt : '{' localdeclerations statementlist '}'
;
localdeclerations :/* empty */
|vardec localdeclerations
;
statementlist : /* empty */
| statement statementlist
;
statement : expressionstmt
| compoundstmt
| selectionstmt
| iterationstmt
| assignmentstmt
| returnstmt
| readstmt
| writestmt
;
expressionstmt : expression ';'
| ';'
;
assignmentstmt : var '=' expressionstmt
;
selectionstmt : IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement
;
iterationstmt : WHILE '(' expression ')' statement
;
returnstmt : RETURN ';'
| RETURN expression ';'
;
writestmt : WRITE expression ';'
;
readstmt : READ var ';'
;
expression : simpleexpression
;
var : ID
| ID '[' expression ']'
;
simpleexpression : additiveexpression
| additiveexpression relop simpleexpression
;
relop : LE
| '<'
| '>'
| GE
| EQ
| NE
;
additiveexpression : term
| term addop term
;
addop : '+'
| '-'
;
term : factor
| term multop factor
;
multop : '*'
| '/'
;
factor : '(' expression ')'
| NUM
| var
| call
;
call : ID '(' args ')'
;
args : arglist
| /* empty */
;
arglist : expression
| expression ',' arglist
;
%%
main(){
yyparse();
}
%{
#包括
#包括
#包括“lex.yy.c”
无效yyerror/*由yyparse在发生错误时调用*/
char*s;
{
printf(“%s\n”,s);
}
%}
%启动程序
%联合{
int值;
字符*字符串;
}
%token NUM INT VOID WHILE IF THEN ELSE READ WRITE RETURN LE GE EQ NE
%令牌ID
%令牌整数
%左“|”
%左“&”
%左'+''-'
%左'*''/''%
%左耳门
%%
节目:decllist
{fprintf(stderr,“程序”);}
;
decllist:dec
{fprintf(stderr,“\n dec”);}
|dec decllist
;
十二月:瓦尔德克
{fprintf(stderr,“vardec”);}
|芬代克
{fprintf(stderr,“耶”);}
;
类型规格:INT
|空虚
;
vardec:typespec ID';'
{fprintf(stderr,“yes”);}
|typespec ID“['NUM']”;'
{fprintf(stderr,“再次”);}
;
fundec:typespec ID'('params')'compoundstmt
;
参数:无效
|参数列表
;
参数列表:param
|参数列表
;
参数:类型规范ID
|类型规范ID“['']”
;
compoundstmt:“{”LocalDecellerations语句列表“}”
;
本地偏差:/*空*/
|vardec局部偏差
;
语句列表:/*空*/
|语句列表
;
声明:expressionstmt
|复合物
|选择TMT
|迭代TMT
|转让STMT
|返回stmt
|readstmt
|书面语
;
表达式TMT:表达式“;”
| ';'
;
assignmentstmt:var'='表达式stmt
;
selectionstmt:IF'('expression')'语句
|IF'('expression')语句ELSE语句
;
iterationstmt:WHILE'('expression')'语句
;
returnstmt:RETURN';'
|返回表达式“;”
;
writestmt:写表达式“;”
;
readstmt:读取变量“;”
;
表达式:simpleexpression
;
变量:ID
|ID“[”表达式“]”
;
simpleexpression:additiveexpression
|additiveexpression relop simpleexpression
;
重播:乐
| ''
|通用电气
|情商
|东北
;
加法表达式:术语
|术语添加术语
;
addop:“+”
| '-'
;
术语:因子
|术语多顶因子
;
multop:“*”
| '/'
;
因子:“(“表达式”)”
|NUM
|变量
|召唤
;
调用:ID'('args')'
;
args:arglist
|/*空*/
;
arglist:表达式
|表达式“,”arglist
;
%%
main(){
yyparse();
}
这是我的莱克斯
%{
int mydebug=1;
int lineno=0;
#include "y.tab.h"
%}
%%
int {if (mydebug) fprintf(stderr, "int found\n");
return(INT);
}
num {if (mydebug) fprintf(stderr, "num found\n");
return(NUM);
}
void {if (mydebug) fprintf(stderr, "void found \n");
return(VOID);
}
while {if (mydebug) fprintf(stderr, "while found \n");
return(WHILE);
}
if {if (mydebug) fprintf(stderr, "if found \n");
return(IF);
}
then {if (mydebug) fprintf(stderr, "then found \n");
return(THEN);
}
else {if (mydebug) fprintf(stderr, "else found \n");
return(ELSE);
}
read {if (mydebug) fprintf(stderr, "read found \n");
return(READ);
}
write {if (mydebug) fprintf(stderr, "void found \n");
return(WRITE);
}
return {if (mydebug) fprintf(stderr, "void found \n");
return(RETURN);
}
'<=' {if (mydebug) fprintf(stderr, "void found \n");
return(LE);
}
'>=' {if (mydebug) fprintf(stderr, "void found \n");
return(GE);
}
'==' {if (mydebug) fprintf(stderr, "void found \n");
return(EQ);
}
'!=' {if (mydebug) fprintf(stderr, "void found \n");
return(NE);
}
[a-zA-Z][a-zA-Z0-9]* {if (mydebug) fprintf(stderr,"Letter found\n");
yylval.string=strdup(yytext); return(ID);}
[0-9][0-9]* {if (mydebug) fprintf(stderr,"Digit found\n");
yylval.value=atoi((const char *)yytext); return(NUM);}
[ \t] {if (mydebug) fprintf(stderr,"Whitespace found\n");}
[=\-+*/%&|()\[\]<>;] { if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
return (*yytext);}
\n { if (mydebug) fprintf(stderr,"cariage return %c\n",*yytext);
lineno++;
return (*yytext);}
%%
int yywrap(void)
{ return 1;}
%{
int-mydebug=1;
int lineno=0;
#包括“y.tab.h”
%}
%%
int{if(mydebug)fprintf(stderr,“int-find\n”);
返回(INT);
}
num{if(mydebug)fprintf(stderr,“num found\n”);
返回(NUM);
}
void{if(mydebug)fprintf(stderr,“void found\n”);
退货(作废);
}
while{if(mydebug)fprintf(stderr,“whilefind\n”);
返回(同时);
}
if{if(mydebug)fprintf(stderr,“if-find\n”);
返回(如果有);
}
然后{if(mydebug)fprintf(stderr,“then found\n”);
返回(然后);
}
else{if(mydebug)fprintf(stderr,“else-found\n”);
返回(其他);
}
读取{if(mydebug)fprintf(stderr,“read-find\n”);
返回(读取);
}
写入{if(mydebug)fprintf(stderr,“void found\n”);
返回(写入);
}
返回{if(mydebug)fprintf(stderr,“void found\n”);
返回(返回);
}
'='{if(mydebug)fprintf(stderr,“void found\n”);
返回(GE);
}
'='{if(mydebug)fprintf(stderr,“void found\n”);
返回(EQ);
}
'!=' {if(mydebug)fprintf(stderr,“void found\n”);
返回(NE);
}
[a-zA-Z][a-zA-Z0-9]*{if(mydebug)fprintf(stderr,“找到的字母”);
yylval.string=strdup(yytext);return(ID);}
[0-9][0-9]*{if(mydebug)fprintf(stderr,“找到的数字\n”);
value=atoi((const char*)yytext);返回(NUM);}
[\t]{if(mydebug)fprintf(stderr,“找到空白\n”);}
[=\-+*/%&(\[\];]{if(mydebug)fprintf(stderr,“返回令牌%c\n”,*yytext);
返回(*yytext);}
\n{if(mydebug)fprintf(stderr,“cariage返回%c\n”,*yytext);
lineno++;
返回(*yytext);}
%%
int-yywrap(无效)
{返回1;}
如果我输入类似“int a;”的内容它一直到新行并打印“回车”,但随后停止,并在末尾吐出语法错误。有人知道为什么吗
我已经看了很多遍了,似乎找不到什么能阻止它。我有一个以前的计划,我要回去看看我是否能弄明白,但我被难住了。有人能帮忙吗?你的lexer在行尾返回
'\n'
(换行符)标记,但是你的解析器从不接受它们,所以当解析器点击第一个换行符时,你会得到语法错误。你的语法中到底期望lexer发送的\n
在哪里,为(f)lex提供-d
命令行选项要比将printf分散到所有词法操作上少得多。