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