-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiYACC.mly
95 lines (72 loc) · 2.37 KB
/
miniYACC.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
95
%{
(*
Alex Halter
HPL Spring 14
File miniYACC.mly *)
open AstTree
%} /* declarations */
%token EOL SEMI ASSIGN PLUS /* lexer tokens */
%token MINUS MULT DIV LPAR RPAR
%token EQUALS LT DOT WHILE THEN
%token PROC TRUE FALSE MALLOC IF ELSE
%token CONCURR ATOM NULL SKIP COLON LBRACK RBRACK
%token VAR
%token INTEGER
%start prog /* the entry point */
%type <unit> prog
%type <AstTree.cmdNode> cmd assign seqcon parallel
%type <AstTree.expNode> expr location fieldLocation
%type <AstTree.boolNode> boolean
%token < string > IDENT
%token < string > FIELD
%token < int > INTEGER
%left ASSIGN
%left PLUS MINUS /* lowest precedence */
%left TIMES DIV /* medium precedence */
%left DOT LT EQUALS
%nonassoc UMINUS /* highest precedence */
%% /* rules */
prog :
cmd EOL {
AstTree.run $1
}
cmd :
assign {$1}
| seqcon {$1}
| expr LPAR expr RPAR { Call($1,$3,[])}
| VAR IDENT SEMI cmd {Declaration($2,$4,[])}
| MALLOC LPAR IDENT RPAR {Malloc($3,[])}
| parallel {$1}
expr :
expr PLUS expr { Math(AstTree.Plus,$1,$3,[]) }
| expr MINUS expr { Math(AstTree.Sub,$1,$3,[]) }
| expr TIMES expr { Math(AstTree.Mult,$1,$3,[]) }
| expr DIV expr { Math(AstTree.Div,$1,$3,[]) }
| MINUS expr %prec UMINUS { Minus($2,[])}
| PROC IDENT COLON cmd {Procedure($2,$4,[])}
| location {$1 }
| INTEGER {Integer($1)}
| FIELD {Field($1,[])}
parallel:
LBRACK cmd CONCURR cmd RBRACK {Concurrent($2,$4,[])}
| ATOM LPAR cmd RPAR { Atom ($3,[])}
boolean :
TRUE {True}
| FALSE {False}
| expr EQUALS expr {Equals($1,$3,[])}
| expr LT expr {Lessthan($1,$3,[])}
location:
NULL { Null }
| IDENT {Variable($1,[])}
| expr fieldLocation {FieldLocation($1,$2,[])}
fieldLocation :
DOT expr { $2 }
seqcon:
SKIP { Skip }
| LBRACK cmd SEMI cmd RBRACK { Sequence($2,$4,[])}
| WHILE boolean cmd { While($2,$3,[]) }
| IF boolean cmd ELSE cmd {If($2,$3,$5,[])}
assign :
IDENT ASSIGN expr {VarAssign($1,$3,[]) }
| expr DOT expr ASSIGN expr {FieldAssign ($1,$3,$5,[])}
%% (* trailer *)