我想编译一个简单的计算器例子,我在互联网上找到了我的嵌入式环境,但是我对flex/bison的依赖有一些困难。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的东西更少依赖?
如果你的平台永远不会返回'EINTR'' errno',你能简单地用'#define EINTR 999'来解决吗? – sarnold 2012-02-14 22:48:05