annotate lab2/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 /* lab2/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 %{
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Keiko
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 open Dict
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 open Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 %}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 %token <Dict.ident> IDENT
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 %token <Keiko.op> MULOP ADDOP RELOP MONOP
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 %token <int> NUMBER BOOLCONST
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 /* punctuation */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 %token SEMI DOT COLON LPAR RPAR COMMA
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 %token EQUAL MINUS ASSIGN EOF BADTOK
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 %token SUB BUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 /* keywords */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 %token BEGIN DO ELSE END IF
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 %token THEN VAR WHILE PRINT NEWLINE
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 %token ARRAY OF
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 %token INTEGER BOOLEAN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 %type <Tree.program> program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 %start program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 %%
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 program :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 decls BEGIN stmts END DOT { Program ($1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 decls :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 /* empty */ { [] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | decl 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 decl :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 VAR name_list COLON typexp SEMI { Decl ($2, $4) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 name_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 name { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | name COMMA name_list { $1 :: $3 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 typexp :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 INTEGER { Integer }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | BOOLEAN { Boolean }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | ARRAY NUMBER OF typexp { Array ($2, $4) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 stmts :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 stmt_list { seq $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 stmt_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 stmt { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | stmt SEMI stmt_list { $1 :: $3 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 stmt :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 /* empty */ { Skip }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 | variable ASSIGN expr { Assign ($1, $3) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | PRINT expr { Print $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 | NEWLINE { Newline }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 | IF expr THEN stmts END { IfStmt ($2, $4, Skip) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 | IF expr THEN stmts ELSE stmts END { IfStmt ($2, $4, $6) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 | WHILE expr DO stmts END { WhileStmt ($2, $4) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 expr :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 simple { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 | expr RELOP simple { makeExpr (Binop ($2, $1, $3)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 | expr EQUAL simple { makeExpr (Binop (Eq, $1, $3)) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 simple :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 term { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 | simple ADDOP term { makeExpr (Binop ($2, $1, $3)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 | simple MINUS term { makeExpr (Binop (Minus, $1, $3)) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 term :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 factor { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 | term MULOP factor { makeExpr (Binop ($2, $1, $3)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 factor :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 variable { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 | NUMBER { makeExpr (Constant ($1, Integer)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 | BOOLCONST { makeExpr (Constant ($1, Boolean)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 | MONOP factor { makeExpr (Monop ($1, $2)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 | MINUS factor { makeExpr (Monop (Uminus, $2)) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 | LPAR expr RPAR { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 variable :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 name { makeExpr (Variable $1) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 | variable SUB expr BUS { makeExpr (Sub ($1, $3)) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 name :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 IDENT { makeName $1 !Lexer.lineno } ;