CMSC330: CFG Practice Problems - Generating and Parsing Context-Free Grammars, Assignments of Programming Languages

A series of practice problems related to context-free grammars (cfgs). Students are required to design unambiguous grammars, give derivations and parse trees for given strings, and draw nfa/dfa for certain grammars. The problems cover various aspects of cfgs, including ambiguous grammars and palindromic strings.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-a65
koofers-user-a65 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC330: CFG Practice Problems
1) What language does the following grammar generate?
SaaSbb |
2) Given the language L={aibjc2i+1dk|i, j, k 0}:
a) Design an unambiguous grammar that generates L.
b) Give the leftmost and rightmost derivations of the string abbcccd using the grammar from part a).
c) Give the parse trees for the string abbcccd based on the grammar of part a).
3) Given the language L={anbmanm|nm0}
a) Design an unambiguous grammar that generates L.
b) Give the leftmost and rightmost derivations of the string aaabba using the grammar from part a).
c) Give the parse trees for the string aaabba based on the grammar of part a).
4) Given the regular expression R= (a|b)+bab:
a) Design a grammar for R.
b) Give the leftmost and rightmost derivations of the string aaabba using the grammar from part a).
c) Give the parse trees for the string aaabba based on the grammar of part a).
5) Draw a NFA/DFA that represents the same language as the following grammar:
S01R|
R0R|T|
T0T|0
6) Given the language L={w {a, b}|whas the same number of a’s and b’s}:
a) Give an unambiguous grammar describing L.
Edit: the grammar you write can be ambiguous. It’s pretty hard to write an unambiguous one for this
problem.
Note: Remember, the a’s and b’s can be in any order, as long as there are the same number of each.
b) Give a derivation for the string aabb using your grammar from a) and draw the corresponding parse
tree.
1
pf2

Partial preview of the text

Download CMSC330: CFG Practice Problems - Generating and Parsing Context-Free Grammars and more Assignments Programming Languages in PDF only on Docsity!

CMSC330: CFG Practice Problems

  1. What language does the following grammar generate? S → aaSbb | 
  2. Given the language L = {aibj^ c^2 i+1dk^ | i, j, k ≥ 0 }: a) Design an unambiguous grammar that generates L. b) Give the leftmost and rightmost derivations of the string abbcccd using the grammar from part a). c) Give the parse trees for the string abbcccd based on the grammar of part a).
  3. Given the language L = {anbman−m^ | n ≥ m ≥ 0 } a) Design an unambiguous grammar that generates L. b) Give the leftmost and rightmost derivations of the string aaabba using the grammar from part a). c) Give the parse trees for the string aaabba based on the grammar of part a).
  4. Given the regular expression R = (a | b)+b∗ab∗: a) Design a grammar for R. b) Give the leftmost and rightmost derivations of the string aaabba using the grammar from part a). c) Give the parse trees for the string aaabba based on the grammar of part a).
  5. Draw a NFA/DFA that represents the same language as the following grammar: S → 01 R |  R → 0 R | T |  T → 0 T | 0
  6. Given the language L = {w ∈ {a, b}∗^ | w has the same number of a’s and b’s}: a) Give an unambiguous grammar describing L. Edit: the grammar you write can be ambiguous. It’s pretty hard to write an unambiguous one for this problem. Note: Remember, the a’s and b’s can be in any order, as long as there are the same number of each. b) Give a derivation for the string aabb using your grammar from a) and draw the corresponding parse tree.
  1. Give an unambiguous grammar describing the following languages: Edit: the grammars you write can be ambiguous. It’s pretty hard to write unambiguous ones for this problem. a) {w ∈ {a, b}∗^ | the number of a’s in w is two times the number of b’s} b) {w ∈ {a, b}∗^ | the number of a’s in w is two times the number of b’s plus one}
  2. Given the regular expression ((1(0 | 1)∗) | )0: a) Draw a corresponding DFA. b) Design a grammar for the language. c) Describe in words what language is generated by this grammar.
  3. Give a grammar for each of the following languages: a) {ambnam+n^ | m ≥ 0 and n ≥ 1 } b) {ambncpdq^ | m + n = p + q} c) {w ∈ {a, b}∗^ | w contains no substrings of the form ab} d) {γ 1 γ 2... γnγn... γ 2 γ 1 | γi ∈ {a, b}, 1 ≤ i ≤ n} Note: This language is describing all strings that are palindromes, that is, they read the same forwards as backwards, with alphabet Σ = {a, b}
  4. Design a grammar for the language: {anbnambm^ | m, n ≥ 1 } ∪ {anbmambn^ | m, n ≥ 1 }.
  5. The following grammar is an ambiguous grammar for balanced parentheses: S → (S) | SS |  a) Show that this grammar is ambiguous. b) Design a new grammar for balanced parentheses that is not ambiguous.