title: Suitable stage for building symbol table
body: <p>Are there any reasons to start building symbol table at the lexer stage?</p>
  
  <p>In the book <a href="https://rads.stackoverflow.com/amzn/click/com/0596155972" rel="nofollow noreferrer" rel="nofollow noreferrer">flex &amp; bison: Text Processing Tools</a> the author gives an example of lexer with an attempt to build a simple symbol table. There is a workaround in the following code to distinguish a symbol definition from its references:</p>
  
  <pre class="lang-flex prettyprint-override"><code>/* declaration keywords */
  auto |
  char |
  int  |
  /* ... skip ... */
  volatile { defining = 1; }
  
  /* ... skip ... */
  
  /* punctuators */
  "{"|"&lt;%"|";"
  { defining = 0; }
  </code></pre>
  
  <p>This solution will not work in more complicated cases, such as <code>int a = b, c = d;</code> (symbol <code>c</code> will not be marked as a definition). In addition to this,  nested scopes cannot be handled at the lexer stage.</p>
  
  <p>It is noticed in the question <a href="https://stackoverflow.com/questions/31262197/lex-and-yacc-symbol-table-generation">lex and yacc (symbol table generation)</a> that accessing symbol table from lexer is conventional, but I still can not see pros and why table built in lexer may be useful later.</p>