



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to midterm 2 questions related to the tiger programming language, including a short grammar for a specific language, identifying components of the compiler that need to be changed for specific modifications, and garbage collection considerations in tiger. Additionally, it explains how the tiger interpreter deals with logical and/or expressions and typechecking comparison and addition.
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Many acceptable answers; one was the following: (defparameter g '( (S --> |(| L |)| #'(lambda (o l c) (cons 'ExpList l))) (L --> #'(lambda () nil)) (L --> x O #'(lambda (x o) (cons 'x o))) (O --> #'(lambda () nil)) (O --> , x O #'(lambda (c x o) (cons 'x o)))))
Yes. The DFA looks like this: State 1 (start state) -- Transition on ( to State 2 State 2 -- Transition on x to State 3 -- Transition on ) to State 5 State 3 -- Transition on , to State 4 -- Transition on ) to State 5 State 4 -- Transition on x to State 3 State 5 (final state) -- No outgoing transitions
(defun aug(&rest r)(if (null (cdr r)) (car r) r))
lalr-parser calls aug. More precisely: reduce-cat, a local function within lalr-parser, calls aug.
It is called when a reduction is performed.
r is bound to a list of the results of parsing the constituents of the rhs of the rule.
P. Forbid in grammar. Or T/I: make typechecker/interpreter cause an error here.
P. Parser's augment for () should return nil. Or, have T/I to return nil on empty explist.
P. Have our parser mangle the AST similarly to a&b.
L/P/T/I: We can make true and false reserved words in the lexer, and modify the parser to require them in boolean expressions. This would also require modifications to the typechecker and interpreter to do logic only on booleans. This is the preferred answer. Alternatively, we can make the modifications only to the typechecker/ interpreter and leave true/false as non-reserved tokens.
T/I. We add this function to the initial environment in the typechecker as well as implementing the function in the interpreter.
Garbage collection would start when we try to allocate an array or record and do not have enough memory.
Variable declarations in let statements, function calls, and for loops all allocate space on the stack.
Nothing deallocates memory on the heap, but return from a function as well as the "end" of a for/let all deallocate stack space.
We would need to implement many free()-like statements, one for each type, to implement explicit memory management.
Live identifiers in accessible environments are roots of the garbage collection in tiger.
let type t1={a:int,b:t1} type t2={c:t1} var x:t2:=nil in x.c.b.a end No errors (OK) - program fails at run-time only. --02---- let type t1=int var x:t in x:= 3 end P, line 2: var x:t1 is not a legal variable declaration; it lacks an initializer. --03--- let var x:=5 in x:=3+4^5 end L, line 1: ^ is not a legal token or part of a legal token in Tiger. --04--- let type a= {x:int, y:int} type b= {x:int, y:int} type c=a var i:a :=(x=1,y=2} var j:b :=nil var k:c :=nil in b:=j; k:=j end P, line 4: ( is unmatched. T, line 7: b:=j is illegal because b is a type, not a variable. T, line 7: k:=j is illegal because k and j are different types. --05--- let type intlist ={hd:int, tl:intlist} type b=c type c=b in 1234 end T, line 3/4: b and c are illegal mutually recursive types. --06--- let type intarray = array of intarray var M:intarray := array[10] of M in 5676 end T, line 1: intarray is an illegal recursive type. P, line 2: illegal syntax for array initialization. --07--- let function f(a:int):int=3aa end in print(f(2)); "done" end P, line 1: end token without matching let/in. T, line 2: print is of type string -> void, but f returns int. --08--- 3+4*(("big"<"little")>0)-9) P, line 1: Unmatched ). There is no "T" error in this program.
a) At time t, there are R live nodes and H total cells in the heap. A copying GC divides the heap in two equal sections, so the time to perform the GC is: ( R * c) / ( H /2 - R ) where c is the cost of copying a single cell from one section of the heap to the other. b) If R = H /2, we see that the cost of a GC approaches infinity; in other words, the GC fails to free any memory and must either ask the OS for more memory or exit with an error.
let function q(a,b)= let in a:=3;b:=7 end type ar=array of int var m:=ar [6] of 0 var i:= in q(i, m[i+2]); for i:=0 to 5 do print(chr(m[i]+ ord("0"))) end
m[4] is 7; other elements are 0
m[5] is 7; other elements are 0
All six elements of m are 0
print() requires a string argument, not an int.
q(a,b) is not properly type declared. The declaration should be q(a:int,b:int):int or something similar.