【问题标题】:Dependency problems with flex/bisonflex/bison 的依赖问题
【发布时间】: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


【解决方案1】:

简而言之:没有。flex 将在所有 C 扫描仪中引用 EINTR。所以你基本上有三个选择(按降序排列):

  1. 修复您的编译器。哪个没有定义EINTR
  2. %top 块中定义您自己的YY_INPUT
  3. 定义一个虚拟EINTR0INT_MAX 可能是此特定应用程序的不错选择,但重新定义标准宏总是会产生有趣的副作用。

【讨论】:

  • 看起来“不”是正确的答案。我自己定义了 EINTR,但遇到了很多关于非 ANSI 内容的其他错误(如 unistd.h 等)并放弃了。我目前正在使用 Ragel。它看起来更适合嵌入式开发。
  • @haole flex 有一个选项nounistd。如果还有其他的热门话题,我很想听听他们的看法——flex 当然不想过多地阻碍它在非标准环境中的使用:-)。另一方面,如果 Ragel 满足您的需求,您可能更适合那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2023-03-31
  • 2012-05-23
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多