Docsity

Context-Sensitive Grammars - Programming Language Translation - Quiz, Exercises of Programming Languages

Partial preview of the text

Download Context-Sensitive Grammars - Programming Language Translation - Quiz and more Exercises Programming Languages in PDF only on Docsity!

CSE 4100 Midterm Exam โ€“ Fall 2009

Name:

Number Subproblem Points Score 1 10 2 a 5 2 b 5 3 15 4 a 10 4 b 5 5 a 20 5 b 5 Total 75

Use only one side of the paper and start each problem on a new page!! Please show all work to receive ANY credit!!!!

  1. (10 points) Context-Sensitive Grammars and Derivations

In a CFG, the left-hand-side of a production rule must be a non-terminal. However, in a context-sensitive grammar (CSG), the left-hand-side of each production rule can be a sequence of one or more non-terminals (at least one) and terminals (zero or more) as shown below:

S โˆ’โ†’ x S B C C B โˆ’โ†’ B C y C โˆ’โ†’ y z S โˆ’โ†’ x y C y B โˆ’โ†’ y y z C โˆ’โ†’ z z

Note that x, y, and z are terminals, and S, A, B, and C are non-terminals. In a CSG, during a single step of the derivation, two symbols (say y B) can be replaced by two symbols (say y y) for the production rule y B โˆ’โ†’ y y, which is unlike a CFG where one symbol (always a non-terminal) can be replaced by zero or more non-terminals or terminals. This is shown in the step of the derivation below:

ฮฑ y B ฮฒ =โ‡’ ฮฑ y y ฮฒ

where ฮฑ and ฮฒ are strings of terminals and non-terminals. Using this grammar, develop a derivation for the string: xxxyyyzzz.

  1. (15 points) CFGs, Left Recursion, and  Moves

As the first step in the design of a top-down parser, reformulate the grammar given below into an equivalent CFG that is suitable for top-down parsing by first removing any left recursion that exists, and then removing all  moves.

A 1 โˆ’โ†’ x A 2 A 3 โˆ’โ†’ A 1 x A 1 โˆ’โ†’ A 3 y A 3 โˆ’โ†’ A 2 y A 2 โˆ’โ†’ A 3 x A 3 โˆ’โ†’ z A 2 โˆ’โ†’ A 1 y

In the grammar, x, y, and z are terminals, and A 1 , A 2 , and A 3 are non-terminals. Make sure that you clearly indicate your results separately, i.e., box the CFG without left recursion with  moves, and box the result after  moves have been eliminated.

  1. (15 points) FIRST and FOLLOW for CFGs

Consider the grammar given below, with S as the start symbol, X, Y, and Z, as non-terminals, a, b, c, d, f, and g as terminals, and  as the empty symbol.

S โˆ’โ†’ X Y Z X โˆ’โ†’ a | Z b |  Y โˆ’โ†’ c | d X Y |  Z โˆ’โ†’ f | g

(a) (10 points) Compute FIRST and FOLLOW for each non-terminal in the grammar.

  1. (20 points) Design of a Context Free Grammar

In the ML functional programming language, a list is a sequences of one or more list ele- ments (which may also be themselves lists), that are enclosed in square brackets with elements separated by commas. For example:

val L = [1, 2, 3]; -- an integer list with three elements val N = ["one", "two"]; -- a string list with two elements val P = [1.5]; -- a real list with one elemernt val Q = [[4,5,6], [7,8,9]]; -- a list containing two tuples each of which -- is a list of three elements

Lists are accessed in ML by applying the function head (hd) to return the first list element and tail (tl) to return a list containing all elements except the first one, e.g.,

hd(L); -- returns the integer 1 tl(L); -- returns the list [2, 3] hd(P); -- returns the real 1. tl(P); -- returns the null list [] tl(Q); -- returns the list [[7,8,9]] hd(tl(L)); -- returns the integer 2 -- note that combinations are possible tl(tl(L)); -- returns the list [3] -- applied in a nested fashion tl(tl(tl(L)); -- returns the null list []

Assume that tokens have been defined for: INT, REAL, STR, IDENT, HD, TL, VAL.

(a) (20 points) Design a CFG for ML programs that contain one or more val list declarations followed by one or more statements to access lists and/or apply functions. To get you started, the first few grammar rules are given below, where val decclarations refes to the val statements above, and statement list refers to the application of hd, tl, etc., to declared list variables. ml_program --> val_declarations statement_list

val_declarations --> val_declarations list_declaration | list_declarations

list_declaration --> ???

statement_list --> ???

Additional space for part 5a.