c - Flex Bison 计算器不打印结果

标签 c bison yacc lex flex-lexer

我正在尝试为nor表达式编写一个计算器,例如“true or 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 or false”之类的表达式时,我看不到任何结果被打印出来。有谁知道出了什么问题吗?

最佳答案

这个

printf ("%s",$1);

尝试在地址 01 处打印字符串。你的意思可能是

printf ("%d",$1);

关于c - Flex Bison 计算器不打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15140550/

相关文章:

c - yylineno 在 yacc 文件中始终具有相同的值

c - 指针和指针澄清

c - 如何在 C 中枚举 Windows 上绑定(bind)的 UDP/TCP 套接字

c++ - strace 会阻止程序的正确执行吗?

Bison YYSTYPE : Trying to use char*

c - 什么是驱动程序?

Bison 目标的 CMake 自定义命令

c - C 中的 size_t 是什么?

c - 我如何使用变量类型标记此输入行,以便我可以决定保存我的信息的结构的大小?

c - 使用 Bison/Yacc 在 %union def 中包含结构