title: LEX: code to read a specific grammar error
body: <p>Very new to lex. This is for project for Prog. Langs. class</p>
  
  <p>Consider a language built over the following grammar:</p>
  
  <pre><code>&lt;program&gt; ::= &lt;statement&gt; | &lt;program&gt; &lt;statement&gt;
  &lt;statement&gt; ::= &lt;assignStmt&gt; | &lt;ifStmt&gt; | &lt;whileStmt&gt; | &lt;printStmt&gt;
  &lt;assignStmt&gt; ::= &lt;id&gt; = &lt;expr&gt; ;
  &lt;ifStmt&gt; ::= if ( &lt;expr&gt; ) then &lt;stmt&gt;
  &lt;whileStmt&gt; ::= while ( &lt;expr&gt; ) do &lt;stmt&gt;
  &lt;printStmt&gt; ::= print &lt;expr&gt; ;
  &lt;expr&gt; ::= &lt;term&gt; | &lt;expr&gt; &lt;addOp&gt; &lt;term&gt;
  &lt;term&gt; ::= &lt;factor&gt; | &lt;term&gt; &lt;multOp&gt; &lt;factor&gt;
  &lt;factor&gt; ::= &lt;id&gt; | &lt;number&gt; | - &lt;factor&gt; | ( &lt;expr&gt; )
  &lt;id&gt; ::= &lt;letter&gt; | &lt;id&gt; &lt;letter&gt;
  &lt;letter&gt; ::= a | b | c | d | e | f | g | h | i | j
  | k | l | m | n | o | p | r | s | t
  | u | v | w | x | y | z
  &lt;number&gt; ::= &lt;digit&gt; | &lt;number&gt; &lt;digit&gt;
  &lt;digit&gt; ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  &lt;addOp&gt; ::= + | -
  &lt;multOp&gt; ::= * | / | %
  </code></pre>
  
  <p>Implement a lex-based C program that scans for all the tokens of the language (keywords, identifiers, numbers, operators, and so on). </p>
  
  <p>My problem is I get "l7t2.l:32: unrecognized rule" error. I believe it stems from the declaration of "word" above but not sure how to fix it.</p>
  
  <p>Heres my lex file,  l7t2.l</p>
  
  <pre><code>%option noyywrap
  
  %{
  #include "l7t2.h"
  int totDol = 0;
  int *outword;
  %}
  
  digit [0-9]
  number {digit}*
  
  letter  [a-zA-Z]
  word    ({letter}{[a-zA-Z0-9]}+)
  
  %%
  
  "if" {return IF;}
  "then" {return THEN;}
  "while" {return WHILE;}
  "do" {return DO;}
  "+" {return PLUSOP;}
  "-" {return MINUSOP;}
  "*" {return MULTOP;}
  "/" {return DIVOP;}
  "%" {return MODOP;}
  ";" {return SEMICOLON;}
  "=" {return EQUAL;}
  "print" {return PRINT;}
  
  [ \t\n]+        ;
  
  {word} {strcpy(outword, yytext);}
  
  \${number}  {totDol = 0; totDol += strtod(yytext+1, NULL); return totDol;}
  
  %%
  </code></pre>