2
votes

I am trying to write a calculator for nor expressions, such as "true nor false". Here is my .l file:

%{
#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");
}

Here is my .y file:

/* 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;}
    ;
%%

The problem is that when I enter an expression like "true nor false", I don't see any result being printed out. Anyone know what's wrong?

1
Does bison even compile it? Seems like you have a syntax error in the definition of line.StoryTeller - Unslander Monica
Yes, it does. I accidentally posted it with a syntax error in here, but I don't have one in the original code.John Roberts

1 Answers

2
votes

This

printf ("%s",$1);

Attempts to print a string at address 0 or 1. You probably meant

printf ("%d",$1);