You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
937 B

tree grammar t047treeparserWalker;
options {
language=Python3;
tokenVocab=t047treeparser;
ASTLabelType=CommonTree;
}
program
: declaration+
;
declaration
: variable
| ^(FUNC_DECL functionHeader)
| ^(FUNC_DEF functionHeader block)
;
variable returns [res]
: ^(VAR_DEF type declarator)
{
$res = $declarator.text;
}
;
declarator
: ID
;
functionHeader
: ^(FUNC_HDR type ID formalParameter+)
;
formalParameter
: ^(ARG_DEF type declarator)
;
type
: 'int'
| 'char'
| 'void'
| ID
;
block
: ^(BLOCK variable* stat*)
;
stat: forStat
| expr
| block
;
forStat
: ^('for' expr expr expr block)
;
expr: ^(EQEQ expr expr)
| ^(LT expr expr)
| ^(PLUS expr expr)
| ^(EQ ID expr)
| atom
;
atom
: ID
| INT
;