2012-02-14 124 views
0

我想编译一个简单的计算器例子,我在互联网上找到了我的嵌入式环境,但是我对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的东西更少依赖?

+0

如果你的平台永远不会返回'EINTR'' errno',你能简单地用'#define EINTR 999'来解决吗? – sarnold 2012-02-14 22:48:05

回答

0

总之:在所有C扫描仪中,No. flex将引用EINTR。所以你基本上有三种选择(按降序排列):

  1. 修复你的编译器。哪一个没有定义EINTR
  2. %top区块中定义您自己的YY_INPUT
  3. 定义虚拟EINTR; 0INT_MAX对于这个特定的应用程序来说可能是很好的选择,但是重新定义标准的宏总是会有有趣的副作用。
+0

看起来像'不'是正确的答案。我自己定义了EINTR,但得到了很多关于非ANSI东西的其他错误(如unistd.h等)并放弃了。我目前正在使用Ragel。它看起来更适合嵌入式开发。 – ivarec 2012-02-17 18:04:21

+0

@haole flex有一个选项'nounistd'。如果有其他showstoppers,我会有兴趣听到他们 - flex肯定不想阻止在非标准环境中使用它的必要性:-)。另一方面,如果Ragel满足您的需求,那么您可能更适合那里。 – 2012-02-17 22:25:57