tuned
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 23 Nov 2012 21:21:27 +0000
changeset 75 898c25a4e399
parent 74 8f85d1f61663
child 76 373cf55a3ca5
tuned
fib.while
hw08.tex
matcher.scala
while1.scala
--- a/fib.while	Fri Nov 23 19:31:37 2012 +0000
+++ b/fib.while	Fri Nov 23 21:21:27 2012 +0000
@@ -1,12 +1,22 @@
-{ n := 9;
-     minus1 := 0;
-     minus2 := 1;
-     temp := 0;
-     while n > 0 do  {
+/*
+  
+  Fibonacci Program
+
+  input: n
+  output: fib_res
+
+*/
+
+n := 9;
+minus1 := 0;
+minus2 := 1;
+temp := 0;
+while n > 0 do  {
        temp := minus2;
        minus2 := minus1 + minus2;
        minus1 := temp;
        n := n - 1
-     };
-     fib_res := minus2
-}
+};
+fib_res := minus2;
+print fib_res 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hw08.tex	Fri Nov 23 21:21:27 2012 +0000
@@ -0,0 +1,68 @@
+\documentclass{article}
+\usepackage{charter}
+\usepackage{hyperref}
+\usepackage{amssymb}
+\usepackage{amsmath}
+\usepackage{tikz}
+\usetikzlibrary{automata}
+
+\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
+
+\begin{document}
+
+\section*{Homework 8}
+
+\begin{enumerate}
+\item Suppose the following grammar for the WHILE-language:
+
+\begin{center}
+\begin{tabular}{l}
+$S \rightarrow N\cdot P$\\
+$P \rightarrow V\cdot N$\\
+$N \rightarrow N\cdot N$\\
+$N \rightarrow A \cdot N$\\
+$N \rightarrow \texttt{student} \;|\; \texttt{trainer} \;|\; \texttt{team} \;|\; \texttt{trains}$\\
+$V \rightarrow \texttt{trains} \;|\; \texttt{team}$\\
+$A \rightarrow \texttt{The} \;|\; \texttt{the}$\\
+\end{tabular}
+\end{center}
+
+
+\item Consider the following grammar 
+
+\begin{center}
+\begin{tabular}{l}
+$S \rightarrow N\cdot P$\\
+$P \rightarrow V\cdot N$\\
+$N \rightarrow N\cdot N$\\
+$N \rightarrow A \cdot N$\\
+$N \rightarrow \texttt{student} \;|\; \texttt{trainer} \;|\; \texttt{team} \;|\; \texttt{trains}$\\
+$V \rightarrow \texttt{trains} \;|\; \texttt{team}$\\
+$A \rightarrow \texttt{The} \;|\; \texttt{the}$\\
+\end{tabular}
+\end{center}
+
+where $S$ is the start symbol and $S$, $P$, $N$, $V$ and $A$ are non-terminals.
+Using the CYK-algorithm, check whether or not the following string can be parsed
+by the grammar:
+
+\begin{center}
+\texttt{The trainer trains the student team}
+\end{center}
+
+\item {\bf (Optional)} The task is to match strings where the letters are in alphabetical order---for example, 
+\texttt{abcfjz} would pass, but \texttt{acb} would not. Whitespace should be ignored---for example
+\texttt{ab c d} should pass. The point is to try to get the regular expression as short as possible!
+See:
+
+\begin{center}
+\url{http://callumacrae.github.com/regex-tuesday/challenge11.html}
+\end{center}
+\end{enumerate}
+
+\end{document}
+
+%%% Local Variables: 
+%%% mode: latex
+%%% TeX-master: t
+%%% End: 
--- a/matcher.scala	Fri Nov 23 19:31:37 2012 +0000
+++ b/matcher.scala	Fri Nov 23 21:21:27 2012 +0000
@@ -4,11 +4,12 @@
 
 case object NULL extends Rexp
 case object EMPTY extends Rexp
+case object ALLC extends Rexp            // recognises any character
 case class CHAR(c: Char) extends Rexp
 case class ALT(r1: Rexp, r2: Rexp) extends Rexp
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
 case class STAR(r: Rexp) extends Rexp
-case class NOT(r: Rexp) extends Rexp
+case class NOT(r: Rexp) extends Rexp     // negation of a regular expression
 
 
 // nullable function: tests whether the regular 
@@ -16,6 +17,7 @@
 def nullable (r: Rexp) : Boolean = r match {
   case NULL => false
   case EMPTY => true
+  case ALLC => false
   case CHAR(_) => false
   case ALT(r1, r2) => nullable(r1) || nullable(r2)
   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
@@ -28,6 +30,7 @@
 def no_more (r: Rexp) : Boolean = r match {
   case NULL => true
   case EMPTY => false
+  case ALLC => false
   case CHAR(_) => false
   case ALT(r1, r2) => no_more(r1) && no_more(r2)
   case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
@@ -39,7 +42,9 @@
 // derivative of a regular expression w.r.t. a character
 def der (c: Char, r: Rexp) : Rexp = r match {
   case NULL => NULL
-  case EMPTY => NULL  case CHAR(d) => if (c == d) EMPTY else NULL
+  case EMPTY => NULL  
+  case ALLC => EMPTY
+  case CHAR(d) => if (c == d) EMPTY else NULL
   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
   case SEQ(r1, r2) => 
     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/while1.scala	Fri Nov 23 21:21:27 2012 +0000
@@ -0,0 +1,174 @@
+// A parser and evaluator for teh while language
+// 
+//:load matcher.scala
+//:load parser3.scala
+
+// some regular expressions
+val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
+val DIGIT = RANGE("0123456789")
+val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
+val NUM = PLUS(DIGIT)
+val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "print") 
+val SEMI: Rexp = ";"
+val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!", "<", ">")
+val WHITESPACE = PLUS(RANGE(" \n"))
+val RPAREN: Rexp = ")"
+val LPAREN: Rexp = "("
+val BEGIN: Rexp = "{"
+val END: Rexp = "}"
+val COMMENT = SEQS("/*", NOT(SEQS(STAR(ALLC), "*/", STAR(ALLC))), "*/")
+
+// tokens for classifying the strings that have been recognised
+abstract class Token
+case object T_WHITESPACE extends Token
+case object T_COMMENT extends Token
+case object T_SEMI extends Token
+case object T_LPAREN extends Token
+case object T_RPAREN extends Token
+case object T_BEGIN extends Token
+case object T_END extends Token
+case class T_ID(s: String) extends Token
+case class T_OP(s: String) extends Token
+case class T_NUM(s: String) extends Token
+case class T_KWD(s: String) extends Token
+
+val lexing_rules: List[Rule[Token]] = 
+  List((KEYWORD, (s) => T_KWD(s.mkString)),
+       (ID, (s) => T_ID(s.mkString)),
+       (OP, (s) => T_OP(s.mkString)),
+       (NUM, (s) => T_NUM(s.mkString)),
+       (SEMI, (s) => T_SEMI),
+       (LPAREN, (s) => T_LPAREN),
+       (RPAREN, (s) => T_RPAREN),
+       (BEGIN, (s) => T_BEGIN),
+       (END, (s) => T_END),
+       (WHITESPACE, (s) => T_WHITESPACE),
+       (COMMENT, (s) => T_COMMENT))
+
+// the tokenizer
+val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE, T_COMMENT))
+
+// the abstract syntax trees
+abstract class Stmt
+abstract class AExp
+abstract class BExp 
+type Block = List[Stmt]
+case object Skip extends Stmt
+case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
+case class While(b: BExp, bl: Block) extends Stmt
+case class Assign(s: String, a: AExp) extends Stmt
+case class Print(s: String) extends Stmt
+
+case class Var(s: String) extends AExp
+case class Num(i: Int) extends AExp
+case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
+
+case object True extends BExp
+case object False extends BExp
+case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
+
+// atomic parsers
+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)
+
+case object NumParser extends Parser[List[Token], Int] {
+  def parse(ts: List[Token]) = ts match {
+    case T_NUM(s)::ts => Set((s.toInt, 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 ()
+  }
+}
+
+
+// arithmetic expressions
+lazy val AExp: Parser[List[Token], AExp] = 
+  (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
+  (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
+lazy val T: Parser[List[Token], AExp] = 
+  (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
+lazy val F: Parser[List[Token], AExp] = 
+  (T_LPAREN ~> AExp <~ T_RPAREN) || 
+  IdParser ==> Var || 
+  NumParser ==> Num
+
+// boolean expressions
+lazy val BExp: Parser[List[Token], BExp] = 
+  (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
+  (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || 
+  (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || 
+  (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || 
+  (T_KWD("true") ==> ((_) => True)) || 
+  (T_KWD("false") ==> ((_) => False: BExp))
+
+lazy val Stmt: Parser[List[Token], Stmt] =
+  (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
+  (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
+  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
+    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
+  (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } || 
+  (T_KWD("print") ~ IdParser) ==> { case (x, y) => Print(y) } 
+
+lazy val Stmts: Parser[List[Token], Block] =
+  (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
+  (Stmt ==> ((s) => List(s) : Block))
+
+lazy val Block: Parser[List[Token], Block] =
+  (T_BEGIN ~> Stmts <~ T_END) || 
+  (Stmt ==> ((s) => List(s)))
+
+// interpreter
+type Env = Map[String, Int]
+
+def eval_bexp(b: BExp, env: Env) : Boolean = b match {
+  case True => true
+  case False => false
+  case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
+  case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
+  case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
+  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
+}
+
+def eval_aexp(a: AExp, env : Env) : Int = a match {
+  case Num(i) => i
+  case Var(s) => env(s)
+  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
+  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
+  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
+}
+
+def eval_stmt(s: Stmt, env: Env) : Env = s match {
+  case Skip => env
+  case Assign(x, a) => env + (x -> eval_aexp(a, env))
+  case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
+  case While(b, bl) => 
+    if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
+    else env
+  case Print(x) => { println(env(x)); env }
+}
+
+def eval_bl(bl: Block, env: Env) : Env = bl match {
+  case Nil => env
+  case s::bl => eval_bl(bl, eval_stmt(s, env))
+}
+
+def eval_prog(name: String) : Env = {
+  val tks = Tok.fromFile(name)
+  val ast = Stmts.parse_all(tks).head
+  eval_bl(ast, Map.empty)
+}
+
+
+//examples
+
+eval_prog("fib.while")