/
FL.g
61 lines (51 loc) · 1.59 KB
/
FL.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
grammar FL;
@header {
import types.*;
import java.util.LinkedList;
}
program returns [Program program] :
{ LinkedList<Function> functions = new LinkedList<Function>(); }
( f=function { functions.add($f.function); } )+
{ $program = new Program(functions); }
;
function returns [Function function] :
{ LinkedList<String> args = new LinkedList<String>(); }
n=ID
( a=ID { args.add($a.text); } )+
'='
e=expr NEWLINE+
{ $function = new Function($n.text,args,$e.expr); }
;
expr returns [Expr expr] :
b=binary { $expr = $b.expr; }
| a=apply { $expr = $a.expr; }
| i=ifThenElse { $expr = $i.expr; }
;
binary returns [Expr expr] :
l=atom { $expr = $l.expr; }
( o=ops r=atom { $expr = new Binary($o.symbol,$expr,$r.expr); } )*
;
apply returns [Apply expr] :
{ LinkedList<Expr> args = new LinkedList<Expr>(); }
i=ID
( a=atom { args.add($a.expr); } )+
{ $expr = new Apply($i.text,args); }
;
ifThenElse returns [IfThenElse expr] :
'if' c=expr 'then' e1=expr 'else' e2=expr
{ $expr = new IfThenElse($c.expr,$e1.expr,$e2.expr); }
;
atom returns [Expr expr] :
ID { $expr = new Argument($ID.text); }
| INT { $expr = new Literal(Integer.parseInt($INT.text)); }
| '(' e=expr ')' { $expr = $e.expr; }
;
ops returns [Ops symbol] :
'==' { $symbol = Ops.Equal; }
| '+' { $symbol = Ops.Plus; }
| '-' { $symbol = Ops.Minus; }
;
ID : ('a'..'z')+ ;
INT : '-'?'0'..'9'+ ;
NEWLINE : '\r'? '\n' ;
WS : (' '|'\t')+ {skip();} ;