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

CS 164 Midterm: Regex, Finite Automata, Grammars, Parsing & Type Checking, Exams of Programming Languages

The instructions and problems for the cs 164 midterm examination at the university of california, berkeley. The exam covers topics such as regular expressions, finite automata, grammars, parsing, and type checking. Students are required to write their answers in the space provided on the exam and are allowed to refer to their prepared notes. The exam is closed book and closed notes, except for the notes. The document also includes examples and explanations for each problem.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shailaja_987c
shailaja_987c 🇮🇳

4.3

(31)

230 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download CS 164 Midterm: Regex, Finite Automata, Grammars, Parsing & Type Checking and more Exams Programming Languages in PDF only on Docsity! CS 164 o Please read all instructions (including these) carefully. o Please print your name at the bottom of each page on the exam. i o There are seven questions on the exam, each worth between 10 and 20 points. You have I hour and 20 minutes to work on the exam, so you should plan to spend approximately 12 minutes on each question. Midterm Examination o The exam is closed book, but you may refer to your two sheets of prepared notes. o Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper. o Solutions will be graded on correctness and clarity. There are no "tricky' problems on the exam-each problem has a relatively simple and straightforward solution. You may get as few as 0 points for a question if your solution is far more complicated than necessary. NAME: SID or SS#: Problem, Max points, Points 1 15 2 15 3 20 4 10 5 20 6 10 7 10 Fall 94 page 1 of 8 CS 164 Handout 11 1. Regular Expressions (15 points) Consider a language where real constants are defined as follows: A real constant contains a decimal point or E notation, or both. For instance, 0.01, 2.71821828, -1.2El2, and 7E-5 are real constants. The symbol '-" denotes unary minus and may appear before the number or on the exponent. There is no unary '+' operation. There must be at least one digit to left of the decimal cs164f941 1 point, but there might be no digits to the right of the decimal point. The exponent following the "E" is a (possibly negative) integer. Write a regular expression for such real constants. Use the standard regular expression notation described by the following grammar: R-->(epsilon|char|R+R|R*|RR|(R) You may define names for regular expressions you want to use in more than one place (e.g., foo = R). digit = 0+1+2+3+4+5+6+7+8+9 posint = digit digit* int = ((epsilon+ -) posint exp = E int frac = .digit* real = (int f rac (exp + epsilon)) + (int (frac + epsilon) exp) Fall 94 page 2 of 8 CS 164 handout 11 2. Finite Automata (15 poilats) Consider a DFA with a start state so and a transition function trans. For a state s and input character c, trans(s, c) = s' if there is a transition from state s to state sl on character c. If there is -no transition from s on c then trans(s, C) = none. The following algorithm simulates the behavior of such a DFA, accepting if the input string is accepted by the DFA and rejecting otherwise. state - so while there's input left do: char +-- next input character if trans(state,char) = none then stop and reject state <-- trans(state,char) (end of loop) accept if state is a final state, otherwise reject Now consider an NFA with a start state so and a transition function trans. In this case, for a state s and input character c (we now allow c = c), trans(s, c) is the set of states s' for which there is a transition from s to s' on c. In the style of the algorithm above, give a (deterministic) algorithm that simulates the behavior of such an NFA. You may use the epsilon-closure operation described in class and in the text. Simulate subset construction. Use state-set instead of state, state-set - c-closure(so) while there's still input left do: char<-- next input character if esiplon-closure(Union s subset state-set trans(s, char)) = 0 then stop and reject state-set - epsilon-closure(Union s subset state-settxans(s,char)) cs164f941 2 Fall 94 page 5 of 8 CS 164 Handout 11 5. Parsing (20 points) Consider the following grammar. The nonterminals are S' and S. The terminals are op and x. The start symbol is S'. S' --> S S --> S OP S|X (a) (15 points) Draw the DFA built from sets of LR(O) items for this grammar. Show the contents of each state. (Note: Don't augment the grammar with a new start symbol.) (b) (3 points) Is this grammar SLR(l)? Briefly explain why or why not. (c) (2 points) Is this grammar LR(l)? Briefly explain why or why not. Fall 94 page 6 of 8 CS 164 Handout 11 6. Bison and Abstract Syntax 'JPrees (10 points) Consider the following constructors for a tree language: Expression app(Expression, Expression); Expression lambda(Expression, Expression); Expression ido; Now consider the following Bison grammar: %token ID LAMBDA %type <Expression> Expr %% Expr : ID { $$ = id() ; } | '(' LAMBDA ID '.' Expr ')' cs164f941 5 {$$ = lambda( id(), $5); } | '(' Expr Expr ')' { $$ = app($2, $3); } Draw the abstract syntax tree that would be produced when parsing the sequence of tokens below. Label aB the nodes of your AST with the appropriate constructor. ( ( LAMBDA ID . (ID ID) ) ( LAMBDA ID . ID ) ) Fall 94 page 7 of 8 CS 164 Handout 11 7. Type Checking (10 points) Suppose we want to design a type checker for Scheme programs. In Scheme, functions can be passed as arguments to and returned as results from functions. R@ecaU the type checking rule for function application given in class: Self-application occurs when a function is called with itself as a parameter. This is, if f is a function taking a function as a parameter, the f(f) is an instance of self-application. Briefly explain why type checking of self-application must always fail using the type checking rule above. cs164f941 6 Fall 94 page 8 of 8 Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If you have any questions about these online exams please contact examfile@hkn.eecs.berkeley.edu. cs164f941 Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If you have any questions about these online exams please contact examfile@hkn.eecs.berkeley.edu.7
Docsity logo



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