Docsity

Practice Problems 3 - Organization, Programing, Language | CMSC 330, Assignments of Programming Languages

Toggle sidebar
bg1
CMSC 330
Practice Problems 3
For this exercise, we’ll call a backtracking recursive-
descent parser a backtracking parser, and a non-
backtracking recursive-descent parser a predic-
tive parser.
1. Consider the grammar
S→ABd |aBc
A→
B→b|c
(a) Compute First sets for each produc-
tion
(b) Can the grammar be parsed by a pre-
dictive parser?
(c) Implement a predictive parser for the
grammar
(d) Use your parser to parse the strings
”bd” and ”acc”. Show the sequence
of procedure calls in the parse, and
what symbols remain to be parsed at
each point.
2. Consider the grammar
S→AS |b
A→SA |a
(a) Compute First sets for each produc-
tion
(b) Can the grammar be parsed by a pre-
dictive parser?
(c) Can the grammar be parsed by a back-
tracking parser?
(d) Is the grammar ambiguous? Prove your
answer.
(e) Are all ambiguous grammars non-parseable
by predictive parsers?
(f) Are all non-ambiguous grammars parseable
by predictive parsers?
3. Consider the grammar
S→(L)|a
L→L , S |S
(a) Compute First sets for each produc-
tion
(b) Can the grammar be parsed by a pre-
dictive parser?
(c) Can the grammar be parsed by a back-
tracking parser?
(d) Rewrite grammar using the rule for
eliminating left recursion
(e) Compute First sets for each produc-
tion
(f) Can the resulting grammar be parsed
by a predictive parser?
(g) Write a predictive parser for the gram-
mar
(h) Use your parser to parse the string
”(a,a)”. Show the sequence of calls in
the parse, and what symbols remain
to be parsed at each point.
4. Consider the grammar
E→E + T |T
T→a|(E)
(a) Compute First sets for each produc-
tion
(b) Is the grammar ambiguous?
(c) Can the grammar be parsed by a pre-
dictive parser?
(d) Can the grammar be parsed by a back-
tracking parser?
(e) Rewrite grammar using the rule for
eliminating left recursion
(f) Compute First sets for each produc-
tion
(g) Can the resulting grammar be parsed
by a predictive parser?
(h) Write a predictive parser for the gram-
mar
(i) Use your parser to parse the string ”a
+ a + a”.
pf2

Partial preview of the text

Download Practice Problems 3 - Organization, Programing, Language | CMSC 330 and more Assignments Programming Languages in PDF only on Docsity!

CMSC 330

Practice Problems 3

For this exercise, we’ll call a backtracking recursive- descent parser a backtracking parser, and a non- backtracking recursive-descent parser a predic- tive parser.

  1. Consider the grammar S → ABd | aBc A →  B → b | c

(a) Compute First sets for each produc- tion (b) Can the grammar be parsed by a pre- dictive parser? (c) Implement a predictive parser for the grammar (d) Use your parser to parse the strings ”bd” and ”acc”. Show the sequence of procedure calls in the parse, and what symbols remain to be parsed at each point.

  1. Consider the grammar S → AS | b A → SA | a

(a) Compute First sets for each produc- tion (b) Can the grammar be parsed by a pre- dictive parser? (c) Can the grammar be parsed by a back- tracking parser? (d) Is the grammar ambiguous? Prove your answer. (e) Are all ambiguous grammars non-parseable by predictive parsers? (f) Are all non-ambiguous grammars parseable by predictive parsers?

  1. Consider the grammar S → ( L ) | a L → L , S | S

(a) Compute First sets for each produc- tion (b) Can the grammar be parsed by a pre- dictive parser? (c) Can the grammar be parsed by a back- tracking parser? (d) Rewrite grammar using the rule for eliminating left recursion (e) Compute First sets for each produc- tion (f) Can the resulting grammar be parsed by a predictive parser? (g) Write a predictive parser for the gram- mar (h) Use your parser to parse the string ”(a,a)”. Show the sequence of calls in the parse, and what symbols remain to be parsed at each point.

  1. Consider the grammar E → E + T | T T → a | (E)

(a) Compute First sets for each produc- tion (b) Is the grammar ambiguous? (c) Can the grammar be parsed by a pre- dictive parser? (d) Can the grammar be parsed by a back- tracking parser? (e) Rewrite grammar using the rule for eliminating left recursion (f) Compute First sets for each produc- tion (g) Can the resulting grammar be parsed by a predictive parser? (h) Write a predictive parser for the gram- mar (i) Use your parser to parse the string ”a

  • a + a”.
  1. Consider the grammar E → T + E | T T → a | (E)

(a) Can the grammar be parsed by a pre- dictive parser? (b) Would the grammar accept the same language as the grammar in the pre- vious problem? (c) What is the difference between this grammar and the previous grammar rewritten to eliminate left recursion?

  1. Rewrite the following grammars so they can be parsed by a predicative parser by elimi- nating left recursion and applying left fac- toring where necessary.

(a) S → S + a | b (b) S → S + a | S + b | c (c) S → S + a | S + b |  (d) S → a b c | a c (e) S → a b c | a b b (f) S → a b c | a b (g) S → a a | a b | a c (h) S → a a | a b | a (i) S → a a | a b |  (j) S → a S c | a S b | b (k) S → a S c | a S b | a (l) S → a S c | a S | a