title: Flex and Bison: Beginning a sentence with a specific keyword
body: <p>I am working on a program using Flex and Bison. My task can be done using only Flex(using start conditions etc.), but I have understood that using Bison might make my life easier. My task is to design a program which recognizes a programming language declaration part. Its syntax and logic can be understood through my code below. My problem is that I want my program to recognize as an acceptable declaration part every part of code which begins <strong>only</strong> with the "<strong>var</strong>" keyword! Until now, I have not managed to do that. How can I succeed it?</p>
  
  <p>I present below my .l (flex) and .y (bison) files.</p>
  
  <p><strong>exercise4.l</strong></p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  %}
  %%
  [ \t\n]+                { /* Ignore all whitespaces */ }
  ";"         { /* Ignore all semicolons */ }
  ":"         { /* Ignore all colons */ }
  var                     { printf("A keyword: %s\n",yytext); return VAR; }
  real                    { printf("A variable type: %s\n",yytext); return REAL; }
  boolean                 { printf("A variable type: %s\n",yytext); return BOOLEAN; }
  integer                 { printf("A variable type: %s\n",yytext); return INTEGER; }
  char                    { printf("A variable type: %s\n",yytext); return CHAR; }
  [a-zA-Z][a-zA-Z0-9_]*   { printf("A variable name: %s\n",yytext); return VAR_NAME; }
  .                       { printf("Unrecognized character!\n"); return yytext[0]; }
  %%
  </code></pre>
  
  <p><strong>exercise4.y</strong></p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;    
  %}
  %token VAR VAR_NAME REAL BOOLEAN INTEGER CHAR
  %%
  program : VAR typedecls ;
  typedecls : typedecl | typedecls typedecl ;
  typedecl : varlist ':' var_type ';' ;
  varlist : VAR_NAME | varlist ',' VAR_NAME ;
  var_type : REAL | BOOLEAN | INTEGER | CHAR ;
  %%
  main( argc, argv )
  int argc;
  char **argv;
  {
  extern FILE *yyin;
  ++argv, --argc; /* skip over program name */
  if ( argc &gt; 0 )
  yyin = fopen( argv[0], "r" );
  else
  yyin = stdin;
  //yylex();
  yyparse();
  }
  yyerror(char *s)  
  {  
   printf("\nError\n");  
  }
  #include "lex.yy.c"
  </code></pre>