diff lab0/parser.mly @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lab0/parser.mly	Thu Oct 05 08:04:15 2017 +0100
@@ -0,0 +1,36 @@
+/* lab1/parser.mly */
+/* Copyright (c) 2017 J. M. Spivey */
+
+%{
+open Tree
+%}
+
+%token <string>         IDENT
+%token <float>          NUMBER 
+%token                  PLUS MINUS TIMES DIVIDE OPEN CLOSE EQUAL EOF BADTOK
+
+%type <string * Tree.expr>  equation
+
+%start                  equation
+
+%%
+
+equation :
+    expr EOF                            { ("it", $1) }
+  | IDENT EQUAL expr EOF                { ($1, $3) } ;
+
+expr :
+    term                                { $1 }
+  | expr PLUS term                      { Binop (Plus, $1, $3) }
+  | expr MINUS term                     { Binop (Minus, $1, $3) } ;
+
+term :
+    factor                              { $1 }
+  | term TIMES factor                   { Binop (Times, $1, $3) }
+  | term DIVIDE factor                  { Binop (Divide, $1, $3) } ;
+
+factor :
+    NUMBER                              { Number $1 }
+  | IDENT                               { Variable $1 }
+  | OPEN expr CLOSE                     { $2 } ;
+