Flex Bison计算器不打印结果

时间:2013-02-28 16:25:21

标签: c bison yacc lex flex-lexer

我正在尝试为表达式编写计算器,例如“true或false”。这是我的.l文件:

%{
#include <stdlib.h>
#include "y.tab.h"
%}

%% 
"true"              {yylval=1;  return BOOLEAN;}
"false"             {yylval=0;  return BOOLEAN;}
"nor"               {return NOR;}
" "                 { }
.                   {return yytext[0];}

%%

int main(void)
{
    yyparse();
    return 0;
}

int yywrap(void)
{
     return 0;
}
int yyerror(void)
{
    printf("Error\n");
}

这是我的.y文件:

/* Bison declarations.  */
%token BOOLEAN
%token NOR
%left NOR
%% /* The grammar follows.  */

input:
    /* empty */
    | input line
    ;

line:
    '\n'
    | exp '\n' {printf ("%s",$1); }
    ;

exp:
    BOOLEAN     { $$ = $1;}
    | exp NOR exp   { $$ = !($1 || $3); }
    | '(' exp ')'  { $$ = $2;}
    ;
%%

问题是,当我输入“true或false”这样的表达式时,我看不到任何结果被打印出来。谁知道什么是错的?

1 个答案:

答案 0 :(得分:2)

这个

printf ("%s",$1);

尝试在地址01处打印字符串。你可能意味着

printf ("%d",$1);