Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

OCaml Programming, XML Nodes, and Grammar Analysis - Prof. Kirill A. Mechitov, Exams of Computer Science

Various topics related to ocaml programming, xml nodes, and grammar analysis, including ocaml function types, function evaluation, string concatenation, list processing, prefix notation evaluation, dfa for numbers divisible by 5, xml node dfa, ocamllex specification for xml nodes, int list grammar, top-down parsing functions, arithmetic expression grammar, and ambiguity resolution in grammars.

Typology: Exams

2010/2011

Uploaded on 06/02/2011

koofers-user-bm1
koofers-user-bm1 🇺🇸

10 documents

1 / 10

Toggle sidebar

Related documents


Partial preview of the text

Download OCaml Programming, XML Nodes, and Grammar Analysis - Prof. Kirill A. Mechitov and more Exams Computer Science in PDF only on Docsity! CS 421 Midterm 1 Name:____________________________________ 1 CS421 Spring 2009 Midterm 1 Thursday, February 26, 2009 • You have 90 minutes to complete this exam. • This is a closed-book exam. • Do not share anything with other students. Do not talk to other students. Do not look at another student’s exam. Do not expose your exam to easy viewing by other students. Violation of any of these rules will count as cheating. • If you believe there is an error, or an ambiguous question, seek clarification from one of the TAs. You must use a whisper, or write your question out. • Including this cover sheet, there are 10 pages to the exam. Please verify that you have all 10 pages. • Please write your name and NetID in the spaces above, and at the top of every page. Question Possible points Points earned EC points 1 8 2 8 3 6 4 8 5 8 6 8 7 4 8 8 9 8 10 (EC) 8 11 10 12 6 13 10 14 8 15 (EC) 6 Total 100 + 14 Name: NetID: CS 421 Midterm 1 Name:____________________________________ 2 1. (8 pts) Give the types of the following OCaml functions: a. let f (a,b,c) = a + c f: __int * 'a * int -> int____________________ b. let g (a,b) = b g: __'a * 'b -> 'b___________________________ c. let rec h a b = h: __'a list list -> 'a list -> bool_________________ if a = [b] then true else h a (tl b) d. let rec flatten lst = match lst with flatten: ___'a list list -> 'a list_________________ (x::xs)::ys -> x::(flatten (xs::ys)) | []::ys -> flatten ys | [] -> [];; 2. (8 pts) Consider this function: let sec arg = match arg with (x::y)::z -> y | a::b -> b What would the output be if sec is applied on the following values? If there is a type mismatch, write "Error;” if there is a run-time error, write “Run-time error.” a) [1;2;3] _type error______ b) [[1;2;3];[4;5;6];[7;8;9]] __[2; 3]____________ c) [[1];[2];[3]] __[]_________________ d) [[];[1;2;3]] __[]_________________ 3. (6 pts) Given the function let rec zero lst = match lst with [] -> [] | x::xs -> (x>0)::zero xs What is the output of the application zero [8;0;-3;4]? __[true; false; false; true]_________ 4. (8 pts) Write a function concat: string list -> string -> string concatenates the strings in its first argument, separated by the second argument, e.g. concat [“CS”; “421”; “rocks”] “ “ = “CS 421 rocks”. (concat [] = “”.) let rec concat sl s = match sl with [] -> "" | [s'] -> s' | s'::sl' -> s' ^ s ^ concat sl' s;; CS 421 Midterm 1 Name:____________________________________ 5 9. (8 pts) Give an ocamllex specification for XML nodes as described in the previous question. You may do this as a single regular expression if you like, or as a more complicated specification. For the purposes of this question, you may be more liberal about spaces if you like; you can stick to the specification given in question 8, or you can allow more spaces; in particular, spaces after the tag, after each attribute, and before the closing bracket, are legal in XML. You can return zero as the value of the node. let letter = ['A' - 'Z'] | ['a' - 'z'] rule tokenize = parse (* Add your regular expression here*) "<" letter+ (" " letter+ "=\"" letter* "\"")* (">"|"/>") { 0 } 10. (8 pts extra credit) Modify the ocamllex spec from question 9 so that it returns as its value a pair containing the tag and a list of key-value pairs. That is, it has type string * ((string * string) list). let letter = ['A' - 'Z'] | ['a' - 'z'] rule tokenize = parse "<" (letter+ as tag) { (tag, attributes lexbuf) } and attributes = parse | " " (letter+ as attr) "=\"" (letter* as value) "\"" {(attr,value)::attributes lexbuf} | (">"|"/>") { [] } CS 421 Midterm 1 Name:____________________________________ 6 11. (10 pts) This is a grammar for Ocaml int lists: list → [ ] | [ numbers ] numbers → int | int ; numbers (a) Explain why this grammar is not LL(1). For both non-terms, right-hand sides have overlapping FIRST sets. (b) Here is a top-down (i.e. LL(1)) grammar for the same language: list → [ list2 list2 → ] | numbers ] numbers → int numbers2 numbers2 → | ; numbers Write top-down parsing functions parseList and parseList2, both with type token list → (token list) option. You can assume that parseNumbers and parseNumbers2, with the same type, are provided. The set of tokens is defined by this type definition: type token = Lbracket | Rbracket | Int | Semicolon (Recall that the option type has definition: type ‘a option = None | Some of ‘a.) let rec parseList tlis = if hd tlis = Lbracket then parseList2 (tl tlis) else None and parseList2 tlis = if hd tlis = Rbracket then Some (tl tlis) else match parseNumbers tlis with None -> None | Some tlis' -> if hd tlis' = Rbracket then Some (tl tlis') else None CS 421 Midterm 1 Name:____________________________________ 7 12. (6 pts) A grammar for arithmetic expressions should have these properties, if possible: (a) It should be unambiguous (b) It should be LL(1) (c) It should enforce left-associativity of + and * (d) It should enforce precedence of * over + None of the following grammars satisfies all of these criteria. For each grammar, list all the properties that it fails to satisfy (using letters a-d): (1) E → id | E+id | E * id | (E) Fails: ______b, c, d________ (2) E → id | id+E | id*E | (E) Fails: ______b, c, d________ (3) E → T + E | T Fails: ______b, c________ T → P * T | P P → id | ( E ) (4) E → id F | ( E ) Fails: ______c, d________ F → | + E | * E (5) E → E + T | T Fails: ______b________ T → T * P | P P → id | ( E ) (6) E → T E’ Fails: _______c_______ E’ → | + E T → P T’ T’ → | * T P → id | ( E )
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved