【发布时间】:2012-02-14 18:02:13
【问题描述】:
我正在尝试为我的嵌入式环境编译一个我在互联网上找到的简单计算器示例,但我在与 flex/bison 的依赖关系方面遇到了一些困难。
我的测试文件如下:
lexer.l
%{
// lexer.l From tcalc: a simple calculator program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "tcalc.tab.h"
extern YYSTYPE yylval;
%}
%option noyywrap
%option never-interactive
%option nounistd
delim [ \t]
whitesp {delim}+
digit [0-9]
number [-]?{digit}*[.]?{digit}+
%%
{number} { sscanf(yytext, "%lf", &yylval); return NUMBER;}
"+" { return PLUS; }
"-" { return MINUS; }
"/" { return SLASH; }
"*" { return ASTERISK; }
"(" { return LPAREN; }
")" { return RPAREN; }
"\n" { return NEWLINE; }
{whitesp} { /* No action and no return */}
tcalc.y
/* tcalc.y - a four function calculator */
%{
#define YYSTYPE double /* yyparse() stack type */
#include <stdlib.h>
%}
/* BISON Declarations */
%token NEWLINE NUMBER PLUS MINUS SLASH ASTERISK LPAREN RPAREN
/* Grammar follows */
%%
input: /* empty string */
| input line
;
line: NEWLINE
| expr NEWLINE { printf("\t%.10g\n",$1); }
;
expr: expr PLUS term { $$ = $1 + $3; }
| expr MINUS term { $$ = $1 - $3; }
| term
;
term: term ASTERISK factor { $$ = $1 * $3; }
| term SLASH factor { $$ = $1 / $3; }
| factor
;
factor: LPAREN expr RPAREN { $$ = $2; }
| NUMBER
;
%%
/*--------------------------------------------------------*/
/* Additional C code */
/* Error processor for yyparse */
#include <stdio.h>
int yyerror(char *s) /* called by yyparse on error */
{
printf("%s\n",s);
return(0);
}
/*--------------------------------------------------------*/
/* The controlling function */
#include "lex.h"
int parse(void)
{
char exp[] = "2+3\n\0\0";
yy_scan_buffer(exp, sizeof(exp));
yyparse();
exit(0);
}
当我尝试用我的编译器编译它时,我收到一个关于 EINTR 未找到的错误。我的 errno.h 头文件中没有 EINTR(来自我的编译器的工具链)。
是否有一些选项确实使 flex/bison 更轻量级并且对 POSIX 的依赖更少?
【问题讨论】:
-
如果你的平台永远不会返回
EINTRerrno,你能简单地通过#define EINTR 999来解决这个问题吗?
标签: parsing bison flex-lexer