我正在尝试从 .txt 文件中解析一组算术表达式。(每行一个。)我能够为第一行获得正确的逻辑,但解析器为第二个表达式给出 0。此外,我想在输出中打印整个字符串,但从哪里开始感到困惑。
莱克斯
%{
#include <stdio.h>
#include "y.tab.h"
int yylval; /*declared extern by yacc code. used to pass info to yacc*/
%}
letter [A-Za-z]
digit ([0-9])*
op "+"|"*"|"("|")"|"/"|"-"
ws [ \t\n\r]+$
other .
%%
{ws} { /*Nothing*/ }
{digit} { yylval = atoi(yytext); return NUM;}
{op} { return yytext[0];}
{other} { printf("bad %c bad %d \n",*yytext,*yytext); return '?'; }
%%
雅克
%{
#include <stdio.h>
#include <string.h>
#define YYSTYPE int /* the attribute type for Yacc's stack */
extern int yylval; /* defined by lex, holds attrib of cur token */
extern char yytext[]; /* defined by lex and holds most recent token */
extern FILE * yyin; /* defined by lex; lex reads from this file */
%}
%token NUM
%%
Calc : Expr {printf(" = %d\n",$1);}
| Calc Expr {printf(" = %d\n",$1);}
;
Expr : Expr '+' Expr { $$ = $1 + $3; }
| Expr '-' Expr { $$ = $1 - $3; }
| Expr '*' Expr { $$ = $1 * $3; }
| Expr '/' Expr { if($3==0)
yyerror("Divide by Zero Encountered.");
else
$$ = $1 / $3;
}
| '-' Expr { $$ = -$2; }
| Fact { $$=$1; }
;
Fact : '(' Expr ')' { $$ = $2; }
| Id { $$ = $1; }
;
Id : NUM { $$ = yylval; }
;
%%
void yyerror(char *mesg); /* this one is required by YACC */
main(int argc, char* *argv){
char ch,c;
FILE *f;
if(argc != 2) {printf("useage: calc filename \n"); exit(1);}
if( !(yyin = fopen(argv[1],"r")) ){
printf("cannot open file\n");exit(1);
}
yyparse();
}
void yyerror(char *mesg){
printf("Bad Expression : %s\n", mesg);
}
文本文件
4+3-2*(-7)
65*+/abc
9/3-2*(-5)
输出
=21
Bad Expression : syntax error
预期产出
4+3-2*(-7)=21
65*+/abc=Bad Expression : syntax error
9/3-2*(-5)=13
即使我从文本文件的第二行中删除了错误的表达式,解析器也会给出结果
=21
=0
代替
=21
=13
我尝试读取数据并将其存储在变量中并使用 while 循环中的文件处理选项显示算术表达式,并在循环中使用 yyparse() 逐行扫描。由于源代码有点复杂,我无法找到问题所在,而且我从 20 天就开始研究这个东西。
使用命令运行代码
yacc -v -t -d calc.yacc (I am getting 22 shift/reduce conflicts.)
lex calc.lex
gcc y.tab.c lex.yy.c -lm -ll -o calc
./calc calc.txt