Csc 4330/6330, Programming Language Concepts (Summer 2020)
Homework 6 (Due: July 28, Tuesday; Late Submission: July 30, Thursday)
Handin under assignment 6sudo handin4330 6 WAE.scala
sudo handin4330 6 WAE2Driver.py WAE2DriverLexer.py WAE2DriverParser.py
- (WAE.scala)
Template files: WAE.scala,
Driver.scala
Consider the following Scala code to implement the WAE language evaluator. In this part, we are not bulding Lexer/Parser but just the evaluator. The case-class WAENode stores the WAE-expression. Not all fields of the case-class are used for every type of WAE-expression.
object WAE { abstract class Node case class WAENode( nodeType: String, // will be "num", "id", "+", "-", "*", "/", "if", or "with" numValue: Double = 0.0, // used to store num value idValue: String = "", // used to store id value child1: WAENode = null, // used for if, with, +, -, *, / child2: WAENode = null, // used for if, +, -, *, / child3: WAENode = null, // used for if vars: List[(String,WAENode)] = List[(String,WAENode)]() // used for with ) extends Node def eval(e: WAENode, d: Map[String,Double]): Double = ??? }
We will assume that the WAE-expressions are well formed and that they are available in Scala variables as shown in Driver.scala. For the two semantic error situations (uninstantiated variable and divide by zero), you can simply throw an exception:throw new Exception("Uninstantiated variable error") throw new Exception("Divide by zero error")
You have to complete the definition of eval. - 50 BONUS POINTS!! (WAE2Driver.py WAE2DriverLexer.py WAE2DriverParser.py)
Driver Program Generation using PLYUsing Python and PLY write a program that reads a text file containing one WAE expression per line and generates the Driver.scala file that when run evaluates each of the expressions in the file and prints the value of the WAE expression. We will assume that there is no semantic error in these WAE expressions. A sample input file is shown below:
macbook-pro:toDriver raj$ more in.dat 45; {+ 2 3}; {with {{x 2}{y 3}} {+ x y}};
Here is sample run of the PLY program and the generated output:macbook-pro:toDriver raj$ python3 WAE2Driver.py in.dat > Driver.scala macbook-pro:toDriver raj$ more Driver.scala object Driver extends App { import WAE._ val d = Map[String,Double]() val e0 = WAENode(nodeType="num",numValue=45) println(eval(e0,d)) val e1 = WAENode(nodeType="+", child1=WAENode(nodeType="num",numValue=2) , child2=WAENode(nodeType="num",numValue=3) ) println(eval(e1,d)) val e2 = WAENode(nodeType="with", vars=List(("x",WAENode(nodeType="num",numValue=2) ), ("y",WAENode(nodeType="num",numValue=3) ) ), child1=WAENode(nodeType="+", child1=WAENode(nodeType="id",idValue="x") , child2=WAENode(nodeType="id",idValue="y") ) ) println(eval(e2,d)) }
We can assume the following familiar grammar:
waeStart : wae SEMI wae : NUMBER wae : ID wae : LBRACE PLUS wae wae RBRACE wae : LBRACE MINUS wae wae RBRACE wae : LBRACE TIMES wae wae RBRACE wae : LBRACE DIV wae wae RBRACE wae : LBRACE IF wae wae wae RBRACE wae : LBRACE WITH LBRACE alist RBRACE wae RBRACE alist : LBRACE ID wae RBRACE alist : LBRACE ID wae RBRACE alist