Home | History | Annotate | Download | only in tests
      1 tree grammar t047treeparserWalker;
      2 options {
      3     language=Python;
      4     tokenVocab=t047treeparser;
      5     ASTLabelType=CommonTree;
      6 }
      7 
      8 program
      9     :   declaration+
     10     ;
     11 
     12 declaration
     13     :   variable
     14     |   ^(FUNC_DECL functionHeader)
     15     |   ^(FUNC_DEF functionHeader block)
     16     ;
     17 
     18 variable returns [res]
     19     :   ^(VAR_DEF type declarator)
     20         { 
     21             $res = $declarator.text; 
     22         }
     23     ;
     24 
     25 declarator
     26     :   ID 
     27     ;
     28 
     29 functionHeader
     30     :   ^(FUNC_HDR type ID formalParameter+)
     31     ;
     32 
     33 formalParameter
     34     :   ^(ARG_DEF type declarator)
     35     ;
     36 
     37 type
     38     :   'int'
     39     |   'char'
     40     |   'void'
     41     |   ID        
     42     ;
     43 
     44 block
     45     :   ^(BLOCK variable* stat*)
     46     ;
     47 
     48 stat: forStat
     49     | expr
     50     | block
     51     ;
     52 
     53 forStat
     54     :   ^('for' expr expr expr block)
     55     ;
     56 
     57 expr:   ^(EQEQ expr expr)
     58     |   ^(LT expr expr)
     59     |   ^(PLUS expr expr)
     60     |   ^(EQ ID expr)
     61     |   atom
     62     ;
     63 
     64 atom
     65     : ID      
     66     | INT      
     67     ; 
     68