diff -r 08375ca3874e -r b4f5714485e1 progs/fun_parser.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/fun_parser.scala Wed Oct 02 02:09:48 2019 +0100 @@ -0,0 +1,189 @@ +// A Small Compiler for a Simple Functional Language +// (includes a lexer and a parser) + +object Fun_Parser { + +import scala.language.implicitConversions +import scala.language.reflectiveCalls +import java.io._ + +abstract class Token extends Serializable +case object T_SEMI extends Token +case object T_COMMA extends Token +case object T_LPAREN extends Token +case object T_RPAREN extends Token +case class T_ID(s: String) extends Token +case class T_OP(s: String) extends Token +case class T_NUM(n: Int) extends Token +case class T_KWD(s: String) extends Token + + +// Parser combinators +// type parameter I needs to be of Seq-type +// +abstract class Parser[I, T](implicit ev: I => Seq[_]) { + def parse(ts: I): Set[(T, I)] + + def parse_all(ts: I) : Set[T] = + for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head + + def parse_single(ts: I) : T = parse_all(ts).toList match { + case List(t) => t + case _ => { println ("Parse Error\n") ; sys.exit(-1) } + } +} + +// convenience for writing grammar rules +case class ~[+A, +B](_1: A, _2: B) + +class SeqParser[I, T, S](p: => Parser[I, T], + q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] { + def parse(sb: I) = + for ((head1, tail1) <- p.parse(sb); + (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2) +} + +class AltParser[I, T](p: => Parser[I, T], + q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] { + def parse(sb: I) = p.parse(sb) ++ q.parse(sb) +} + +class FunParser[I, T, S](p: => Parser[I, T], + f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] { + def parse(sb: I) = + for ((head, tail) <- p.parse(sb)) yield (f(head), tail) +} + +// convenient combinators +implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { + def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) + def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) + def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) +} + +def ListParser[I, T, S](p: => Parser[I, T], + q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = { + (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] } || + (p ==> ((s) => List(s))) +} + +case class TokParser(tok: Token) extends Parser[List[Token], Token] { + def parse(ts: List[Token]) = ts match { + case t::ts if (t == tok) => Set((t, ts)) + case _ => Set () + } +} + +implicit def token2tparser(t: Token) = TokParser(t) + +implicit def TokOps(t: Token) = new { + def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q) + def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f) + def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q) +} + +case object NumParser extends Parser[List[Token], Int] { + def parse(ts: List[Token]) = ts match { + case T_NUM(n)::ts => Set((n, ts)) + case _ => Set () + } +} + +case object IdParser extends Parser[List[Token], String] { + def parse(ts: List[Token]) = ts match { + case T_ID(s)::ts => Set((s, ts)) + case _ => Set () + } +} + + + +// Abstract syntax trees for the Fun language +abstract class Exp extends Serializable +abstract class BExp extends Serializable +abstract class Decl extends Serializable + +case class Def(name: String, args: List[String], body: Exp) extends Decl +case class Main(e: Exp) extends Decl + +case class Call(name: String, args: List[Exp]) extends Exp +case class If(a: BExp, e1: Exp, e2: Exp) extends Exp +case class Write(e: Exp) extends Exp +case class Var(s: String) extends Exp +case class Num(i: Int) extends Exp +case class Aop(o: String, a1: Exp, a2: Exp) extends Exp +case class Sequence(e1: Exp, e2: Exp) extends Exp +case class Bop(o: String, a1: Exp, a2: Exp) extends BExp + + + +// Grammar Rules for the Fun language + +// arithmetic expressions +lazy val Exp: Parser[List[Token], Exp] = + (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==> + { case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } || + (M ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ y => Sequence(x, y): Exp } || M +lazy val M: Parser[List[Token], Exp] = + (T_KWD("write") ~ L) ==> { case _ ~ y => Write(y): Exp } || L +lazy val L: Parser[List[Token], Exp] = + (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } || + (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T +lazy val T: Parser[List[Token], Exp] = + (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || + (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || + (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F +lazy val F: Parser[List[Token], Exp] = + (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> + { case x ~ _ ~ z ~ _ => Call(x, z): Exp } || + (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || + IdParser ==> { case x => Var(x): Exp } || + NumParser ==> { case x => Num(x): Exp } + +// boolean expressions +lazy val BExp: Parser[List[Token], BExp] = + (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || + (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || + (Exp ~ T_OP("<") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || + (Exp ~ T_OP(">") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", z, x): BExp } || + (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || + (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp } + +lazy val Defn: Parser[List[Token], Decl] = + (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==> + { case _ ~ y ~ _ ~ w ~ _ ~ _ ~ r => Def(y, w, r): Decl } + +lazy val Prog: Parser[List[Token], List[Decl]] = + (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } || + (Exp ==> ((s) => List(Main(s)) : List[Decl])) + + + +// Reading tokens and Writing parse trees + +def serialise[T](fname: String, data: T) = { + val out = new ObjectOutputStream(new FileOutputStream(fname)) + out.writeObject(data) + out.close +} + +def deserialise[T](fname: String) : T = { + val in = new ObjectInputStream(new FileInputStream(fname)) + val data = in.readObject.asInstanceOf[T] + in.close + data +} + + +def main(args: Array[String]) = { + val fname = args(0) + val pname = fname.stripSuffix(".tks") ++ ".prs" + val tks = deserialise[List[Token]](fname) + serialise(pname, Prog.parse_single(tks)) + + // testing whether read-back is working + //val ptree = deserialise[List[Decl]](pname) + //println(s"Reading back from ${pname}:\n${ptree.mkString("\n")}") +} + +} \ No newline at end of file