Docsity

Regular Expressions - Programming Languages and Compilers - Solved Exams, Exams of Programming Languages

Toggle sidebar
bg1
CS 164 Handout 11
Midterm Examination
ī˜
Please read all instructions (including these) carefully.
ī˜
Please print your name at the bottom of each page on the exam.
ī˜
There are seven questions on the exam, each worth between 10 and 20 points. You have 1 hour
and 20 minutes to work on the exam, so you should plan to spend approximately 12 minutes on
each question.
ī˜
The exam is closed book, but you may refer to your two sheets of prepared notes.
ī˜
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.
ī˜
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: Sample Solutions
SID or SS#:
Problem Max points Points
1 15
2 15
3 20
4 10
5 20
6 10
7 10
TOTAL 100
Fall 94 page 1 of 8
pf3
pf4
pf5
pf8

Partial preview of the text

Download Regular Expressions - Programming Languages and Compilers - Solved Exams and more Exams Programming Languages in PDF only on Docsity!

Midterm Examination

 Please read all instructions (including these) carefully.

 Please print your name at the b ottom of each page on the exam.

 There are seven questions on the exam, each worth b etween 10 and 20 p oints. You have 1 hour and 20 minutes to work on the exam, so you should plan to sp end approximately 12 minutes on each question.

 The exam is closed b o ok, but you may refer to your two sheets of prepared notes.

 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 pap er. Please do not use any additional scratch pap er.

 Solutions will b e 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 p oints for a question if your solution is far more complicated than necessary.

NAME: Sample Solutions

SID or SS#:

Problem Max p oints Points 1 15 2 15 3 20 4 10 5 20 6 10 7 10 TOTAL 100

  1. Regular Expressions (15 p oints)

Consider a language where real constants are de ned as follows: A real constant contains a decimal p oint or E notation, or b oth. For instance, 0 :01, 2 :71821828, ~ 1 :2E12, and 7E~ 5 are real constants. The symb ol ~" denotes unary minus and may app ear b efore the numb er or on the exp onent. There is no unary +" op eration. There must b e at least one digit to left of the decimal p oint, but there might b e no digits to the right of the decimal p oint. The exp onent following the \E" is a (p ossibly negative) integer. Write a regular expression for such real constants. Use the standard regular expression notation describ ed by the following grammar:

R!  j char j R + R j R  j RR j (R)

You may de ne 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 = ( + ~) posint exp = E int frac = : digit real = (int frac (exp + )) + (int (frac + ) exp)

  1. Grammars (20 p oints)

Consider the following grammar. The nonterminals are E, T, and L. The terminals are +,id,(,), and ;. The start symb ol is E.

E! E + T j T T! id j id () j id(L) L! E; L j E

Give an LL(1) grammar that generates the same language as this grammar. As part of your work please show that your grammar is LL(1).

(a) Eliminate left recursion:

E! TE^0 E^0! +TE^0 j  T! id j id() j id(L) L! E; L j E

(b) Left factor:

E! TE^0 E^0! +TE^0 j  T! id T^0 T^0!  j (T^00 T^00! ) j L) L! EL^0 L^0! ; L j 

(c) Check that it's LL(1). For this part you just needed to give enough information to show that there would be no conflicts in the parsing table. The following is sufficient:

E^0! +TE^0 j  First(+TE^0 ) = f+g Follow(E^0 ) = f$; ); ; g T^0!  j (T^00 First((T^00 ) = f(g Follow(T^0 ) = f+; $; ); ; g T^00! ) j L) First()) = f)g First(L)) = fidg L^0! ; L j  First(; L) = f; g Follow(L^0 ) = f)g

  1. Parsing (10 p oints)

In b oth parts of this question, we are lo oking for clarity and brevity as well as the right idea. Supp ose you are writing a parser for a programming language that includes the following syntax for lo oping constructs:

Lo op! do stmt while expr j do stmt until expr j do stmt forever

(a) (5 p oints) A predictive parser (i.e., a top-down parser with no backtracking) can't use this grammar. Give a brief (a couple of sentences) explanation of this fact.

When trying to expand a production for non-terminal ``loop'', the parser cannot decide which of the three productions to expand using only the next few input tokens.

(b) (5 p oints) Give a brief explanation of why a b ottom-up parser do es not have diculty with this grammar.

When a bottom-up parser must decide which of the three productions to choose (reduce), the while'',until'', or ``forever'' has already been read and shifted onto the stack.

  1. Bison and Abstract Syntax Trees (10 p oints)

Consider the following constructors for a tree language:

Expression app(Expression, Expression); Expression lambda(Expression, Expression); Expression id();

Now consider the following Bison grammar:

%token ID LAMBDA %type Expr

%%

Expr : ID { $$ = id(); } | '(' LAMBDA ID '.' Expr ')' { $$ = lambda( id(), $5); } | '(' Expr Expr ')' { $$ = app($2, $3); }

Draw the abstract syntax tree that would b e pro duced when parsing the sequence of tokens b elow. Lab el all the no des of your AST with the appropriate constructor.

( ( LAMBDA ID. (ID ID) ) ( LAMBDA ID. ID ) )

app

lambda lambda


id app id id

id id

  1. Typ e Checking (10 p oints)

Supp ose we want to design a typ e checker for Scheme programs. In Scheme, functions can b e passed as arguments to and returned as results from functions. Recall the typ e checking rule for function application given in class:

A f : t 1! t 2 A x : t 1 A ` f (x) : t 2

Self-application o ccurs 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. Brie y explain why typ e checking of self-application must always fail using the typ e checking rule ab ove.

From the rule, in a self-application f(f) we know that:

f : t1 -> t f : t

But t1 is a proper subexpression of t1 -> t2, so t1 <> t1 -> t2. Since f cannot have two disinct distinct types, type checking fails for self application.

An alternative (but equivalent) answer is:

If f : t1 -> t2 and f : t1 then t1 = t1 -> t2 since f has one type. But then

t1 = t1 -> t2 = (t1 -> t2) -> t2 = ((t1 -> t2) -> t2) -> t2 = ... -> t2 -> t2 -> t

which is infinite. Since types are finite size, type checking must fail for self-application.