title: Why is my bison/flex not working as intended?
body: <p>I have this homework assignment where I have to transform some input into a particular output. The problem I'm having is that I can only convert the first line into the output I need, the other lines return a "syntax error" error.</p>
  
  <p>Additionally, if I change the lines order, no lines are converted so only one particular line is working.</p>
  
  <p>This is my input file: </p>
  
  <p><strong>Input.txt</strong></p>
  
  <pre><code>B0102 Bobi 2017/01/16 V8 1, massage 12.50
  J1841 Jeco 20.2 2017/01/17 V8 2, Tosse 2, tosquia 22.50
  B2232 Bobi 2017/01/17 Tosse 1, Leptospirose 1, bath 30.00, massage 12.50
  B1841 Jeco 21.4 2017/01/18 Leptospirose 1, Giardiase 2
  </code></pre>
  
  <p>And this is the output I should obtain:</p>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Bobi (B0102) paid 2 services/vaccines 22.50
  Jeco (J1841) paid 3 services/vaccines 62.50
  Bobi (B2232) paid 4 services/vaccines 62.50
  Jeco (B1841) paid 2 services/vaccines 30.00
  </code></pre>
  
  <p>If I change the line order in the input file, not even the first line is converted.
  However, if the order is as I showed above, this is my output:</p>
  
  <pre><code>Bobi (B0102) paid 2 services/vaccines 22.50
  syntax error
  </code></pre>
  
  <p>This is my code:</p>
  
  <p><strong>file.y</strong></p>
  
  <pre><code>%{
      #include "file.h"
      #include &lt;stdio.h&gt;
      int yylex();
      int counter = 0;
      int vaccineCost = 10;
  %}
  
  %union{
      char* code;
      char* name;
      float value;
      int quantity;
  };
  
  %token COMMA WEIGHT DATE SERVICE VACCINE
  %token CODE
  %token NAME
  %token VALUE
  %token QUANTITY
  
  %type &lt;name&gt; NAME
  %type &lt;code&gt; CODE
  %type &lt;value&gt; VALUE
  %type &lt;quantity&gt; QUANTITY
  %type &lt;value&gt; services
  
  
  %start begining
  
  %%
  
  begining: /*empty*/
      | animal
      ;
  
  animal: CODE NAME WEIGHT DATE services {printf("%s (%s) paid %d services/vaccines %.2f\n", $2, $1, counter, $5); counter = 0;}
      | CODE NAME DATE services {printf("%s (%s) paid %d services/vaccines %.2f\n", $2, $1, counter, $4); counter = 0;}
      ;
  
  services: services COMMA SERVICE VALUE {$$ = $1 + $4; counter++;}
      | services COMMA VACCINE QUANTITY{$$ = $1 + $4*vaccineCost;counter++;}
      | SERVICE VALUE{$$ = $2;counter++;}
      | VACCINE VALUE 
  {$$ = $2*vaccineCost;counter++;}
      ;
  
  %%
  
  int main(){
      yyparse();
      return 0;
  }
  
  void yyerror (char const *s) {
      fprintf (stderr, "%s\n", s);
  }
  
  </code></pre>
  
  <p><strong>file.flex</strong></p>
  
  <pre><code>%option noyywrap
  
  %{
      #include "file.h"
      #include "file.tab.h"
      #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
  %}
  
  /*Patterns*/
  YEAR 20[0-9]{2}
  MONTH 0[1-9]|1[0-2]
  DAY 0[1-9]|[1-2][0-9]|3[0-1]
  
  %%
  ,                                   {return COMMA,;}
  [A-Z][0-9]{4}            {yylval.code = strdup(yytext); return CODE;}       
  [A-Z][a-z]*          {yylval.name = strdup(yytext); return NAME;}
  [0-9]+[.][0-9]                             {return WEIGHT;}
  {YEAR}"/"{MONTH}"/"{DAY}                           {return DATE;}
  (banho|massagem|tosquia)                    {return SERVICE;}
  [0-9]+\.[0-9]{2}              {yylval.value = atof(yytext);return VALUE;}
  (V8|V10|Anti-Rabatica|Giardiase|Tosse|Leptospirose)          {return VACCINE;}
  [1-9]           {yylval.quantity = atoi(yytext);return QUANTITY;}
  \n  
  .       
  &lt;&lt;EOF&gt;&gt; return 0;
  
  %%
  </code></pre>
  
  <p>And these are the commands I execute:</p>
  
  <pre><code>bison -d file.y
  flex -o file.c file.flex
  gcc file.tab.c file.c -o exec -lfl
  ./exec &lt; Input.txt
  </code></pre>
  
  <p>Can anyone point me in the right direction or tell me what is wrong with my code?</p>
  
  <p>Thanks and if I my explaination wasn't good enough I'll try my best to explain it better!!</p>