| Refresh | Home EGTry.com

the tree of a simplied java class definition


tree of the input:

public class A extends B implements I,J,K { int a; byte b;  int X(); byte Y();}


antlr tree construction grammar

grammar TreeConstruct;
options 
{
  output=AST;
}

tokens 
{
  Fields;
  Methods;

}
/*
public class A extends B implements I,J,K { int a; byte b;  int X(); byte Y();}
*/
prog 
 : modifier? 'class' name=ID 
  ('extends' superclass=ID)? 
  ('implements' interfaces+=ID  (',' interfaces+=ID)* )?
  '{'
    (
      fieldDef
     | methodDef
    )*   
  '}' ->
   ^($name 
      ^('extends' $superclass)? 
      ^('implements' $interfaces+)? 
      ^(Fields fieldDef+)? 
      ^(Methods methodDef+)?
   )
   ;


fieldDef:
  type ID^ ';'!
  ; 

methodDef:
  type ID^ '('! ')'! ';'!
  ;
   
type: 'int' | 'byte';
  
modifier: 
  'public' | 'protected'| 'private'
  ;
 
ID: 'a' .. 'z' | 'A' .. 'Z';

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