]> git.kianting.info Git - uann/blob - ocaml_yacc/parser.mly
add ocaml EoC ast viewet
[uann] / ocaml_yacc / parser.mly
1 /* File parser.mly */
2 %token <string> INT
3 %token <string> ID
4 %token PLUS MINUS TIMES DIV
5 %token LPAREN RPAREN ASSIGN IN IMPLY FUNC
6 %token EOL
7 %right IMPLY FUNC
8 %left PLUS MINUS/* lowest precedence */
9 %left TIMES DIV /* medium precedence */
10 %nonassoc UMINUS/* highest precedence */
11 %start main /* the entry point */
12 %type <Ast.ast> main
13 %%
14 main:
15 blocks EOL{ $1 }
16 ;
17 blocks:
18 block{ $1 }
19 |blocks block{match $1 with Ast.Node x -> Ast.Node (x @ [$2])
20 | Ast.Int x -> Ast.Node[$1; $2] | Ast.Leaf x -> Ast.Node [$1; $2]} /* It Must not be entered */
21 ;
22
23 block:
24 expr {$1}
25 | let_bind {$1}
26 ;
27
28 let_bind:
29 | typ ID ASSIGN expr IN block {Ast.Node [Ast.Leaf "%let"; Ast.Node[$1; Ast.Leaf $2; $4]; $6]}
30 ;
31
32 typ:
33 | ID {Ast.Leaf $1}
34 | typ IMPLY typ {Ast.Node [Ast.Leaf "->"; $1 ; $3]}
35
36 expr:
37 app_expr {$1}
38 | bin_expr {$1}
39 | lam_expr{$1}
40 ;
41
42 lam_expr:
43 arg FUNC block {Ast.Node [Ast.Leaf "%lambda"; $1 ; $3]}
44 ;
45 arg:
46 typ ID { Ast.Node [$1;Ast.Leaf $2] }
47 ;
48
49 app_expr:
50 expr expr { Ast.Node [ Ast.Leaf "%apply"; $1; $2] }
51 ;
52 ;
53 bin_expr:
54 INT { Ast.Int (int_of_string $1)}
55 | ID { Ast.Leaf $1 }
56 | LPAREN expr RPAREN { $2 }
57 | expr PLUS expr { Ast.Node [ Ast.Leaf "+"; $1; $3] }
58 | expr MINUS expr { Ast.Node[ Ast.Leaf "-"; $1 ; $3] }
59 | expr TIMES expr { Ast.Node[ Ast.Leaf "*"; $1 ;$3] }
60 | expr DIV expr { Ast.Node[ Ast.Leaf "/"; $1; $3] }
61 | MINUS expr %prec UMINUS { Ast.Node[ Ast.Leaf "-" ; $2] }
62 ;
63