Practical Parsing for ANSI C
by Daniele Paolo Scarpazza


Listing One

Digit             [0-9]
Letter            [a-zA-Z_]
 ...
%%
","                                     { return ','; }
":"                                     { return ':'; }
"="                                     { return '='; }
 ...
"+="                                    { return ADD_ASSIGN;   }
">>="                                   { return RIGHT_ASSIGN; }
"<<="                                   { return LEFT_ASSIGN;  }
 ...
"auto"                                  { return AUTO;         }
"break"                                 { return BREAK;        }
"case"                                  { return CASE;         }
 ...
{Letter}({Letter}|{Digit})*             { return category();   }
 ...
%%
#include <blocks.hpp>
 ...
int category()
{
  Block * blockp = Block::currentp;
  ...
  while (blockp) 
  {
     if (blockp->symbol_table.name_is_defined(yytext)) 
     {
        if (blockp->symbol_table.name_is_typedef(yytext)) 
          return TYPE_NAME;    
        else 
          return IDENTIFIER; 
     }
     blockp = blockp->parentp;
  }
  return IDENTIFIER;
}
 ...


Listing Two

int category()
{
  Block * blockp = Block::currentp;
  ...
  while (blockp) 
  {
     if (blockp->symbol_table.name_is_defined(yytext)) 
     {
        if (blockp->symbol_table.name_is_typedef(yytext)) 
        {
           if (Block::currentp->type_stack.is_valid_type())
         return IDENTIFIER;
       else 
         return TYPE_NAME;    
        } 
        else 
          return IDENTIFIER; 
     }
     blockp = blockp->parentp;
  }
  return IDENTIFIER;
}
 ...


Listing Three

int category()
{
  Block * blockp = Block::currentp;
  ...
  while (blockp) 
  {
     if (blockp->symbol_table.name_is_defined(yytext)) 
     {
        if (blockp->symbol_table.name_is_typedef(yytext)) 
        {
           if (Block::currentp->type_stack.is_valid_type() &&
               parenthesis_level==0)
         return IDENTIFIER;
       else 
         return TYPE_NAME;
        } 
        else 
          return IDENTIFIER; 
     }
     blockp = blockp->parentp;
  }
  return IDENTIFIER;
}
 ...
 


2


