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.

92 lines
1.2 KiB

grammar t017parser;
options {
language = JavaScript;
}
program
: declaration+
;
declaration
: variable
| functionHeader ';'
| functionHeader block
;
variable
: type declarator ';'
;
declarator
: ID
;
functionHeader
: type ID '(' ( formalParameter ( ',' formalParameter )* )? ')'
;
formalParameter
: type declarator
;
type
: 'int'
| 'char'
| 'void'
| ID
;
block
: '{'
variable*
stat*
'}'
;
stat: forStat
| expr ';'
| block
| assignStat ';'
| ';'
;
forStat
: 'for' '(' assignStat ';' expr ';' assignStat ')' block
;
assignStat
: ID '=' expr
;
expr: condExpr
;
condExpr
: aexpr ( ('==' | '<') aexpr )?
;
aexpr
: atom ( '+' atom )*
;
atom
: ID
| INT
| '(' expr ')'
;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
INT : ('0'..'9')+
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
)+
{$channel=HIDDEN;}
;