title: Format validation with lex and yacc
body: <p>Lets say I have a file containing strings like this:</p>
  
  <pre><code>qwerty01234xy+-/
  rtweqq22222xx+++
  </code></pre>
  
  <p>The first 6 chars of [A-Z], then comes 5 of [0-9], then 2 of [A-Z] and at the end 3 of [+-/]. I would like to write a format checker, which yields syntax errors.
  What I was doing till now, is something like this:</p>
  
  <p>lex file:</p>
  
  <pre><code>&lt;code&gt;
  ...
  /*states*/
  %x WORD1_STATE
  %x NUMBER_STATE
  %x WORD22_STATE
  %x ETC_STATE
  %%
  ...
  yy_push_state(ETC_STATE)
  yy_push_state(WORD22_STATE)
  yy_push_state(NUMBER_STATE)
  yy_push_state(WORD1_STATE)
  ...
   /*rules*/
  &lt;WORD1_STATE&gt;^[A-Z]{6}    yy_pop_state(); yylval.string=strdup(yytext); return WORD1;
  &lt;NUMBER_STATE&gt;[0-9]{5}    yy_pop_state(); yylval.string=strdup(yytext); return NUMBER;
  &lt;WORD22_STATE&gt;[A-Z]{2}    yy_pop_state(); yylval.string=strdup(yytext); return WORD2;
  &lt;ETC_STATE&gt;[+-/]{3}    yy_pop_state(); yylval.string=strdup(yytext); return ETC;
  
  \n        /*do nothing*/
  &lt;*&gt;.      fprintf(stderr, "Bad character at line %d column %d: \"%s\"\n", yylloc.first_line, yylloc.first_column, yytext); yy_pop_state();
  &lt;/code&gt;
  </code></pre>
  
  <p>yacc rules:</p>
  
  <pre><code>&lt;code&gt;
   entries :
           | entry
           | error
           ;
   entry : WORD1 NUMBER WORD2 ETC;
  &lt;/code&gt;
  </code></pre>
  
  <p>My goal is the following: if this checker sees a line like this:</p>
  
  <pre><code>aaaaaa01W56ss--1
  </code></pre>
  
  <p>It yields the following error:</p>
  
  <pre><code>Bad character in NUMBER at line x at column 9
  Bad character in ETC at line x at column 16
  </code></pre>
  
  <p>Is this the right direction? My code is not working of course. :)</p>