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