title: Lisp grammar in yacc
body: <p>I am trying to build a Lisp grammar. Easy, right? Apparently not.</p>
  
  <p>I present these inputs and receive errors...</p>
  
  <pre><code>( 1 1)
  23 23 23 
  ui ui
  </code></pre>
  
  <p>This is the grammar...</p>
  
  <pre><code>%%
  sexpr: atom                 {printf("matched sexpr\n");}
      | list
      ;
  list: '(' members ')'       {printf("matched list\n");}
      | '('')'                {printf("matched empty list\n");}
      ;
  members: sexpr              {printf("members 1\n");}
      | sexpr members         {printf("members 2\n");}
      ;
  atom: ID                    {printf("ID\n");}
      | NUM                   {printf("NUM\n");}
      | STR                   {printf("STR\n");}
      ;
  %%
  </code></pre>
  
  <p>As near as I can tell, I need a single non-terminal defined as a program, upon which the whole parse tree can hang. But I tried it and it didn't seem to work.</p>
  
  <p>edit - this was my "top terminal" approach: </p>
  
  <pre><code>program: slist;
  
  slist: slist sexpr | sexpr;
  </code></pre>
  
  <p>But it allows problems such as:</p>
  
  <pre><code>( 1 1 
  </code></pre>
  
  <p>Edit2: The FLEX code is...</p>
  
  <pre><code>%{
      #include &lt;stdio.h&gt;
      #include "a.yacc.tab.h"
      int linenumber;
      extern int yylval;
  %}
  %%
  \n                         { linenumber++; }
  [0-9]+                     { yylval = atoi(yytext); return NUM; }
  \"[^\"\n]*\"               { return STR; }
  [a-zA-Z][a-zA-Z0-9]*       { return ID; }
  .
  %%
  </code></pre>
  
  <p>An example of the over-matching...</p>
  
  <pre><code>(1 1 1)
  NUM
  matched sexpr
  NUM
  matched sexpr
  NUM
  matched sexpr
  (1 1
  NUM
  matched sexpr
  NUM
  matched sexpr
  </code></pre>
  
  <p>What's the error here? </p>
  
  <p>edit: The error was in the lexer.</p>