gpt4 book ai didi

c - 如何在 YACC 语法中回显输入文本?

转载 作者:行者123 更新时间:2023-11-30 16:50:21 33 4
gpt4 key购买 nike

我试图显示文本文件中的整个算术表达式及其结果,我尝试使用文件处理选项,但它不起作用。

YACC:

%{
#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",$2);}
| Calc error {yyerror("\n");}
;
Expr : Term { $$ = $1; }
| Expr '+' Term { $$ = $1 + $3; }
| Expr '-' Term { $$ = $1 - $3; }
;
Term : Fact { $$ = $1; }
| Term '*' Fact { $$ = $1 * $3; }
| Term '/' Fact { if($3==0){
yyerror("Divide by Zero Encountered.");
break;}
else
$$ = $1 / $3;
}
;
Fact : Prim { $$ = $1; }
| '-' Prim { $$ = -$2; }
;
Prim : '(' 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);
}
/*
f=fopen(argv[1],"r");
if(f!=NULL){
char line[1000];
while(fgets(line,sizeof(line),f)!=NULL)
{
fprintf(stdout,"%s",line);
yyparse();
}

}
*/
yyparse();
}

void yyerror(char *mesg){
printf("\n%s", mesg);
}

LEX

%{
#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%cbad%d\n",*yytext,*yytext); return '?'; }

%%

我的文本文件包含这两个表达式:

4+3-2*(-7)
9/3-2*(-5)

我想要输出为:

4+3-2*(-7)=21
9/3-2*(-5)=13

但是输出是:

=21
=13

因为解析器会立即执行所有计算,所以这个(注释代码)不合法使用。因此需要将输入表达式传递给语法并在 Calc block 中打印。我无法在谷歌上找到任何有关显示语法输入的相关内容。提前感谢您的评论和建议。

最佳答案

你不想在语法中这样做。太复杂了,而且太容易受到语法可能进行的任何重新排列的影响。您可以考虑在词法分析器中执行此操作,即 print yytext在除空白操作之外的每个操作中,就在返回之前,但我会通过覆盖 lex(1) 的输入函数来回显读取的所有输入。

注意,您应该使用 flex(1),而不是 lex(1),并请注意,如果您进行更改,yyyext不再是char[]并成为char * .

我在您之前的问题中没有提到这一点,但这条规则:

{other} {  printf("bad%cbad%d\n",*yytext,*yytext); return  '?'; }

最好写成:

{other} {  return yytext[0]; }

这样解析器就会看到它并产生语法错误,因此您不必自己打印任何内容。此技术还可以让您摆脱单个特殊字符 +,-=*,/,(,) 的规则。 ,因为解析器将通过 yytext[0] 识别它们.

关于c - 如何在 YACC 语法中回显输入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216461/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com