- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 bison 和 flex 来解析线性时序逻辑公式并从中生成自动机。我以“默认”方式使用 flex,即如有必要,将 token 写入 yylval 并返回 bison token 标识符。
我正在使用单独的例程来扫描输入文件。输入由标识符名称和整数/实数组成,flex 已经可以处理这两者。示例:
x y
3.20 78.3
3.31 76.2
3.32 77.4
//etc
我的问题是:如果可能的话,我将如何利用flex来扫描输入文件?我知道如何切换 Flex 的输入缓冲区,但之后如何获取 token 值?如果解决方案比单独的例程更复杂,那么我就不会费心去做。
顺便说一句,我正在使用 C,但将来我需要最终将其移植到 C++。
谢谢。
最佳答案
如果问题像您的示例所示那么简单,您可以只使用 fscanf
。 fscanf
的问题可能与使用 Flex 生成的扫描器的问题没有什么不同,即换行符被简单地视为空格。如果您的扫描仪返回换行符的特殊标记(或者可以说服这样做,请参见下文),请继续阅读。
如果我正确理解您想要做什么,您只需正确设置输入,然后重复调用yylex
。如果您使用默认的不可重入 API,那么它可能看起来像这样:
// Returns a count, and sets the out parameter to a
// malloc'd array of malloc'd strings.
// The free function is left as an exercise.
int read_headers(char*** out) {
int count = 0;
char** id_array = NULL;
*out = id_array;
int token;
while ((token = yylex()) == TOKEN_IDENTIFIER) {
id_array = realloc(id_array, (count + 1) * sizeof *id_array);
if (id_array) *out = id_array;
else
// Handle error
id_array[count ++] = strdup(yytext);
}
if (token != '\n')
// Handle error
return count;
}
// Reads exactly n numbers and stores them in the out parameter,
// which must point at an array of size n.
// Returns: n on success. 0 if EOF reached. -1 on error
int read_n_numbers(double* out, int n) {
for (int i = 0; i < n; ++i, ++out) {
int token = yylex();
if (token != TOKEN_NUMBER) {
if (i == 0 && token == 0) return 0;
// Error: non-number or too few numbers
return -1;
}
*out = yylval.number;
}
if (yylex() != '\n') {
// Error: too many numbers
return -1;
}
return 0;
}
您的扫描仪可能实际上并未返回换行符的 \n
。这在表达式语法中很少有用(尽管有时有用)。但修改扫描器以根据需要处理换行符是很容易的;您只需要使用开始条件即可。我们将其设为包含启动条件,因为它只需要处理换行符,但请注意,这意味着所有未标记的规则也都处于事件状态,因此您需要确保处理换行符的未标记规则也只处理单个换行符。
%s RETURN_NEWLINES
%%
<RETURN_NEWLINES>\n { return '\n'; }
\n ; // Ignore a single newline
[ \t]+ ; // Ignore any number of horizontal whitespace
完成后,您只需在扫描之前调用 BEGIN(RETURN_NEWLINES)
即可启用换行符(并且 BEGIN(INITIAL)
返回忽略它们。)您需要在 Flex 定义文件中放置启用和禁用换行扫描的函数,因为所需的宏不会导出。
关于c - 在 yacc/bison 之外使用 lex/flex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668237/
我有一个使用 Yacc 编写的语法。语法的相关部分摘录在这里 postfix : primary | postfix '[' expr ']' | postfix '[' ex
如何在 bison/yacc 中使用继承的属性? 说,我有这样的语法 - 程序 -> stmts 我想做的是在 Bison 身上: 程序:stmts {$$.next = newLabel(); $1
有什么方法可以更改 Bison 错误消息的格式?例如,不要说 syntax error, unexpected T_ID, expected ';' or T_IMPORT说 unexpected i
我的语法有这两个标记声明: %token RP %token ELSE 而这两条规则: Statement : IF LP Exp RP Statement; Statement : IF LP
如果有一个表单的输入文件: BEGIN stuff.... END BEGIN stuff ... END 我的 .y 文件是这样的 %token BEGIN %token END %star
我希望为现有语言创建一个 JavaScript 解析器,该语言目前具有笨拙的“手工制作”C# 和 Java 解析器。我想使用 Jison,并且也一直在尝试学习 Bison 的基础知识。 我不确定如何回
目前我正在研究一个源到源编译器,我已经编写了一个野牛解析器,可以正确地为输入创建 AST。我现在需要对语法树进行多次转换,因此我需要向树中插入许多节点。 我可以手动创建所有要添加到语法树中的结构体/联
如果有一个表单的输入文件: BEGIN stuff.... END BEGIN stuff ... END 我的 .y 文件是这样的 %token BEGIN %token END %star
我正在寻找为现有语言创建 JavaScript 解析器的方法,该语言目前具有笨拙的“手工制作”C# 和 Java 解析器。想用Jison,也在努力学习Bison的基础。 一个我不确定如何回答的问题是
我正在使用 flex/bison 编写解析器(我可以用 Python 编写解析器,但我总是更喜欢经典。) 当我用这个编译代码时: gcc -lfl -ly chance.tab.c lex.yy.c
正在使用 flex/bison 开发编译器。我的构建输出中有此警告。 警告:在默认操作中键入冲突 ('s' '') 请问有什么帮助吗? 最佳答案 它似乎与源中的 %token 和 %type 声明有关
考虑这个 lex.l 文件: %{ #include "y.tab.h" %} digit [0-9] letter [a-zA-Z] %% "+"
我正在为 bison 中的一个简单的 Pascal 编译器编写语法,我想可视化我在 pascal.y 中指定的语法的解析树。是否可以基于我的 pascal.y 文件以图形方式显示语法树? 最佳答案 野
我正在尝试编写一个能够使用以下输入的语法: begin #this is a example x = 56; while x > 0 do begin point 15
我正在尝试使用 BNF 语法编写 Flex/Bison 文件。但是,当我尝试编译时出现错误,而且我不确定如何调试它们。 BNF语法: ::= | head() ::=:: | @ | tail() |
我正在用 Flex/Bison 编写一个小程序来标记/解析我创建的查询语言。 我想知道是否有任何方法可以在 Flex/Bison 中创建任何关键字。 我的意思是:flex 将输入分解成一个标记列表,但
我正在尝试使用 flex 和 bison 创建过滤器,因为我想从复杂的语言中获取某些语法元素。我的计划是使用 flex + bison 来识别语法,并转储出感兴趣元素的位置。 (然后使用脚本根据转储的
我正在尝试实现一个可以进行浮点运算的 Flex/bison 计算器。我的 Flex 代码如下所示 %{ #include "calc.tab.h" #include void yyerror(cha
我正在尝试使用 FLEX 和 BISON 进行一些练习。 这是我写的代码: calc_pol.y %{ #define YYSTYPE double #include "calc_pol.tab.h"
我有一个使用括号和方括号作为分隔符的语法。当由 bison 生成的解析器输入带有不平衡分隔符的输入时,传递给 yyerror 的 YYLTYPE* 中的错误位置是输入的结尾。所以,例如,在输入 x
我是一名优秀的程序员,十分优秀!