- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图显示文本文件中的整个算术表达式及其结果,我尝试使用文件处理选项,但它不起作用。
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/
如何使用 yyparse() 编译、链接和调用不同的 YACC 语法在一个程序中? 最佳答案 对每个单独的 yacc 语法生成使用 -p 选项 -p prefix Use pr
我正在为类项目工作,我们必须在其中构建解析器。我们目前正处于在 yacc 中构建解析器的阶段。目前让我感到困惑的是,我读到您需要为每个非终结符分配一个类型。在某些情况下,虽然我会有类似的东西: ...
我有一个关于 yacc 编译器的问题。我不编译简单的 yacc 语法。这是代码部分: /*anbn_0.y */ %token A B %% start: anbn '\n' {printf(" is
我正在使用 Yacc 和 lex 来解析 C 类型语言, 我已经使用 C++ 构建了数据结构。 一切正常,但我无法在 main.cpp 中使用 yyin 读取输入文件。 以下是代码: 请帮忙 ! #i
每当找到一些正则表达式时,我都会使用 lex 来执行一些代码, Yacc 还能做更多的事情吗?如果是,那是什么? 最佳答案 lex 和 yacc 通常一起使用。这就是您通常使用以下两种方式构建应用程序
我是 lex 和 yacc 以及编译器设计的新手。我想知道符号表是在哪个阶段(词汇、句法或任何其他阶段)以及如何生成的? 我可以简单描述一下 y.output 文件,该文件是通过向 yacc 提供 -
我正在 yacc 中处理一个项目并且遇到了 shift/reduce 错误,但我不知道为什么我会得到它。我一直在查看 y.output 文件,但不太确定如何阅读它。我的 y.output 文件超出了
我是 yacc/lex 的新手,我正在开发一个由其他人编写的解析器。我注意到,当找到未定义的标记时,解析器返回错误并停止。有没有一种简单的方法可以让它完全忽略它无法解析的行并继续进行下一个? 最佳答案
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
嗨,我正在学习 Lex 和 yacc。我创建了以下 lex 程序。 %{ #include %} %% [0123456789]+ printf("NUMBER\n"); [a-
莱克斯部分: %% [0-9]+ { yyval = atoi (yytext); return num; } %% 雅克部分: %token num %% exp:num '+' num ; {$$
问题 我正在尝试使用 Python Lex-Yacc (PLY) 实现容错解析器,但在输入字符串末尾使用错误恢复规则时遇到问题。 如何从意外的输入结束中恢复? 示例 此示例语法生成以下形式的字符串 A
我有两个带有函数 xxlex() 和 yylex() 的 lex 文件,还有一个 yacc语法文件。我希望语法规则始终调用 xxlex()。但是 xlex() 只被调用一次,我希望它总是被调用。我该怎
我正在尝试使用 PLY (python implementation of yacc) 编写一个相当简单的语法,并且在我想要的时候让 yacc 减少标记字符串时遇到困难。 我想解释一系列采用不同类型参
notype_declarator: notype_declarator '(' parmlist_or_identifiers %prec '.' { $$ = bui
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭12 年前。 Improve th
拜托,我面临一个简单的问题..这就是问题所在,在我的 lex 文件中,我有类似的内容: char *ptr_String; "name = " { BEGIN sName; } .+ {
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
嗨,我正在尝试运行 R. Levine 所著的 lex 和 yacc 书中的 John 代码,我已经使用命令在 Linux 中编译了 lex 和 yacc 程序 lex example.l yacc
我想传递 token 的实际字符串。如果我有一个名为ID的 token ,那么我希望我的yacc文件实际知道称为什么ID。我必须将使用yylval的字符串从flex文件传递到yacc文件。我怎么做?
我是一名优秀的程序员,十分优秀!