Home | History | Annotate | Download | only in scopes
      1 grammar SymbolTable;
      2 
      3 /* Scope of symbol names.  Both globals and block rules need to push a new
      4  * symbol table upon entry and they must use the same stack.  So, I must
      5  * define a global scope and say that globals and block use this by saying
      6  * 'scope Symbols;' in those rule definitions.
      7  */
      8 
      9 options {
     10 	language=ObjC;
     11 }
     12 
     13 scope Symbols {
     14   ANTLRPtrBuffer *names;
     15 }
     16 
     17 @memVars {
     18 int level;
     19 }
     20 
     21 @init {
     22 level = 0;
     23 }
     24 
     25 prog
     26 // scope Symbols;
     27     :   globals (method)*
     28     ;
     29 
     30 globals
     31 scope Symbols;
     32 @init {
     33     level++;
     34     $Symbols::names = [ANTLRPtrBuffer newANTLRPtrBufferWithLen:10];
     35 }
     36     :   (decl)*
     37         {
     38             NSLog( @"globals: \%@", [$Symbols::names toString] );
     39             level--;
     40         }
     41     ;
     42 
     43 method
     44     :   'method' ID '(' ')' block
     45     ;
     46 
     47 block
     48 scope Symbols;
     49 @init {
     50     level++;
     51     $Symbols::names = [ANTLRPtrBuffer newANTLRPtrBufferWithLen:10];
     52 }
     53     :   '{' (decl)* (stat)* '}'
     54         {
     55             NSLog( @"level \%d symbols: \%@", level, [$Symbols::names toString] );
     56             level--;
     57         }
     58     ;
     59 
     60 stat:   ID '=' INT ';'
     61     |   block
     62     ;
     63 
     64 decl:   'int' ID ';'
     65         {[$Symbols::names addObject:$ID];} // add to current symbol table
     66     ;
     67 
     68 ID  :   ('a'..'z')+
     69     ;
     70 
     71 INT :   ('0'..'9')+
     72     ;
     73 
     74 WS  :   (' '|'\n'|'\r')+ {$channel=HIDDEN;}
     75     ;
     76