-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.mly
94 lines (76 loc) · 2.2 KB
/
parser.mly
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
86
87
88
89
90
91
92
93
94
%{
open Ast;;
%}
%token LPAREN RPAREN PLUS TIMES EQUAL SEMICOL VAR COMMA IF
%token THEN ELSE LBRACKET RBRACKET WHILE LESS THROW CALLCC
%token GREAT COLONEQUAL END TRY WITH RAISE BREAK PLUSPLUS MINUSMINUS
%token<int> INTEGER
%token<string> IDENT STRING
%token EOF
%left PLUS
%left TIMES
%left LESS
%left GREAT
%left EQUAL
%start program
%type <Ast.instruction list> program
%%
program:
instructions EOF { $1 }
instructions:
| instruction { [$1] }
| instruction instructions { $1::$2 }
instruction:
| SEMICOL { AstEmpty }
| declaration { AstDeclaration($1) }
| command { AstCommand($1) }
command:
| assignment SEMICOL { $1 }
| call SEMICOL { $1 }
| alternative { $1 }
| loop_while { $1 }
| CALLCC IDENT LBRACKET instructions RBRACKET { AstCallcc($2,$4) }
| THROW IDENT SEMICOL { AstThrow($2) }
| TRY if_instructions WITH IDENT if_instructions { AstTryWith($2,$4,$5) }
| RAISE IDENT SEMICOL { AstRaise($2) }
| BREAK { AstBreak }
| IDENT PLUSPLUS { AstPlusPlus $1 }
| IDENT MINUSMINUS { AstMinusMinus $1 }
declaration:
| declaration_var SEMICOL { $1 }
declaration_var:
| VAR variables { AstDeclarationVariable($2) }
variables:
| variable { [$1] }
| variable COMMA variables { $1::$3 }
assignment:
| variables COLONEQUAL value { AstAssignment($1,$3) }
value:
| expression { AstValueExpression($1) }
| STRING { AstValueString($1) }
call:
| IDENT { AstCall($1,[]) }
| IDENT arguments { AstCall($1,$2) }
arguments:
| STRING { [AstValueString $1] }
| expression { [AstValueExpression $1] }
| expression arguments { (AstValueExpression $1)::$2 }
alternative:
| IF expression if_instructions ELSE if_instructions { AstAlternative($2,$3,$5) }
| IF expression if_instructions { AstAlternative($2,$3,[]) }
if_instructions:
| LBRACKET instructions RBRACKET { $2 }
loop_while:
| WHILE expression LBRACKET instructions RBRACKET { AstWhile($2,$4) }
variable:
| IDENT { $1 }
expression:
| IDENT { AstVariable($1) }
| INTEGER { AstEntier($1) }
| expression PLUS expression { AstAddition ($1, $3) }
| expression TIMES expression { AstMultiplication ($1, $3) }
| expression LESS expression { AstLess ($1, $3) }
| expression GREAT expression { AstGreat ($1, $3) }
| expression EQUAL expression { AstEqual ($1, $3) }
| LPAREN expression RPAREN { $2 }
;