- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一项大学作业编写一个使用 flex 和 bison 的解析器。目前,我的目标是读取由整数、字符串及其运算符组成的表达式。整数运行良好——问题出在字符串上。在我运行程序后,当我在控制台中输入一个字符串时,它应该打印回表达式的结果——在本例中它是一个字符串类型,后跟字符串的值。所以如果我输入“hello”,我应该返回“it:String="hello""。问题是,在我最后一次减少 bison 文件时(其中 bison 使用开始变量的规则之一减少到开始变量),字符串值不知何故在它的末尾获得了一个换行符。所以字符串最终变成了“hello\n”,所以 it:String="hello"\n 被打印到控制台。我已经通过解析跟踪确认字符串值在最后一次减少之前是正确的,然后它获得了换行符,我不明白为什么。我认为这个问题将通过一些代码片段变得非常清楚。
这是 lex 文件的重要部分。最后一条规则是我返回 STRING token 的位置。
%{
#include <iostream>
#include <string>
#include <stdlib.h>
#include "y.tab.h"
using namespace std;
void yyerror(char*);
%}
%%
0 { yylval.iVal = atoi(yytext);
return INTEGER;
}
[1-9][0-9]* { yylval.iVal = atoi(yytext);
return INTEGER;
}
[-+()~$^*/;\n] return *yytext;
"==" return EQ;
"!=" return NE;
"&&" return AND;
"||" return OR;
"\""[^"\""]*"\"" { yylval.strVal = yytext;
return STRING; }
这是 yacc 文件。应用规则“program: program strExpr '\n' ”时,我会在此处将响应打印到控制台。
%token EQ NE AND OR STRFIND
%token<iVal> INTEGER
%token<strVal> STRING
%left OR
%left AND
%left EQ NE
%left '+' '-'
%left '*' '/'
%left UNARY
%right '^'
%{
#include <iostream>
#include <cmath>
#include <string>
#define YYDEBUG 1
using namespace std;
void yyerror(char *);
int yylex(void);
%}
%union {
int iVal;
char* strVal;
}
%type<iVal> intExpr
%type<strVal> strExpr
%printer {fprintf(yyoutput, "%s", $$);} strExpr
%%
program:
program intExpr '\n' {cout<<"it:Int="<<$2<<"\n";}
| program strExpr '\n' {cout<<"it:String="<<$2<<"\n";}
| program intExpr ';'
| program strExpr ';'
| program intExpr ';' '\n'
| program strExpr ';' '|n'
| program '\n'
| program ';'
| program ';' '\n'
| ;
expr:
intExpr
| strExpr
intExpr:
INTEGER
| '-' intExpr %prec UNARY { $$ = $2 * (-1); }
| '+' intExpr %prec UNARY { $$ = $2; }
| intExpr '+' intExpr { $$ = $1 + $3; }
| intExpr '*' intExpr { $$ = $1 * $3; }
| intExpr '-' intExpr { $$ = $1 - $3; }
| intExpr '/' intExpr { if ($3 == 0) {
yyerror(0);
return 1;
} else
$$ = $1 / $3; }
| '(' intExpr ')' { $$ = $2; }
| intExpr '^' intExpr { int i;
int val = 1;
for (i = 0; i < $3; i++) {
val = val * $1;
}
$$ = val;
}
| intExpr EQ intExpr { if ($1 == $3)
$$ = 1;
else
$$ = 0;
}
| intExpr NE intExpr { if ($1 != $3)
$$ = 1;
else
$$ = 0;
}
| intExpr AND intExpr { if ($1 != 0 && $3 != 0)
$$ = 1;
else
$$ = 0;
}
| intExpr OR intExpr { if ($1 != 0 || $3 != 0)
$$ = 1;
else
$$ = 0;
}
| ;
strExpr:
STRING
| '(' strExpr ')' { $$ = $2; }
| ;
%%
void yyerror(char *s) {
fprintf(stderr, "error\n");
}
int main(void) {
yydebug = 1;
yyparse();
return 0;
}
这是示例运行的输出:
"hello"
it:String="hello"
1+1
it:Int=2
3+4
it:Int=7
后面那个额外的换行符是什么:String="hello"?
这是解析跟踪,它告诉我在最后一次减少之前添加了换行符,但我不知道为什么?
Starting parse
Entering state 0
Reducing stack by rule 10 (line 45):
-> $$ = nterm program ()
Stack now 0
Entering state 1
Reading a token: "hello"
Next token is token STRING ()
Shifting token STRING ()
Entering state 4
Reducing stack by rule 25 (line 93):
$1 = token STRING ()
-> $$ = nterm strExpr ("hello")
Stack now 0 1
Entering state 11
Reading a token: Next token is token '\n' ()
Shifting token '\n' ()
Entering state 29
Reducing stack by rule 2 (line 37):
$1 = nterm program ()
$2 = nterm strExpr ("hello"
)
$3 = token '\n' ()
it:String="hello"
-> $$ = nterm program ()
Stack now 0
Entering state 1
Reading a token:
我一定会感谢你的帮助。
最佳答案
yylval.strVal = yytext;
yytext
是指向静态缓冲区的指针。每次读取 token 时,缓冲区内容都会发生变化。
yylval.strVal = strdup(yytext);
这将摆脱换行符,但当然会引入内存泄漏。你需要照顾好它。
关于c++ - 为什么我的字符串标记在我的 c++ bison 程序中最终减少时会换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41006907/
在 Vaadin 7.0,显示时JavaBean Table 中的数据与 BeanContainer ,用新数据刷新表的正确方法是什么? 最佳答案 该表通过监听器监视表项的属性。如果您通过表的 Ite
首先,我使用的是带有 Axis2 1.6.2 的 eclipse,我正在 tomcat 6 上部署我创建的 Web 服务。Web 服务是在 eclipse 中通过自上而下的方法创建的。 我被要求使对我
我已将 Rails 3.1.1 应用程序升级到 Rails 3.1.3,现在,对于每个请求,它仅响应错误数量的参数(3 for 1)。不幸的是,它没有说明错误在哪里,并且应用程序跟踪为空。我认为存在一
我是一名优秀的程序员,十分优秀!