Syntax error yacc程序中的奇怪语法错误
我不知道为什么我的程序不起作用。 在解析程序中没有“a=5;”时,它工作正常(下面是一段代码) 和lex文件:Syntax error yacc程序中的奇怪语法错误,syntax-error,yacc,lex,Syntax Error,Yacc,Lex,我不知道为什么我的程序不起作用。 在解析程序中没有“a=5;”时,它工作正常(下面是一段代码) 和lex文件: %{ #include <stdlib.h> #include <stdio.h> #include "translator.h" %} %option noyywrap %% (public) { return (PUBLIC); } (class) { return (CLASS); } (static) { return (STA
%{
#include <stdlib.h>
#include <stdio.h>
#include "translator.h"
%}
%option noyywrap
%%
(public) { return (PUBLIC); }
(class) { return (CLASS); }
(static) { return (STATIC); }
(void) { return (VOID); }
(main) { return (MAIN); }
(String) { return (STRING); }
(int) { return (INT); }
(println) { return (PRINTLN); }
(\{) { return '{'; }
(\}) { return '}'; }
(\() { return '('; }
(\)) { return ')'; }
(\[) { return '['; }
(\]) { return ']'; }
(\;) { return ';'; }
(\=) { return '='; }
"==" { return (EQ); }
(\n) { return (ENDLINE); }
[a-zA-Z_\$][a-zA-Z0-9_\$]* {
printf("found %s\n", yytext);
strcpy(yylval.name, yytext);
return ID;
}
([0])|([1-9][0-9]*) {
printf("found %s\n", yytext);
yylval.int_val = atoi(yytext);
return NUM;
}
" " { /*space - do nothing*/ }
. { /*do nothing*/ }
%%
%{
#包括
#包括
#包括“translator.h”
%}
%选项No yywrap
%%
(公共){返回(公共);}
(类){返回(类);}
(静态){返回(静态);}
(无效){返回(无效);}
(main){返回(main);}
(字符串){返回(字符串);}
(int){返回(int);}
(println){return(println);}
(\{){返回'{';}
(\}){return'}';}
(\(){return'(';}
(\){return')';}
(\[){返回'[';}
(\]){return']';}
(\;){return';';}
(\=){return'=';}
“=”{return(EQ);}
(\n){return(ENDLINE);}
[a-zA-Z\$][a-zA-Z0-9\$]{
printf(“找到%s\n”,yytext);
strcpy(yylval.name,yytext);
返回ID;
}
([0])|([1-9][0-9]*) {
printf(“找到%s\n”,yytext);
yylval.int_val=atoi(yytext);
返回NUM;
}
“”{/*空格-不执行任何操作*/}
. {/*什么也不做*/}
%%
我希望有人能在我的代码中找到bug。以下是您对
方法体的定义:
method_body:
local_declarations
|
statements
;
这指定方法体
包含本地声明
或语句
,但不能同时包含这两个声明。您需要将其更改为接受local\u声明
,后跟语句
运行时会发生什么?输出有什么问题?你想得到什么?当我运行它时,所有的工作都正常,直到达到这一行“a=5;”。我认为我的程序可以“理解”a,但不能理解“=”。它是三个地址码转换器。例如,^在类stmtasign的var_ref I create对象中,该类的构造函数中有一个输出“let$1$3”。
%{
#include <stdlib.h>
#include <stdio.h>
#include "translator.h"
%}
%option noyywrap
%%
(public) { return (PUBLIC); }
(class) { return (CLASS); }
(static) { return (STATIC); }
(void) { return (VOID); }
(main) { return (MAIN); }
(String) { return (STRING); }
(int) { return (INT); }
(println) { return (PRINTLN); }
(\{) { return '{'; }
(\}) { return '}'; }
(\() { return '('; }
(\)) { return ')'; }
(\[) { return '['; }
(\]) { return ']'; }
(\;) { return ';'; }
(\=) { return '='; }
"==" { return (EQ); }
(\n) { return (ENDLINE); }
[a-zA-Z_\$][a-zA-Z0-9_\$]* {
printf("found %s\n", yytext);
strcpy(yylval.name, yytext);
return ID;
}
([0])|([1-9][0-9]*) {
printf("found %s\n", yytext);
yylval.int_val = atoi(yytext);
return NUM;
}
" " { /*space - do nothing*/ }
. { /*do nothing*/ }
%%
method_body:
local_declarations
|
statements
;