title: Is there an option for flex to match whole words only?
body: <p>I'm writing a lexer and I'm using Flex to generate it based on custom rules.</p>
  
  <p>I want to match identifiers of sorts that start with a letter and then can have either letters or numbers. So I wrote the following pattern for them:</p>
  
  <pre><code>[[:alpha:]][[:alnum:]]*
  </code></pre>
  
  <p>It works fine, the lexer that gets generated recognizes the pattern perfectly, although it doesn't only match whole words but all appearances of that pattern.</p>
  
  <p>So for example it would match the input "Text" and "9Text" (discarding that initial 9).</p>
  
  <p>Consider the following simple lexer that accepts IDs as described above:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  
  #define LINE_END 1
  #define ID       2
  
  %}
  
  /* Flex options: */
  %option noinput
  %option nounput
  %option noyywrap
  %option yylineno
  
  /* Definitions: */
  WHITESPACE  [ \t]
  BLANK       {WHITESPACE}+
  NEW_LINE    "\n"|"\r\n"
  ID          [[:alpha:]][[:alnum:]_]*
  
  %%
  
  {NEW_LINE}        {printf("New line.\n"); return LINE_END;}
  {BLANK}           {/* Blanks are skipped */}
  {ID}              {printf("ID recognized: '%s'\n", yytext); return ID;}
  .                 {fprintf(stderr, "ERROR: Invalid input in line %d: \"%s\"\n", yylineno, yytext);}
  
  %%
  
  int main(int argc, char **argv) {
     while (yylex() != 0);
     return 0;
  }
  </code></pre>
  
  <p>When compiled and fed the following input produces the output below:</p>
  
  <p>Input:</p>
  
  <pre><code>Test
  9Test
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Test
  ID recognized: 'Test'
  New line.
  9Test
  ERROR: Invalid input in line 2: "9"
  ID recognized: 'Test'
  New line.
  </code></pre>
  
  <p>Is there a way to make flex match only whole words (i.e. delimited by either blanks or custom delimiters like '(' ')' for example)?</p>
  
  <p>Because I could write a rule that excludes IDs that start with numbers, but what about the ones that start with symbols like "$Test" or "&amp;Test"? I don't think I can enumerate all of the possible symbols.</p>
  
  <p>Following the example above, the desired output would be:</p>
  
  <pre><code>Test
  ID recognized: 'Test'
  New line.
  9Test
  ERROR: Invalid input 2: "9Test"
  New line.
  </code></pre>