title: Using yacc to output boolean expressions
body: <p>I am trying to learn this language for a college class and our teacher gave us a prompt to try. Basically we are to take a boolean expression and output if that expression is true or false. The input will be in the format of:
  true and (false or true) or false.</p>
  
  <p>I have talked with my professor about many solutions and he is wanting the class to make tokens for AND OR NOT TRUE FALSE. He also wants us to use the logical operators in the yacc file instead of the tokens, IE ||, &amp;&amp;, !.</p>
  
  <pre><code>test.l
  %{
  #include "y.tab.h"
  %}
  
  AND   [Aa][Nn][Dd]
  OR    [Oo][Rr]
  NOT   [Nn][Oo][Tt]
  op '&amp;' | '|' | "!"
  %%
  
  [a-zA-Z] {return ALPHA;}
  [\t]+    ;
  [\n]     {return '\n';}
  {AND}    { return (AND); }
  {OR}     { return (OR); }
  {NOT}    { return (NOT); }
  [Tt][Rr][Uu][Ee]                   { yylval = 1;
                                    return (boolean); }
  [Ff][Aa][Ll][Ss][Ee]               { yylval = 0;
                                    return (boolean); }
  
  . {();}
  %%
  </code></pre>
  
  <pre><code>test.y
  
  %{
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int yylex();
  %}
  
  %token ALPHA AND OR NOT TRUE FALSE boolean
  %left "&amp;" "|"
  %right '!'
  
  %%
  program: bexpr '\n' {if ($1 &gt;= 1)
                     {
                      printf("TRUE\n");
                      exit(0);
                     }
                     else{
                      printf("FALSE\n");
                      exit(0);
   }
  |
  ;
  
  bexpr:    bexpr "|""|" bterm { $$ = $1 || $3; }
            | bterm { $$ = $1; }
  ;
  bterm:    bterm "&amp;""&amp;" bfactor { $$ = $1 &amp;&amp; $3; }
            | bfactor { $$ = $1; }
   ;
  bfactor:  '!' bfactor      { $$ = ! $2; }
             | '(' bexpr ')' { $$ = $2; }
             | TRUE { $$ = $1; }
             | FALSE {$$ = $1; }
             | boolean { $$ = $1; }
             ;
  %%
  int main()
  {
  printf("Enter your truth statement\n");
  yyparse();
  return 0;
  }
  </code></pre>
  
  <p>If i were to put in true and false, would expect false. However, I get syntax error. If I only put in true, the output is correct, same for false. Basically if I put anything other than one term, the program throws an error.</p>