progs/fun_parser.scala
changeset 644 b4f5714485e1
child 645 30943d5491b6
equal deleted inserted replaced
643:08375ca3874e 644:b4f5714485e1
       
     1 // A Small Compiler for a Simple Functional Language
       
     2 // (includes a lexer and a parser)
       
     3 
       
     4 object Fun_Parser {
       
     5 
       
     6 import scala.language.implicitConversions    
       
     7 import scala.language.reflectiveCalls
       
     8 import java.io._
       
     9 
       
    10 abstract class Token extends Serializable 
       
    11 case object T_SEMI extends Token
       
    12 case object T_COMMA extends Token
       
    13 case object T_LPAREN extends Token
       
    14 case object T_RPAREN extends Token
       
    15 case class T_ID(s: String) extends Token
       
    16 case class T_OP(s: String) extends Token
       
    17 case class T_NUM(n: Int) extends Token
       
    18 case class T_KWD(s: String) extends Token
       
    19 
       
    20 
       
    21 // Parser combinators
       
    22 //    type parameter I needs to be of Seq-type
       
    23 //
       
    24 abstract class Parser[I, T](implicit ev: I => Seq[_]) {
       
    25   def parse(ts: I): Set[(T, I)]
       
    26 
       
    27   def parse_all(ts: I) : Set[T] =
       
    28     for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
       
    29 
       
    30   def parse_single(ts: I) : T = parse_all(ts).toList match {
       
    31     case List(t) => t
       
    32     case _ => { println ("Parse Error\n") ; sys.exit(-1) }
       
    33   }
       
    34 }
       
    35 
       
    36 // convenience for writing grammar rules
       
    37 case class ~[+A, +B](_1: A, _2: B)
       
    38 
       
    39 class SeqParser[I, T, S](p: => Parser[I, T], 
       
    40                          q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] {
       
    41   def parse(sb: I) = 
       
    42     for ((head1, tail1) <- p.parse(sb); 
       
    43          (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
       
    44 }
       
    45 
       
    46 class AltParser[I, T](p: => Parser[I, T], 
       
    47                       q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] {
       
    48   def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
       
    49 }
       
    50 
       
    51 class FunParser[I, T, S](p: => Parser[I, T], 
       
    52                          f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] {
       
    53   def parse(sb: I) = 
       
    54     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
       
    55 }
       
    56 
       
    57 // convenient combinators
       
    58 implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
       
    59   def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
       
    60   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
       
    61   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
       
    62 }
       
    63 
       
    64 def ListParser[I, T, S](p: => Parser[I, T], 
       
    65                         q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
       
    66   (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] } ||
       
    67   (p ==> ((s) => List(s)))
       
    68 }
       
    69 
       
    70 case class TokParser(tok: Token) extends Parser[List[Token], Token] {
       
    71   def parse(ts: List[Token]) = ts match {
       
    72     case t::ts if (t == tok) => Set((t, ts)) 
       
    73     case _ => Set ()
       
    74   }
       
    75 }
       
    76 
       
    77 implicit def token2tparser(t: Token) = TokParser(t)
       
    78 
       
    79 implicit def TokOps(t: Token) = new {
       
    80   def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
       
    81   def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f)
       
    82   def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
       
    83 }
       
    84 
       
    85 case object NumParser extends Parser[List[Token], Int] {
       
    86   def parse(ts: List[Token]) = ts match {
       
    87     case T_NUM(n)::ts => Set((n, ts)) 
       
    88     case _ => Set ()
       
    89   }
       
    90 }
       
    91 
       
    92 case object IdParser extends Parser[List[Token], String] {
       
    93   def parse(ts: List[Token]) = ts match {
       
    94     case T_ID(s)::ts => Set((s, ts)) 
       
    95     case _ => Set ()
       
    96   }
       
    97 }
       
    98 
       
    99 
       
   100 
       
   101 // Abstract syntax trees for the Fun language
       
   102 abstract class Exp extends Serializable 
       
   103 abstract class BExp extends Serializable 
       
   104 abstract class Decl extends Serializable 
       
   105 
       
   106 case class Def(name: String, args: List[String], body: Exp) extends Decl
       
   107 case class Main(e: Exp) extends Decl
       
   108 
       
   109 case class Call(name: String, args: List[Exp]) extends Exp
       
   110 case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
       
   111 case class Write(e: Exp) extends Exp
       
   112 case class Var(s: String) extends Exp
       
   113 case class Num(i: Int) extends Exp
       
   114 case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
       
   115 case class Sequence(e1: Exp, e2: Exp) extends Exp
       
   116 case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
       
   117 
       
   118 
       
   119 
       
   120 // Grammar Rules for the Fun language
       
   121 
       
   122 // arithmetic expressions
       
   123 lazy val Exp: Parser[List[Token], Exp] = 
       
   124   (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==>
       
   125     { case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } ||
       
   126   (M ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ y => Sequence(x, y): Exp } || M
       
   127 lazy val M: Parser[List[Token], Exp] =
       
   128   (T_KWD("write") ~ L) ==> { case _ ~ y => Write(y): Exp } || L
       
   129 lazy val L: Parser[List[Token], Exp] = 
       
   130   (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } ||
       
   131   (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T  
       
   132 lazy val T: Parser[List[Token], Exp] = 
       
   133   (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || 
       
   134   (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || 
       
   135   (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F
       
   136 lazy val F: Parser[List[Token], Exp] = 
       
   137   (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> 
       
   138     { case x ~ _ ~ z ~ _ => Call(x, z): Exp } ||
       
   139   (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || 
       
   140   IdParser ==> { case x => Var(x): Exp } || 
       
   141   NumParser ==> { case x => Num(x): Exp }
       
   142 
       
   143 // boolean expressions
       
   144 lazy val BExp: Parser[List[Token], BExp] = 
       
   145   (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
       
   146   (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
       
   147   (Exp ~ T_OP("<") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
       
   148   (Exp ~ T_OP(">") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", z, x): BExp } || 
       
   149   (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || 
       
   150   (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp }  
       
   151 
       
   152 lazy val Defn: Parser[List[Token], Decl] =
       
   153    (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==>
       
   154      { case _ ~ y ~ _ ~ w ~ _ ~ _ ~ r => Def(y, w, r): Decl }
       
   155 
       
   156 lazy val Prog: Parser[List[Token], List[Decl]] =
       
   157   (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } ||
       
   158   (Exp ==> ((s) => List(Main(s)) : List[Decl]))
       
   159 
       
   160 
       
   161 
       
   162 // Reading tokens and Writing parse trees
       
   163 
       
   164 def serialise[T](fname: String, data: T) = {
       
   165   val out = new ObjectOutputStream(new FileOutputStream(fname))
       
   166   out.writeObject(data)
       
   167   out.close
       
   168 }
       
   169 
       
   170 def deserialise[T](fname: String) : T = {
       
   171   val in = new ObjectInputStream(new FileInputStream(fname))
       
   172   val data = in.readObject.asInstanceOf[T]
       
   173   in.close
       
   174   data
       
   175 }
       
   176 
       
   177 
       
   178 def main(args: Array[String]) = {
       
   179   val fname = args(0)
       
   180   val pname = fname.stripSuffix(".tks") ++ ".prs"
       
   181   val tks = deserialise[List[Token]](fname)
       
   182   serialise(pname, Prog.parse_single(tks))
       
   183 
       
   184   // testing whether read-back is working
       
   185   //val ptree = deserialise[List[Decl]](pname)
       
   186   //println(s"Reading back from ${pname}:\n${ptree.mkString("\n")}")  
       
   187 }
       
   188 
       
   189 }