FACIT TILL/TO DVG C01 OMTENTAMEN I PROGRAMSPRÅK PROGRAMMING LANGUAGES RESIT EXAMINATION 120824 08:15-13: 15 Ansvarig Lärare: Donald F. Ross Hjälpmedel: Bilaga A: BNF-definition En ordbok: studentenshemspråk engelska Betygsgräns: Kurs: Max 60p, Med beröm godkänd 50p, Icke utan beröm godkänd 40p, Godkänd 30p (varav minimum 20p från tentan, 10p från labbarna) Tenta: Max 40p, Med beröm godkänd 34p, Icke utan beröm godkänd 27p, Godkänd 20p Labbarna: Max 20p, Med beröm godkänd 18p, Icke utan beröm godkänd 14p, Godkänd 10p SKRIV TYDLIGT LÄS UPPGIFTERNA NOGGRANT Course Director: Donald F. Ross Help information: Appendix A: BNF-definition A dictionary: the student s home language English Grading levels: Course: Max 60p, pass with special distinction 50p, pass with distinction 40p, pass 30p (of which a minimum 20p from the exam, 10p from the labs) Exam: Max 40p, grade 5: 34p-40p, grade 4: 27p-33p, grade 3: 20p-26p Labs: Max 20p, grade 5: 18p-20p, grade 4: 14p-17p, grade 3: 10p-13p Write legibly read all questions carefully DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 1 av 15
Uppgift 1 / Question 1 Ange kortfattade svar på följande uppgifter: (a) Ge en definition av (i) aktuell parameter samt (ii) formell parameter (b) Vad används statiskt minne till? (c) Vilken funktion har de statiska och dynamiska pekarna i en stack? (d) Varför heter en prediktiv parser sådant? (e) Vad är ett svagtypat programmeringsspråk? Write short answers to the following questions: (a) Give a definition of (i) actual parameter and (ii) formal parameter (b) What is static memory used for? (c) What is the function of the dynamic and static pointers in a stack? (d) Why is a predictive parser called so? (e) What is a weakly typed programming language? Totalt 5p Total 5p 1. Give a definition of (i) actual parameter and (ii) formal parameter Actual parameter an expression Formal parametr an identifier 2. What is static memory used for? Storing static data objects (declared at the module level or within functions) and code objects 3. What is the function of the dynamic and static pointers in a stack? The dynamic pointer ponts back to the reference environment of the call The static pointer points back to the reference environmemt of the declaration of the function 4. Why is a predictive parser called so? Because based on the input, the parser can decide which construct it is currently handling. For example, using keywords, if processing a statement then the choice of statement can be decided by the keyword ID assignment; IF if statement; WHILE while statement etc More formally, if S ::= A B C then the intersection of First(A), First(B) and First(C) must be empty otherwise the parser cannot choose which construct to process and the grammar is thus ambiguous DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 2 av 15
5. What is a weakly typed programming language? A language in which the data objects may have no fixed type during the program execution. For example in a strongly typed language we have declarations such as A : integer data object A may not be assigned a value of another type. In weakly typed languages, the type of the data object is decided by the value currently bound to the data object. Thus the following sequence may be allowed:- A := 2;.. A := string ; A := 3.1412; A := a ;.. Where the type of A is respectively integer, string, real, character. DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 3 av 15
Uppgift 2 / Question 2 PARSER Hur fungerar parsern i en parsningsprocess? Svar ingående och använda helst exempel. How does the parser work in a parsing process? Discuss in detail and use examples. Totalt 5p Total 5p In for example a Recursive Descent Predictive Parser (RDPP) the only one we have looked at in this course the parser expects a token stream as input. The parser is constructed according to a grammar specification for the language to be parsed. Usually BNF notation is used and the grammar defined as G = (S, P, NT, T) where S is the start symbol, P a set of productions, NT a set of non-terminal symbols and T a set of terminal symbols. See Appendix A for an example. The actual parser may be constructed from the grammar using the following rules of thumb 1. for each non-terminal symbol on the left hand side of a production,, write a procedure 2. for each terminal symbol, match the token and get the next token The execution of the parser effectively traces the parsing tree for any given program. For example, given the grammar in Appendix A and the following program program xyz(input, output); var a, b, c: integer; begin a := b+c end. See below for the parse tree. Comment also on 1. error handling 2. error recovery 3. semantic checking symbol table + operator table DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 4 av 15
program header Var_part Stat_part match program match ID match ( match inout match, match output match ) match ; varrdec vardeclisttail idlist match : type match ; empty match id idlisttail match integer match, idlist match id idlisttail match, idlist match id idlisttail Stat_part empty match begin Stat_list match end match. Stat_list Stat_list_tail stat empty Assign stat match id match := expr operand expr tail match id operator expr match + operand expr tail match id empty DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 5 av 15
Uppgift 3 / Question 3 expr -> term R1 R1 -> e '+' term R1 term -> factor R2 R2 -> e '*' factor R2 factor -> '(' expr ')' DIGIT (e = empty) Titta noggrant på grammatiken ovan. Rita parseträdet för uttrycket (2*2)+2. Read the above grammar carefully. Draw the parse tree for the expression: (2*2)+2. Totalt 5p Total 5p DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 6 av 15
expr term R1 factor R2 + term R1 ( expr ) e factor R2 e digit e term R1 e 2 factor R2 digit * factor R2 2 digit e 2 ( 2 * 2 ) + 2 DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 7 av 15
Uppgift 4 / Question 4 - MINNET / MEMORY program xyz (input, output); var a, b, c: integer; integer procedure X(var p1: integer; p2: integer) begin return 2 * p1 * p2 end; integer procedure Y(var p1, p2: integer) begin return X(p1 + p2, p2) end; begin a := 2; b:=3; c := 4; a := Y(a*b, c) end. Hur allokeras exekveringsminnet för programmet ovan under programmets exekvering? Svara ingående. Ange alla antagande How would the run-time memory be allocated for the program above during the program s execution? Answer in detail. State all assumptions Totalt 5p Total 5p In the execution phase, a, b, c are bound to values. a*b is stored in t2. Y is called and Y.p1 = t1; Y.p2 = c In Y t1 is set to Y.p1+Y.p2 X is called and X.p1 = Y.t1, X.p2 = Y.p2 The return value is stored in Y.t2 In X.t1 is set to 2*X.p1*X.p2 and this value returned Y then returns its result in Y.t2 This value is stored in t1 and ten assigned to a Explain how the stack frames are managed and what the pointers do & parameter passing mechanisms Code for procedure x Code for procedure Y Program code Static data objects: a, b, c Temp Dos: t1, t2 Stack frame for Y Static pointer Dynamic pointer Data objects: p1, p2, t1, t2 Stack frame for X Static pointer Dynamic pointer Data objects: p1, p2, t1 DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 8 av 15
Uppgift 5 / Question 5 Vilka programmeringskonstrukter är nödvändiga och vilka är önskevärda i ett programmeringsspråk?. Svara ingående. Which programming constructs are necessary and which are desirable in a programming language? Answer in detail. Note that this division is to a certain extent subjective. Totalt 5p Total 5p Necessary 1. the ability to define, either explicitly or implicitly, and manipulate data objects 2. flow of control: sequence, decision, repetition (either iteration or recursion) 3. expression evaluation 4. code packaging (abstraction): functions, procedures, modules 5. others? Desirable 1. assignment 2. type checking (strong typing statically; weak typing dynamically) 3. others? Marks for good arguments and examples. DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 9 av 15
Uppgift 6 / Question 6 - LISP ;; (defun make-book (title author class) (list (list 'title title) (list 'author author) (list 'class class) ) ) (defun xxx (book yyy zzz) (if (eql yyy (first (first book))) (cons (list zzz (second (first book))) (rest book)) (cons (first book) (xxx (rest book) yyy zzz)) ) ) ;; (setf book1 (make-book '(Artificial Intelligence) '(Patrick Henry Winston) '(Technical AI))) ;; (setf book1 (xxx book1 'class 'kau)) (quit) ;; Läs noggrant Lispkoden ovan. (a) Vad är det nya värdet av book1? (1p) (b) Hur fungerar funktion xxx? Visa alla parametervärden och returvärde vid varje (rekursivt) anrop av funktionen. (4p) Read the Lisp code above carefully (a) What is the new value of book1? (1p) (b) How does the function xxx work? Show all the parameter values and the return value from the function at each (recursive) call of the function. (4p) The old & new values of book1 are Totalt 5p/Total 5p ((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (KAU (TECHNICAL AI))) DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 10 av 15
How does xxx work? 1. (defun xxx (book yyy zzz) 2. (if (eql yyy (first (first book))) 3. (cons (list zzz (second (first book))) (rest book)) 4. (cons (first book) (xxx (rest book) yyy zzz)) 5. ) 6. ) Analyse the code and make some observations 1. there are 3 parameters book (a list of lists) and 2 unknowns xxx & yyy 2. to find out about these look at the application of the function (setf book1 (xxx book1 'class 'kau)) the first is a category literal ( class) and the second a literal symbol ( kau) 3. from the class case study on the library system in the lectures we know how this was used earlier 4. examine the code line by line and from the inside out 5. THINK WHAT THE INPUT PARAMETER VALUES ARE AT EACH STAGE OF THE FUNCTION VERY IMPORTANT 6. there are clearly lists of lists involved (first (first. In line 2 7. the rest is juggling with list 8. NOTE the recursive call in line 4 together with rest book a typical Lisp cliché First call:- note the actual parameter values note that the values of yyy and zzz DO NOT CHANGE at each call so that book is the parameter of interest book: yyy: zzz: ((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) class kau line 2 becomes (if eql class (first (first book))) (first book) is (TITLE (ARTIFICIAL INTELLIGENCE)) (first (first book)) is therefore TITLE and the comparison is FALSE goto line 4 Line 4 says cons (TITLE (ARTIFICIAL INTELLIGENCE)) with xxx applied to ((AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 11 av 15
Second (recursive) call:- note the actual parameter values book: yyy: zzz: ((AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) class kau line 2 becomes (if eql class (first (first book))) (first book) is (AUTHOR (PATRICK HENRY WINSTON)) (first (first book)) is therefore AUTHOR and the comparison is FALSE goto line 4 Line 4 says cons (AUTHOR (PATRICK HENRY WINSTON)) with xxx applied to (CLASS (TECHNICAL AI))) Third (recursive call):- repeat the analysis with the new value for the book parameter book: yyy: zzz: ((CLASS (TECHNICAL AI))) class kau line 2 becomes (if eql class (first (first book))) (first book) is ((CLASS (TECHNICAL AI))) (first (first book)) is therefore CLASS and the comparison is TRUE goto line 3 Line 3 says (cons (list zzz (second (first book))) (rest book)) Add the values of the parameters and evaluate successively 1. (cons (list zzz (second (first book))) (rest book)) 2. (cons (list kau (second (first ((CLASS (TECHNICAL AI)))))) (rest ((CLASS (TECHNICAL AI))))) 3. (cons (list kau (second ((CLASS (TECHNICAL AI))))) (rest ((CLASS (TECHNICAL AI))))) 4. (cons (list kau (TECHNICAL AI)) (rest ((CLASS (TECHNICAL AI))))) 5. (cons (KAU (TECHNICAL AI)) (rest ((CLASS (TECHNICAL AI))))) 6. (cons (KAU (TECHNICAL AI)) ( ) ) ;; ( ) NIL LIST 7. (KAU (TECHNICAL AI)) This value will be returned to line 4 i.e. the second (recursive) call which was see below Add the values of the parameters and evaluate successively 1. (cons (first book) (xxx (rest book) yyy zzz)) 2. (cons (AUTHOR (PATRICK HENRY WINSTON)) (KAU (TECHNICAL AI)) ) 3. ((AUTHOR (PATRICK HENRY WINSTON)) (KAU (TECHNICAL AI))) This in turn will be returned to line 4 of the first call 1. (cons (first book) (xxx (rest book) yyy zzz)) 2. (cons (TITLE (ARTIFICIAL INTELLIGENCE)) ((AUTHOR (PATRICK HENRY WINSTON)) (KAU (TECHNICAL AI)) ) 3. ( (TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (KAU (TECHNICAL AI)) ) 4. which gives the answer. DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 12 av 15
Uppgift 7 / Question 7 PROLOG No Facit a) Skriv ett predikat ta_bort_n_te(+n, +Lista1, -Lista2) där Lista2 är resultatet när man tagit bort det N:te elementet ur listan Lista1. 3 p b) I bilaga A finner du de regler som definierar hur Pascal-program får skrivas. I den Prologparser som du förhoppningsvis skrivit utgörs del tre av syntaxkontroll. Indata är en lista av lexem som kommer från lexern. Reglerna kan i stort sett göras om direkt till Prologsatser men vissa regler måste göras om för att parsern ska fungera. Vilka regler måste göras om och hur ska de se ut? Förklara utförligt! 2 p Engelsk version a) Write the predicate remove_n_th(+n, +List1, -List2) where List2 is the result when the Nth element is removed from List1. 3 p b) In appendix A you will find the rules which describe how a Pascal program may be written. In the Prolog parser which you hopefully have written, part three is the syntax control. Input is a list of lexem which come from the lexer. Most of the rules may be directly converted into Prolog rules but some have to be converted in order to get the parser work. Which rules must be converted and how will they look like? Describe carefully! 2 p Totalt 5p Total 5p DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 13 av 15
Uppgift 8 / Question 8 OO No Facit This question is about the difference in programming style between data based, structured programming on the one hand, and object-orientated programming on the other hand. Take a room booking system, with concepts like bookings and rooms, as an example. One common design recommendation in connection with database-based systems is to use logic objects, like BookingLogic and RoomLogic, which manage the operations related to records in a database table, together with data objects, like BookingDO (where DO stands for Data Object) and RoomDO, which mimic the database structure. In the following discussion, you may for instance use the question whether one booking overlaps another one as an example. Bookings have a fromtime and a totime, which together make up a time span. To avoid entering into the details of how to decide when two time spans overlap, you may assume the existence of a class TimeSpan with a query overlaps(timespan timespan), so that the query atimespan.overlaps(anothertimespan) returns true if the two time spans overlap and false otherwise. You may also assume that TimeSpan has a constructor with the parameters fromtime and totime, with the obvious interpretations. Here are your tasks: 1. Exemplify the difference in programming style between on the one hand data based, structured programming and on the other hand object-orientated programming. One suggested approach is for you to simply implement the query overlaps described above, once for each of the two programming styles. Define any new classes, variables, and operations you deem appropriate for the task. If your implementation involves any classes, the methods in your examples must be shown inside their respective class declarations. You may use any relevant programming language, real or invented, for your solution. Syntax is not an issue, as long as the interpretation is obvious and unambiguous. 2. To which of the two programming styles would you attribute the design recommendation in the introduction to this question, and why? (An answer without a justification is not an answer). Make sure that your example from the first task reflects your answer here. Totalt 5p Total 5p DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 14 av 15
Bilaga A - Grammatik: Pascallik språk Appendix A: Grammar: Pascal like language 1. [prog] ::= [prog header] [var part] [stat part] 2. [prog header] ::= program id ( input, output ) ; 3. [var part] ::= var [var dec list] 4. [stat part] ::= begin [stat list] end. 5. [var dec list] ::= [var dec] [var dec list] [var dec] 6. [var dec] ::= [id list] : [type] ; 7. [stat list] ::= [stat] [stat list] ; [stat] 8. [stat] ::= [assign stat] 9. [assign stat] ::= id := [expr] 10. [expr] ::= [term] [R1] 11. [R1] ::= e + [term] [R1] 12. [term] ::= [factor] [R2] 13. [R2] ::= e * [factor] [R2] 14. [factor] ::= ( [expr] ) [operand] 15. [type] ::= integer 16. [id list] ::= id [id list], id 17. [operand] ::= id number DFR/ND/EJN 120824 PS omtentamen / PL Resit Exam Facit Sidan 15 av 15