yacc - 我在这个问题上遇到错误,有人可以提出他们的答案吗?
问题描述
问题是:- YACC 中用于评估表达式的程序(用于加减法、乘法、除法的简单计算器程序)。我也做过 mod (%)。这些是我的 yacc 和 lex 部分:-
LEX部分:-
%{
/* Definition section */
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC部分:-
%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
/* Rule Section */
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression which
can have operations Addition,
Subtraction, Multiplication, Division,
Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
现在,当我在终端上执行我的代码时,我使用这些代码 -
lex calc.l
yacc calc.y
gcc lex.yy.c y.tab.c -w
但是当我输入第三个代码时,会发生一个错误,即“NUMBER”未声明,每个未声明的标识符对于它出现的每个函数只报告一次。
解决方案
推荐阅读
- visual-studio - 从 Azure 下载后安装 Visual Studio 项目
- three.js - Three.js:纹理加载不正确
- flutter - 如何在颤振中使用 rxdart 实现 bloc?
- python - 在 Python 中命名布尔值时有哪些规定?
- regex - 正则表达式查找没有双“L”的单词
- python - 如何在python中使用startswith将大txt文件拆分成小txt文件?
- botframework - How to Start a Bot with the Microphone On on Botframework V4 and Webchat
- java - 获取离用户最近的医院名称,而无需使用实际的 Google 地图,只需使用地点 api
- python-3.x - GEKKO 中优化问题的并行化
- java - 当所有映射都返回 ResponseEntity 时,如何测试 @RestController?