Compiler construction 我的yacc文件中有错误
我试图为某种语言创建一个解析器 每当我输入一个示例时,它就会输出“不成功的解析”,我似乎找不到问题所在 这是我的.l文件和.y文件Compiler construction 我的yacc文件中有错误,compiler-construction,yacc,lex,Compiler Construction,Yacc,Lex,我试图为某种语言创建一个解析器 每当我输入一个示例时,它就会输出“不成功的解析”,我似乎找不到问题所在 这是我的.l文件和.y文件 %{ #include <stdio.h> #include "stdlib.h" #include"y.tab.h" FILE *fp,*yyin; extern void yyerror(char *); extern int line = 1; int pos = 1; int com= 0; int errorcomment; %} /* Re
%{
#include <stdio.h>
#include "stdlib.h"
#include"y.tab.h"
FILE *fp,*yyin;
extern void yyerror(char *);
extern int line = 1;
int pos = 1;
int com= 0;
int errorcomment;
%}
/* Regular definitions */
letter [a-zA-Z]
digit [0-9]
blank_str ([\t]|" ")
identifier {letter}({letter}|{digit})*([".""#""$""_"]?({letter}|{digit})+)?
num ([0-9]+)|(([0-9])+"."[0-9]*)([eE]["+""-"]?[0-9]+)?
wrongIdentifier [a-zA-z][a-zA-Z0-9]*["#""$""_""@""^""!""%"]
wrongNumber ([0-9]+)({letter}|["#""$""_""@""^""!""%""+""-" "|"])+
/* Regular definitions */
%option noyywrap
%%
\n {line++; pos = 1;}
{blank_str} {pos +=yyleng;}
[Ii][Ff] {pos +=yyleng; if(com==0)return IF;}
[Ee][Ll][Ss][Ee] {pos +=yyleng; if(com==0)return ELSE;}
[Rr][Ee][Tt][Uu][Rr][Nn] {pos +=yyleng; if(com==0)return RETURN;}
[Ww][Hh][Ii][Ll][Ee] {pos +=yyleng; if(com==0)return WHILE;}
[Vv][Oo][Ii][Dd] {pos +=yyleng; if(com==0)return VOID;}
[Ii][Nn][Tt] {pos +=yyleng; if(com==0)return INT;}
[Ll][Ee][Tt] {pos +=yyleng; if(com==0)return LET;}
{identifier} {pos +=yyleng; if(com==0)return ID;}
{num} {pos +=yyleng; if(com==0)return NUM;}
{wrongIdentifier} {pos +=yyleng; if(com==0)printf("\n**ERROR: Wrong Identifier in Line %d at %d\n\n\n", line, pos-yyleng);}
{wrongNumber} {pos +=yyleng;; if(com==0)printf("\n**ERROR: Wrong Number in Line %d at %d\n\n\n", line, pos-yyleng);}
"+" {pos +=yyleng; if(com==0)return ADDOP;}
"-" {pos +=yyleng; if(com==0)return ADDOP;}
"*" {pos +=yyleng; if(com==0)return MULOP;}
"/" {pos +=yyleng; if(com==0)return MULOP;}
"=" {pos +=yyleng; if(com==0)return AssgOP;}
"<" {pos +=yyleng; if(com==0)return RELOP;}
"<=" {pos +=yyleng; if(com==0)return RELOP;}
">" {pos +=yyleng; if(com==0)return RELOP;}
">=" {pos +=yyleng; if(com==0)return RELOP;}
"==" {pos +=yyleng; if(com==0)return RELOP;}
"!=" {pos +=yyleng; if(com==0)return RELOP;}
"," {pos +=yyleng; if(com==0)return COMMA;}
";" {pos +=yyleng; if(com==0)return Semicolon;}
"(" {pos +=yyleng; if(com==0)return Left_Brace;}
")" {pos +=yyleng; if(com==0)return Right_Brace;}
"[" {pos +=yyleng; if(com==0)return LSquare_Bracket;}
"]" {pos +=yyleng; if(com==0)return RSquare_Bracket;}
"{" {pos +=yyleng; if(com==0)return LWavy_Bracket;}
"}" {pos +=yyleng; if(com==0)return RWavy_Bracket;}
"/*" {pos +=yyleng; errorcomment = line; com=1;}
"*/" {pos +=yyleng; com=0;}
. {pos +=yyleng; printf("\n**ERROR : NOT IN THE ALPHABET in Line %d at %d\n\n\n", line, pos-yyleng);}
%%
%{
#包括
#包括“stdlib.h”
#包括“y.tab.h”
文件*fp,*yyin;
外部无效错误(字符*);
外部-内部线=1;
int pos=1;
int com=0;
国际评论;
%}
/*常规定义*/
字母[a-zA-Z]
数字[0-9]
空白字符([\t]|“”)
标识符{letter}({letter}{digit})*([“\”\“$”\“\”)?({letter}{digit}+)+)?
num([0-9]+)|([0-9])+“[0-9]*)([eE][“+”“-”]?[0-9]+)?
错误标识符[a-zA-z][a-zA-Z0-9]*[“#”“美元”“@”“^”“!”“%”
错误数字([0-9]+)({letter}|[“$”、“@”^”!“%+”-“|”])+
/*常规定义*/
%选项No yywrap
%%
\n{line++;pos=1;}
{blank_str}{pos+=yyleng;}
[Ii][Ff]{pos+=yyleng;if(com==0)返回if;}
[Ee][Ll][Ss][Ee]{pos+=yyleng;if(com==0)返回ELSE;}
[Rr][Ee][Tt][Uu][Rr][Nn]{pos+=yyleng;如果(com==0)返回;}
[Ww][Hh][Ii][Ll][Ee]{pos+=yyleng;if(com==0)返回WHILE;}
[Vv][Oo][Ii][Dd]{pos+=yyleng;如果(com==0)返回VOID;}
[Ii][Nn][Tt]{pos+=yyleng;如果(com==0)返回INT;}
[Ll][Ee][Tt]{pos+=yyleng;如果(com==0)返回LET;}
{identifier}{pos+=yyleng;if(com==0)返回ID;}
{num}{pos+=yyleng;如果(com==0)返回num;}
{ErrorIdentifier}{pos+=yyleng;if(com==0)printf(“\n**错误:第%d行%d处的标识符错误,\n\n\n”,第%d行,pos yyleng);}
{errownumber}{pos+=yyleng;;如果(com==0)printf(“\n**错误:第%d行第%d行第%d行第%d行第%d行第%n行第%n行第%n行错误);}
“+”{pos+=yyleng;if(com==0)返回ADDOP;}
“-”{pos+=yyleng;if(com==0)返回ADDOP;}
“*”{pos+=yyleng;if(com==0)返回MULOP;}
“/”{pos+=yyleng;if(com==0)返回MULOP;}
“=”{pos+=yyleng;如果(com==0)返回AssgOP;}
“=”{pos+=yyleng;如果(com==0)返回RELOP;}
“==”{pos+=yyleng;如果(com==0)返回RELOP;}
“!=”{pos+=yyleng;如果(com==0)返回RELOP;}
“,”{pos+=yyleng;if(com==0)返回逗号;}
“;”{pos+=yyleng;if(com==0)返回分号;}
(“{pos+=yyleng;if(com==0)返回左括号;}
“{pos+=yyleng;if(com==0)返回右括号;}”
“[”{pos+=yyleng;if(com==0)返回LSquare_括号;}
“]”{pos+=yyleng;if(com==0)返回RSquare_括号;}
“{pos+=yyleng;if(com==0)返回LWavy_括号;}
“}”{pos+=yyleng;如果(com==0)返回RWavy_括号;}
“/*”{pos+=yyleng;errorcomment=line;com=1;}
“*/”{pos+=yyleng;com=0;}
. {pos+=yyleng;printf(“\n**错误:不在第%d行%d处的字母表中\n\n\n”,第行,pos yyleng);}
%%
这是一个.y文件
%{
#include <stdio.h> /* C declarations used in actions */
#include <stdlib.h>
#include <ctype.h>
extern int line;
extern FILE *yyin;
extern int yylex(void);
int yywrap()
{
return 1;
}
main()
{
yyparse();
if (yyparse()==0)
printf("Successful Parse.\n");
else
printf("Unsuccessful Parse.\n");
//return 0;
}
extern int yyerror (char * s) // allows for printing of an error msg
{
fprintf(stderr, "Error at line %d : %s\n",line, s);
return 0;
}
%}
%token ID
%token NUM
%token LET
%token IF
%token ELSE
%token INT
%token RETURN
%token VOID
%token WHILE
%token ADDOP
%token MULOP
%token RELOP
%token AssgOP
%token Semicolon
%token COMMA
%token Left_Brace
%token Right_Brace
%token LSquare_Bracket
%token RSquare_Bracket
%token LWavy_Bracket
%token RWavy_Bracket
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%%
program: declaration_list ;
declaration_list: declaration_list declaration
| declaration
;
declaration: var_declaration
| fun_declaration
;
var_declaration: type_specifier ID Semicolon
| type_specifier ID LSquare_Bracket NUM RSquare_Bracket Semicolon
;
type_specifier: INT
| VOID
;
fun_declaration: type_specifier ID Left_Brace params Right_Brace compound_stmt ;
params: param_list
| VOID
;
param_list: param_list COMMA param
| param
;
param: type_specifier ID
| type_specifier ID LSquare_Bracket RSquare_Bracket
;
compound_stmt: LWavy_Bracket local_declararions statement_list RWavy_Bracket ;
local_declararions: local_declararions var_declaration
|
;
statement_list: statement_list statement
|
;
statement: expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt
;
expression_stmt: expression Semicolon
| Semicolon
;
selection_stmt: IF Left_Brace expression_stmt Right_Brace statement %prec LOWER_THAN_ELSE
| IF Left_Brace expression_stmt Right_Brace statement ELSE statement
;
iteration_stmt: WHILE Left_Brace expression_stmt Right_Brace statement ;
return_stmt: RETURN Semicolon
| RETURN expression Semicolon
;
expression: LET var AssgOP expression
| simple_expression
;
var: ID
| ID LSquare_Bracket expression RSquare_Bracket
;
simple_expression: additive_expression RELOP additive_expression
| additive_expression
;
additive_expression: additive_expression ADDOP term
| term
;
term: term MULOP factor
| factor
;
factor: Left_Brace expression Right_Brace
| var
| call
| NUM
;
call: ID Left_Brace args Right_Brace ;
args: arg_list
|
;
arg_list: arg_list COMMA expression
| expression
;
%{
#包括在操作中使用的/*C声明*/
#包括
#包括
外-内线;
外部文件*yyin;
外部内部yylex(无效);
int yywrap()
{
返回1;
}
main()
{
yyparse();
if(yyparse()==0)
printf(“成功解析。\n”);
其他的
printf(“解析不成功。\n”);
//返回0;
}
extern int yyerror(char*s)//允许打印错误消息
{
fprintf(标准,“第%d行错误:%s\n”,第,s行);
返回0;
}
%}
%令牌ID
%令牌数
%代币出租
%代币如果
%其他代币
%标记整数
%代币返还
%代币无效
%代币
%令牌地址
%代币MULOP
%令牌重发
%代币助理
%标记分号
%标记逗号
%标记左括号
%令牌右括号
%令牌方括号
%令牌方括号
%标记LWavy_括号
%标记RWavy_括号
%非ASSOC低于其他
%非ASSOC ELSE
%%
程序:申报单;
声明列表:声明列表声明
|声明
;
声明:var_声明
|《欢乐宣言》
;
变量声明:类型说明符ID分号
|类型说明符ID方括号数方括号分号
;
类型_说明符:INT
|空虚
;
乐趣声明:类型说明符ID左括号参数右括号复合字符串;
参数:参数列表
|空虚
;
参数列表:参数列表逗号参数
|param
;
param:type_说明符ID
|类型说明符ID方括号方括号
;
复合结构:左括号本地声明列表右括号;
局部声明:局部声明变量声明
|
;
语句列表:语句列表语句
|
;
语句:表达式
|复合材料
|选择
|迭代法
|返回时间
;
表达式名称:表达式分号
|分号
;
选择:如果左括号表达式右括号语句%prec LOWER\u比其他语句
|IF左括号表达式stmt右括号语句ELSE语句
;
迭代:左括号表达式右括号语句;
return\u stmt:返回分号
|返回表达式分号
;
表达式:让var AssgOP表达式
|简单表达式
;
变量:ID
|ID LSquare_括号表达式RSquare_括号
;
简单表达式:加法表达式重新加载加法表达式
|加法表达式
;
加法表达式:加法表达式ADDOP term
|术语
;
术语:术语多重因子
|因素
;
因子:左括号表达式右括号
|变量
|召唤
|NUM
;
调用:ID左括号args右括号;
args:arg_列表
|
;
arg\u列表:arg\u列表逗号表达式
|表情
;
任何帮助都会很感激的
我输入一个简单的代码,比如intx[10];而且它仍然不起作用这个问题已经好几个月没有答案了,因为它的构造很差,让任何人都努力去回答它。你应该阅读,寻找有效提问的方法。特别是,没有人试图简化这个问题。简化问题
extern void yyerror(char *);
extern int yyerror (char * s) // allows for printing of an error msg
-t Change the preprocessor directives generated by yacc so that
debugging statements will be incorporated in the compiled code.
main()
{ extern int yydebug; yydebug = 1 ; /* Enable debugging */
yyparse();
if (yyparse()==0)
Reading a token: Now at end of input.
Reducing stack by rule 1 (line 67):
$1 = nterm declaration_list ()
-> $$ = nterm program ()
Stack now 0
Entering state 3
Now at end of input.
Shifting token $end ()
Entering state 9
Stack now 0 3 9
Cleanup: popping token $end ()
Cleanup: popping nterm program ()
Starting parse <----------- **See here** ----
Entering state 0
Reading a token: Now at end of input.
Error at line 2 : syntax error
Cleanup: discarding lookahead token $end ()
Stack now 0
Unsuccessful Parse.
main()
{
/* yyparse(); - removed */
if (yyparse()==0)