Midterm CSU 670 Fall 2005 October 18, 2005 Question 1: ============================================================ 40 points Writing a simple translator Write a program that translates from the language of class dictionary g1 to the language of the class dictionary g2. g1: Main = String. SPL = Block. Block = "{" List(Declaration) "body" NList(Statement) "}". Declaration = "(" Type Variable "=" Exp ")". Type : Bool | Inte. Bool = "bool". Inte = "int". Variable = Ident. Exp : Simple | Compound. Simple : Variable | Numbe. Numbe = int. Compound = "(" Exp Op Exp ")". Op : Plus | GT. Plus = "+". GT = ">". Statement : Assignment | IfStat | WhileStat. Assignment = "(" Variable "=" Exp ")". IfStat = "(if" Exp Block "else" Block ")". WhileStat = "(while" Exp Block ")". List(S) ~ {S}. NList(S) ~ S {S}. g2: Main = String. SPL = Block. Block = "(" List(Declaration) NList(Statement) ")". Declaration = "(" Type Variable "=" Exp ")". Type : Bool | Inte. Bool = "bool". Inte = "int". Variable = Ident. Exp : Simple | Compound. Simple : Variable | Numbe. Numbe = int. Compound = "(" Exp Op Exp ")". Op : Plus | GT. Plus = "+". GT = ">". Statement : Assignment | IfStat | WhileStat. Assignment = "(" Variable "=" Exp ")". IfStat = "(if" Exp Block "else" Block ")". WhileStat = "(while" Exp Block ")". List(S) ~ "(" {S} ")". NList(S) ~ "(" S {S} ")". Example: { (int x = 10) (bool b = (0 > 0)) body (x = 11) (while b { body ( x = x) (while c { body ( y = y) }) }) } (((int x=10) (bool b=(0>0))) ( (x=11) (while b( ()( (x=x) (while c( ()( (y=y))))))))) White space (spaces and new lines) is not important. Turn in a description of the process you would use to write the program and turn in the important parts of the program. Explain why your program works. Question 2: ============================================================ 10 UNKNOWNs, 4 points per each: 40 points Complete a program Consider again class dictionary g1 from question 1. Write a program that takes an SPL-object as input and prints the depth of all the blocks that are contained in the SPL-object. For example, for the input: { (int x = 10) (bool b = (0 > 0)) body (x = 11) (while b { body ( x = x) (while c { body ( y = y) }) }) } your program should print: {(int x=10)(bool b=(0>0))body(x=11)(while b{body(x=x)(while c{body(y=y)})})} Depth =1 {body(x=x)(while c{body(y=y)})} Depth =2 {body(y=y)} Depth =3 Find the UNKNOWNs below: Main { {{ static UNKNOWN1 cg; public static void main(String args[]) throws Exception { SPL m = SPL.UNKNOWN2(System.in); cg = new UNKNOWN3(true,false); m.depth(); System.out.println("done"); System.out.println(); }} } Block { void print() to * (PrintVisitor); } SPL { {{ void depth(){ Main.cg.traverse(UNKNOWN4,"from UNKNOWN5 to UNKNOWN6",new Visitor() { int dep; public void start() {UNKNOWN7;} void before(UNKNOWN8 host) { UNKNOWN9; host.print(); System.out.println(); System.out.println("Depth =" + dep);} void after(UNKNOWN10 host) { dep--;} } );}; }} } Question 3: ============================================================ 31 UNKNOWNs, 0.5 points each: 15.5 points Parsing Consider again class dictionary g1. The display method: SPL { void display() to * (DisplayVisitor); } prints for this input: {(int x=10)(bool b=(0>0))body(x=11) (while b{body(x=x)(while c{body(y=y)})})} the following output. Find the UNKNOWNs. : SPL ( : UNKNOWN1 ( : UNKNOWN2 { : Nonempty_Declaration_List ( : UNKNOWN3 ( : UNKNOWN4 ( ) : Variable ( : Ident "UNKNOWN5" ) : UNKNOWN6 ( : int "UNKNOWN7" ) ) : Nonempty_Declaration_List ( : UNKNOWN8 ( : UNKNOWN9 ( ) : UNKNOWN10 ( : Ident "UNKNOWN11" ) : UNKNOWN12 ( : UNKNOWN13 ( : int "UNKNOWN14" ) : UNKNOWN15 ( ) : UNKNOWN16 ( : int "UNKNOWN17" ) ) ) ) ) } : Statement_NList { : Nonempty_Statement_NList ( : UNKNOWN18 ( : UNKNOWN19 ( : Ident "UNKNOWN20" ) : UNKNOWN21 ( : int "UNKNOWN22" ) ) : Nonempty_Statement_NList ( : UNKNOWN23 ( : UNKNOWN24 ( : Ident "UNKNOWN25" ) : UNKNOWN26 ( : Declaration_List { } : Statement_NList { : Nonempty_Statement_NList ( : UNKNOWN27 ( : UNKNOWN28 ( : Ident "UNKNOWN29" ) : UNKNOWN30 ( : Ident "UNKNOWN31" ) ) ... Find the UNKNOWNs. Question 4 =========================================================== Question 4.1 5 points For the following class dictionary: Container = List(Item) Capacity Weight. Item : Simple | Container common Weight. Weight = int. Simple = . Capacity = int. List(S) ~ {S}. the Java compiler compiler complains: Error: Left recursion detected: "_Container... --> _Item_List... --> _Nonempty_Item_List... --> _Item... --> _Container..." Propose a simple way to eliminate this left recursion as well as any other ambiguity problems with this class dictionary. Question 4.2 5 points Consider the class dictionary: Container = "container" List(Item) Capacity Weight. Item : Simple | Container common Weight. Weight = int. Simple : Apple | Orange. Apple = "an" "apple". Orange = "an" "orange". Capacity = int. List(S) ~ {S}. This class dictionary has LL(1) problems. Repair them by making a minimal number of changes that make the class dictionary LL(1). Question 4.3 10 points Consider the following class dictionary. A = List(B). B = List(C). C = List(D). D = "d". List(S) ~ {S} . It is ambiguous. Propose the simplest way to make this class dictionary non-ambiguous, more specifically: LL(1). Question 5: ============================================================ 13 UNKNOWNs, 2 points per UNKNOWN: 26 UNKNOWNs Class dictionary design Design a class dictionary that accepts the following input and similar inputs. Find the UNKNOWNs below. { A /+/ B B / C C /+/ D bypassing -> A b B C /+/ E bypassing -> A b2 B C / F F /+/ G through -> X y Y G /+/ H bypassing -> A1 b2 B2 H / J J /+/ * } Graph = List(UNKNOWN1) EOF. UNKNOWN2 = UNKNOWN3 Op UNKNOWN4 [Constraint]. Op : UNKNOWN5. Flexible = "/+/". Strict = UNKNOWN6. V : VertexName | Star. Star = UNKNOWN7. Constraint : UNKNOWN8 | UNKNOWN9 common UNKNOWN10. Positive = "through" . Negative = "bypassing" . Triple = UNKNOWN11 VertexName LabelName VertexName. LabelName = Ident. List(S) ~ "UNKNOWN12" {S} "UNKNOWN13". VertexName = Ident.