| Refresh | Home EGTry.com

dynamic scope, rule-qualified variable


antlr dynamic scope example grammar

grammar P;

prog:
  block+
 ;


block
scope {
  String name;
}
  :
  ID {$block::name=$ID.text;} '{'
    (declare
    |
    block)*
   '}'
   ;

declare: 
   TYPE ID ';' {System.out.println("block:"+$block::name+" id="+$ID.text);}
   ;


TYPE: 'int';

ID: ('a' .. 'z'|'A' .. 'Z')+ ('0' .. '9')*;

WS: (' '|'\r'|'\n') {$channel=HIDDEN;};


input

B1 { int a1; int b1;
  B11 {int a11; int b11;
    B111 {int a111; int b111;}
 int x11; int y11;}
 int x1; int y1;}


Output

block:B1 id=a1
block:B1 id=b1
block:B11 id=a11
block:B11 id=b11
block:B111 id=a111
block:B111 id=b111
block:B11 id=x11
block:B11 id=y11
block:B1 id=x1
block:B1 id=y1