Here is my grammar for the Lustre synchronous language using antlr v3, not yet finished but basically working.
/*
* This is a grammar for the LUSTRE
* Synchronous Language
*
* (c) 2008 Julien Boucaron
*
* TODO
* –> expression every expression
* –> add const modifier for arguments
* –> nested node definitions
* –> check with last version
*/
grammar Lustre;
//////////////
// $<PARSER
//////////////
lustre
: (compilationUnit)* EOF
;
compilationUnit
:
constDeclaration
nodeDeclaration nodeDefinition
;
//////////////
// $<DECL
//////////////
constDeclaration
:
(‘const’ (IDENTIFIER)
(‘,’ IDENTIFIER)*
‘:’ BASICTYPE (‘^’ (shift_expression) )?
‘;’)*
;
nodeDeclaration
:
‘node’ IDENTIFIER ‘(‘ nodeArgs ‘)’
( ‘returns’ ) ‘(‘ nodeArgs ‘)’
constDeclaration nodeVars*
‘;’
;
nodeVars:
‘var’ nodeArgs ‘;’
;
nodeArgs
:
(nodeArgsHelper ‘:’ BASICTYPE (‘^’ (shift_expression) )?)+
;
nodeArgsHelper
:
IDENTIFIER
| IDENTIFIER ‘,’ nodeArgsHelper
;
//////////////
// $>DECL
//////////////
nodeDefinition
:
‘let’
nodeBody*
‘tel’
‘;’
;
nodeBody
:
label? nodeAff ‘:=’ expression ‘;’
| label if_nodebody ‘;’
| asserT ‘;’
| gotO ‘;’
| forStatement ‘;’
;
label :
IDENTIFIER ‘:’
;
asserT :
‘assert’ expression
;
forStatement :
‘for’ IDENTIFIER ‘in’ ‘[' expression ('..' expression)? ']‘
‘let’
nodeBody+
‘tel’
;
nodeAff
:
IDENTIFIER vectorHelper? |
‘(‘ IDENTIFIER vectorHelper?
(‘,’ IDENTIFIER vectorHelper?)+ ‘)’
;
vectorHelper
:
‘[' expression ('..' expression )? ']‘
;
//bug present there
//////////////
// $<EXPRESSION
//////////////
expression
:
binaryExpression
;
unaryExpression
:
BOOLCONSTANT
| REAL
| DECIMAL
| preExpression (‘pre’)? IDENTIFIER vectorHelper?
| preExpression (‘pre’)? IDENTIFIER? concatOrPar vectorHelper?
| if_expression
| current
| gotO
;
preExpression
:
(‘not’|'!’|'~’)? signExpression
;
signExpression
:
(‘+’|'-’|'++’|'–’)?
;
concatOrPar
:
‘(‘ expression (‘,’ expression )* ‘)’
//i want one for par expression and the other one for concat…
;
if_expression
:
‘if’ expression ‘then’
expression
‘else’
expression
‘endif’
;
if_nodebody
:
‘if’ expression ‘then’
nodeBody+
‘else’
nodeBody+
‘endif’
;
current :
‘current’ ‘(‘ expression ‘)’
;
gotO :
‘goto’ IDENTIFIER
;
binaryExpression
:
followBy_expression (‘->’ followBy_expression)*
;
followBy_expression
:
or_expression ( (‘or’|'nor’) or_expression )*
;
or_expression
:
and_expression ( (‘and’|'nand’) and_expression )*
;
and_expression
:
xor_expression ( (‘xor’|'nxor’) xor_expression)*
;
xor_expression
:
equal_expression ( (‘=’|'!=’) equal_expression)*
;
equal_expression
:
compare_expression ( (‘<’|'<=’|'=>’|'>’) compare_expression)*
;
compare_expression
:
shift_expression ( (‘<<’|'>>’) shift_expression )*
;
shift_expression
:
add_expression ( (‘+’|'-’) add_expression )*
;
add_expression
:
mul_expression ( (‘*’|'/’|'%’) mul_expression )*
;
mul_expression
:
when_expression (‘when’ when_expression)*
;
when_expression
:
unaryExpression
;
//////////////
// $>EXPRESSION
//////////////
//////////////
// $>PARSER
//////////////
//////////////
// $<LEXER
//////////////
BASICTYPE
:
‘bool’ |
‘int’ |
‘real’
;
BOOLCONSTANT
:
‘true’ |
‘false’
;
DECIMAL :
(’0′..’9′)+
;
REAL :
FloatPrefix
;
IDENTIFIER
: ( ‘a’..’z’ | ‘A’..’Z’ | ‘_’ ) ( ‘a’..’z’ | ‘A’..’Z’ | ‘_’ | ’0′..’9′ )*
;
fragment
FloatPrefix
: ’0′..’9′+ ( ‘.’ ’0′..’9′+ ) ( ( ‘e’ | ‘E’ ) ( ‘+’ | ‘-’ )? ’0′..’9′+ )?
;
//////////////
// $<Skipped
//////////////
COMMENT :
‘–’ ~( ‘\n’ | ‘\r’ )* NEWLINE {skip();}
;
NEWLINE
: ( ‘\n’ | ‘\r’ | ‘\r\n’ ) {skip();}
;
WS
: ( ‘ ‘ | ‘\u000C’ | ‘\t’ ) {skip();}
;