





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






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!!!!
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.
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.
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.
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.