-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
87 lines (78 loc) · 2.24 KB
/
grammar.txt
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
------------- Modified grammar -------------
SPL = Decl+
Decl = VarDecl | FunDecl
VarDecl = (Type | 'var') id '=' Exp ';'
FunDecl = 'function' id '(' [ FArgs ] ')' [ '::' FunType ] '{' Stmt* '}'
RetType = Type | 'Void'
FunType = [ FTypes ] '->' RetType
FTypes = Type [ FTypes ]
Type = BasicType
| ’(’ Type ’,’ Type ’)’
| ’[’ Type ’]’
| id
| '(' ['forall' id+ '.'] FunType ')'
BasicType = 'Int' | 'Bool' | 'Char'
FArgs = id [ ',' FArgs ]
Stmt = 'if' '(' Exp ')' '{' Stmt* '}' [ 'else' '{' Stmt* '}' ]
| 'while' '(' Exp ')' '{' Stmt* '}'
| Stmt1
| 'return' [ Exp ] ';'
| VarDecl
| FunDecl
Stmt1 = id (ArgList | AttribBody) ';'
AttribBody = [Field] '=' Exp
Exp = Exp1 [ '||' Exp ]
Epx1 = Exp2 [ '&&' Exp1 ]
Exp2 = Exp3 [ ('==' | '!=' | '<' | '<=' | '>' | '>=') Exp2 ]
Exp3 = Exp4 [ ':' Exp3 ]
Exp4 = Exp5 [ (('+' | '-') Exp5)* ]
Exp5 = Exp6 [ (('*' | '/' | '%') Exp6)* ]
Exp6 = [ ('!' | '-')* ] Exp7
Exp7 = int | char | 'False' | 'True' | '[]' | Exp8 | Exp9 | StringLit
Exp8 = id [Field | ArgList]
Exp9 = '(' Exp [ ',' Exp ] ')'
StringLit = '"' char* '"'
ArgList = '(' [ ActArgs ] ')'
ActArgs = Exp [ ',' ActArgs ]
Field = [ '.' ('hd' | 'tl' | 'fst' | 'snd') Field ]
int = [ '-' ] digit+
id = alpha ( '_' | alphaNum )*
------------- Original grammar -------------
SPL ::= Decl+
Decl ::= VarDecl | FunDecl
VarDecl ::= ('var' | Type) id '=' Exp ';'
FunDecl ::= id '(' [ FArgs] ')' [ '∷' funType ] '{' VarCle* Stmt+ '}'
RetType ::= Type | 'Void'
FunType ::= [ FTypes ] '→' RetType
FTypes ::= Type [FTypes]
Type ::= BasicType
| ’(’ Type ’,’ Type ’)’
| ’[’ Type ’]’
| id
BasicType ::= 'Int'
| 'Bool'
| 'Char'
FArgs ::= [FArgs ','] id
Stmt ::= 'if' '(' Exp ')' '{' Stmt* '}' [ 'else' '{' Stmt* '}' ]
| 'while' '(' Exp ')' '{' Stmt* '}'
| id Field '=' Exp ';'
| FunCall ';'
| 'return' [ Exp ] ';'
Exp = id Field
| Exp Op2 Exp
| Op1 Exp
| int
| char
| 'False' | 'True'
| '(' Exp ')'
| FunCall
| '[]'
| '(' Exp ',' Exp ')'
Field ::= [ Field ( '.' 'hd' | '.' tl | '.' fst | '.' snd ) ]
FunCall ::= id '(' [ ActArgs ] ')'
ActArgs ::= Exp [ ',' ActArgs ]
Ops ::= '+' | '-' | '*' | '/' | '%' | '==' | '<'
| '>' | '<=' | '>=' | '!=' | '&&' | '||' | ':'
Op1 ::= '!' | '-'
int ::= [ '-' ] digit+
id ::= alpha ('_' | alphaNum)*