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

Material Type: Assignment; Class: ORGNZTN PROGM LANG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-2s4
koofers-user-2s4 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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