https://danielschlegel.org/wp/teaching/csc344-spring-2021/assignment-3/ The link to lab instructions. I'm having a trouble writing the parser and creating the tree. I've attached what I have written...

1 answer below »
https://danielschlegel.org/wp/teaching/csc344-spring-2021/assignment-3/ The link to lab instructions. I'm having a trouble writing the parser and creating the tree. I've attached what I have written so far
Answered 6 days AfterApr 01, 2021

Answer To: https://danielschlegel.org/wp/teaching/csc344-spring-2021/assignment-3/ The link to lab...

Abr Writing answered on Apr 08 2021
139 Votes
import scala.util.parsing.combinator._
abstract class Tree
case class S(e: Tree) extends Tree
case class E(t:
Tree, e2: Tree) extends Tree
case class E2(e: Tree) extends Tree
case class T(f: Tree, t2: Tree) extends Tree
case class T2(f: Tree, t2: Tree) extends Tree
case class F(a: Tree, f2: Tree) extends Tree
case class F2(f2: Tree) extends Tree
case class A(e: Tree) extends Tree
case class C(content: String) extends Tree
case class NULL() extends Tree
class patternParser extends JavaTokenParsers{
def s: Parser[Tree] = e ^^ {case ex => S(ex)}
def e: Parser[Tree] = t ~ e2 ^^ {case te ~ ex2 => if (ex2.isInstanceOf[NULL]) te else E(te, ex2)}
def e2: Parser[Tree] = or ~ e ^^ {case or ~ ex => E2(ex)} | "" ^^ {case "" => NULL()}
def t: Parser[Tree] = f ~ t2 ^^ {case fe ~ te2 => if (te2.isInstanceOf[NULL]) fe else T(fe, te2)}
def t2: Parser[Tree] = f ~ t2 ^^ {case fe ~ te2 => T2(fe, te2)} | "" ^^ {case "" => NULL()}
def f: Parser[Tree] = a ~ f2 ^^ {case ae ~ fe2 => if (fe2.isInstanceOf[NULL]) ae else F(ae, fe2)}
def f2: Parser[Tree] = optional ~ f2 ^^ {case opt ~ fe2 => F2(fe2)} | "" ^^...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here