annotate lab3/parser.mly @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /* lab3/parser.mly */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 /* Copyright (c) 2017 J. M. Spivey */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 %token<string> IDENT
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 %token<int> NUMBER
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 %token<Keiko.op> MONOP MULOP ADDOP RELOP
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 %token MINUS LPAR RPAR COMMA SEMI DOT ASSIGN EOF BADTOK
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 %token BEGIN END VAR PRINT IF THEN ELSE WHILE DO PROC RETURN NEWLINE
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 %start program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 %type<Tree.program> program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 %{
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 open Keiko
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 open Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 %}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 %%
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 program :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 block DOT { Program $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 block :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 var_decl proc_decls BEGIN stmts END { Block ($1, $2, $4) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 var_decl :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 /* empty */ { [] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 | VAR ident_list SEMI { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 ident_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 IDENT { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 | IDENT COMMA ident_list { $1::$3 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 proc_decls :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 /* empty */ { [] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | proc_decl proc_decls { $1::$2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 proc_decl :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 PROC name formals SEMI block SEMI { Proc ($2, $3, $5) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 formals :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 LPAR RPAR { [] } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | LPAR ident_list RPAR { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 stmts :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 stmt_list { seq $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 stmt_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 stmt { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | stmt SEMI stmt_list { $1::$3 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 stmt :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 /* empty */ { Skip }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | name ASSIGN expr { Assign ($1, $3) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | RETURN expr { Return $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | IF expr THEN stmts END { IfStmt ($2, $4, Skip) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 | IF expr THEN stmts ELSE stmts END { IfStmt ($2, $4, $6) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 | WHILE expr DO stmts END { WhileStmt ($2, $4) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 | PRINT expr { Print $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | NEWLINE { Newline } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 actuals :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 LPAR RPAR { [] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 | LPAR expr_list RPAR { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 expr_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 expr { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 | expr COMMA expr_list { $1::$3 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 expr :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 simple { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 | expr RELOP simple { Binop ($2, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 simple :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 term { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 | simple ADDOP term { Binop ($2, $1, $3) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 | simple MINUS term { Binop (Minus, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 term :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 factor { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 | term MULOP factor { Binop ($2, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 factor :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 NUMBER { Constant $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 | name { Variable $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 | name actuals { Call ($1, $2) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 | MONOP factor { Monop ($1, $2) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 | MINUS factor { Monop (Uminus, $2) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 | LPAR expr RPAR { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 name :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 IDENT { makeName $1 !Lexer.lineno } ;