【发布时间】:2013-05-08 18:45:39
【问题描述】:
我想为简单的计算器制作解析器,但我无法理解我在简单输入时遇到错误。 Flex 文件是这样的
%{
#include "exp.tab.h"
#include <string.h>
%}
blanks [ \t\n]+
%%
{blanks} { /* ignore */ }
[0-9]+ {yylval= strtol(yytext, NULL, 10);
return(NUMB);}
%%
Bison 文件如下所示:
%{
#include <stdio.h>
%}
%token NUMB
%left '+'
%%
exp:
NUMB { $$ = $1; }
| exp '+' exp { $$ = $1 + $3; }
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(void) {
yyparse();
}
输入
123 + 12
我收到错误消息。为什么会这样?
【问题讨论】:
-
标记时要注意。 Flex 用于 Adobe/Apache 框架。 Gnu-flex 用于词法分析器。
-
@Reboog711,
flex-lexer实际上是... -
@Charles 谢谢;我不知道。我刚刚修改了“Flex Tag Wiki”以提及 flex-lexer 标签:stackoverflow.com/tags/flex/info
标签: bison flex-lexer