annotate lab1/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 /* lab1/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 Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 %}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 %token <Tree.ident> IDENT
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 %token <Keiko.op> MONOP MULOP ADDOP RELOP
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 %token <int> NUMBER
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 /* punctuation and keywords */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 %token SEMI DOT COLON LPAR RPAR COMMA MINUS VBAR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 %token ASSIGN EOF BADTOK
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 %token BEGIN DO ELSE END IF THEN WHILE PRINT NEWLINE
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 %type <Tree.program> program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 %start program
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 %%
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 program :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 BEGIN stmts END DOT { Program $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 stmts :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 stmt_list { seq $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 stmt_list :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 stmt { [$1] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 | stmt SEMI stmt_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 stmt :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 /* empty */ { Skip }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | name ASSIGN expr { Assign ($1, $3) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | PRINT expr { Print $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | NEWLINE { Newline }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 | IF expr THEN stmts END { IfStmt ($2, $4, Skip) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 | IF expr THEN stmts ELSE stmts END { IfStmt ($2, $4, $6) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | WHILE expr DO stmts END { WhileStmt ($2, $4) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 expr :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 simple { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | expr RELOP simple { Binop ($2, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 simple :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 term { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | simple ADDOP term { Binop ($2, $1, $3) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | simple MINUS term { Binop (Minus, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 term :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 factor { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | term MULOP factor { Binop ($2, $1, $3) } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 factor :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 name { Variable $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 | NUMBER { Constant $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 | MONOP factor { Monop ($1, $2) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | MINUS factor { Monop (Uminus, $2) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 | LPAR expr RPAR { $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 name :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 IDENT { make_name $1 !Lexer.lineno } ;