0% found this document useful (0 votes)
25 views3 pages

Context-Free Languages Tutorial Exercises

This document contains exercises on context-free grammars (CFGs), Chomsky normal form, pushdown automata (PDAs), and the conversion between CFGs and PDAs. Some key points covered include: showing languages are or aren't context-free using closures properties; constructing CFGs and PDAs for various languages; and converting between CFGs and PDAs. DeMorgan's laws and closure under regular operations are also discussed.

Uploaded by

Mohieddine Farid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Context-Free Languages Tutorial Exercises

This document contains exercises on context-free grammars (CFGs), Chomsky normal form, pushdown automata (PDAs), and the conversion between CFGs and PDAs. Some key points covered include: showing languages are or aren't context-free using closures properties; constructing CFGs and PDAs for various languages; and converting between CFGs and PDAs. DeMorgan's laws and closure under regular operations are also discussed.

Uploaded by

Mohieddine Farid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 4: Context-Free Languages

CFG, Chomsky Normal Form, PDA, Conversion CFG to PDA

Exercise 1 Consider the following CFG:

E →E+T |T
T →T ×F |F
F → (E) | a

Give parse trees and derivations for each string.

• a • a+a

• a+a+a • ((a))

Exercise 2 We admit the following result: ”The class of context-free language is not closed under
intersection”. Use DeMorgan’s law to show that the class of context-free languages is not closed
under complementation.
De Morgan’s Laws:
De Morgan’s Laws describe the relationship between set union (∪), set intersection (∩), and
set complement. They can be stated as follows:

(A ∪ B) = (Ā) ∩ (B̄) (1)


(A ∩ B) = (Ā) ∪ (B̄) (2)

Exercise 3 Answer each part for the following context-free grammar G.

R → XRX | S
S → aT b | bT a
T → XT X|X|ε
X→a|b

a. What are the variables of G ? i. True or False: T ⇒ T .


b. What are the terminals of G ? ∗
j. True or False: XXX ⇒ aba.
c. Which is the start variable of G? ∗
k. True or False: X ⇒ aba.
d. Give three strings in L(G). ∗
l. True or False: T ⇒ XX.
e. Give three strings not in L(G).
m. True or False: T ⇒ XXX.
f. True or False: T ⇒ aba.


g. True or False: T ⇒ aba. n. True or False: S ⇒ ε.

h. True or False: T ⇒ T . o. Give a description in English of L(G).

Exercise 4 Give context-free grammars that generate the following languages. In all parts, the
alphabet Σ is {0, 1}.

1
a. {w | w contains at least three 1 s }

b. {w | w starts and ends with the same symbol }

c. {w | the length of w is odd }

d. {w | the length of w is odd and its middle symbol is a 0}

e. w | w = wR , that is, w is a palindrome }




f. The empty set

g. The set of strings over the alphabet | {a, b} with more a’s than b’s

h. The complement of the language {an bn | n ≥ 0}

i. w#x | wR is a substring of x for w, x ∈ {0, 1}∗ }




j. {x1 #x2 # · · · #xk | k ≥ 1, each xi ∈ {a, b}∗ , and for some i and j, xi = xR
j

Exercise 5 • Give informal descriptions and state diagrams of pushdown automata for the
languages in Exercise 4 (a-f ).

• Give informal English descriptions of PDAs for the languages in Exercise 4(g-j)

Exercise 6 Give a context-free grammar that generates the language

A = ai bj ck | i = j or j = k where i, j, k ≥ 0 .


Is your grammar ambiguous? Why or why not?

Exercise 7 Give an informal description of a pushdown automaton that recognizes the language
A in Exercise 6

Exercise 8 Using the conversion CFG to PDA:


• Convert the CFG G4 given in Exercise 1 to an equivalent PDA

• Convert the CFG G given in Exercise 3 to an equivalent PDA,

Exercise 9 Let G = (V, Σ, R, S) be the following grammar. V = {S, T, U }; Σ = {0,# }; and R


is the set of rules:
S → TT | U
T → 0T |T 0|#
U → 0U 00 | #
a. Describe L(G) in English.

b. Prove that L(G) is not regular.

Exercise 10 Convert the following CFG into an equivalent CFG in Chomsky normal form.
A → BAB|B|ε
B → 00 | ε
Exercise 11 Give a counter-example to show that the following construction fails to prove that
the class of context-free languages is closed under star. Let A be a CFL generated by the CFG
G = (V, Σ, R, S). Add the new rule S → SS and call the resulting grammar G′ . This grammar is
supposed to generate A∗ .

2
Exercise 12 Show that the class of context-free languages is closed under the regular operations,
union, concatenation, and star.

Exercise 13 Use the results of Exercise 12 to give another proof that every regular language is
context free, by showing how to convert a regular expression directly to an equivalent context-free
grammar.

Exercise 14 We admits the following result: ”The language {an bn cn | n ≥ 0} is not context-free”.

a. Let C be a context-free language and R be a regular language. Prove that the language C ∩ R
is context free.

b. Let A = {w | w ∈ {a, b, c}∗ and w contains equal numbers of a’s, b’s, and c’s }. Use part
(a) to show that A is not a CFL.

Exercise 15 Let CFG G be the following grammar.

S → aS b| bY |Y a
Y → bY |aY |ε

Give a simple description of L(G) in English. Use that description to give a CFG for L(G), the
complement of L(G).

Exercise 16 Let A/B = {w | wx ∈ A for some x ∈ B}. Show that if A is context free and B is
regular, then A/B is context free.

Exercise 17 • Let Σ = {a, b}. Give a CFG generating the language of strings with twice as
many a’s as b’s. Prove that your grammar is correct.

• Let C = {x#y | x, y ∈ {0, 1}∗ and x ̸= y}. Show that C is a context-free language.

Common questions

Powered by AI

To convert a context-free grammar (CFG) into an equivalent pushdown automaton (PDA), follow these steps: Start with an empty PDA. For each variable in the CFG, create a PDA state. For each production in the CFG, create transitions in the PDA that simulate the expansion of those productions. The PDA uses its stack to store and manipulate symbols that represent the variables and terminals, mimicking the derivations in the CFG. The PDA starts with the initial grammar variable on the stack and processes strings by matching stack operations with string derivations until the stack is empty and the input is consumed .

The language C ∩ R, where C is a context-free language and R is a regular language, remains context-free due to the properties of intersection with a regular set. Regular languages can be represented by finite automata, and the intersection of a context-free language with a regular language can be handled by combining the finite automaton for R with a pushdown automaton for C. The resulting concurrent automaton essentially simulates both the context-free conditions of C and the regular conditions of R, maintaining the context-free nature of the intersection because any language that results from such a simulation remains context-free .

The potential issue with transforming every regular language directly into a context-free grammar is the misinterpretation of the specific constructs of a regular expression as simply being verbatim parts of the CFG. However, this is resolved by understanding that each operation in a regular expression (concatenation, union, and star) must be explicitly modeled in the CFG using non-terminals and production rules to mimic finite automaton transitions accurately. By ensuring that the construction accurately reflects the regular automaton's operations, transforming a regular language into a CFG becomes feasible .

The language generated by the CFG S → aS b | bY | Y a; Y → bY | aY | ε consists of strings formed by matching pairs of a's and b's, allowing alterations of prefix and suffix structures while maintaining balance. The strings start with a block of 'a's and end with a block of 'b's, or vice versa, potentially with a mix that balances out in between. For constructing its complement CFG, one needs to create a grammar that generates strings that violate this structure, ensuring an imbalance or incorrect order, which involves reversing operations or modifying derivations to not match the balanced rules applied in the original CFG .

The construction that adds the rule S → SS to account for the star operation fails because simply adding this rule does not guarantee that the resulting language is the closure under the star operation. A counter-example is the language L = {a^n b^n | n ≥ 0}, which is context-free. Its closure under the star operation should allow strings like 'aabbabab,' which is not the case when only adding S → SS. The CFG becomes unable to combine multiple segments of L correctly without a structured mechanism to enforce proper concatenation, showing that the added rule is insufficient for handling star closure adequately .

De Morgan's Laws can be applied to show that context-free languages are not closed under complementation by demonstrating that if they were, then they would also be closed under intersection, which is known not to be the case. Specifically, if context-free languages were closed under complementation, then for any two context-free languages A and B, the complement of both individually ((¬A) and (¬B)) would also be context-free. By De Morgan's Law, the intersection of two such complements (¬A ∩ ¬B) would result in a context-free language equal to the complement of the union (¬(A ∪ B)). However, since context-free languages are not closed under intersection, this leads to a contradiction, proving that they cannot be closed under complementation either .

A pushdown automaton for language A = {a^i b^j c^k | i = j or j = k} needs to handle two cases. For i = j, the PDA initially pushes 'a's onto the stack and then pops an 'a' for each 'b' ensuring that the number of 'a's and 'b's are the same. For j = k, after handling 'a's and 'b's, the PDA pushes 'b's and pops each one for a corresponding 'c'. Thus, the PDA must manage stack operations to ensure the counts match for the constraints i = j or j = k .

The language C = {x#y | x, y ∈ {0, 1}* and x ≠ y} can be shown to be context-free by constructing a context-free grammar that generates all combinations of strings where x and y are different. One way to achieve this is by developing separate CFG rules that compare the characters in x and y, ensuring at least one position differs. Additionally, one could design a PDA that pushes the characters of x to the stack and then reads y, rejecting if all matched perfectly while accepting if a mismatch is detected, showing it is context-free .

The language L = {a^n b^n c^n | n ≥ 0} is not context-free because context-free grammars cannot enforce the condition that three different segments of strings (a's, b's, and c's) have matching lengths. This condition requires simultaneous matching of three segments which cannot be achieved in a context-free manner, as context-free grammars can only handle two levels of balancing. This is known to exceed the capabilities of context-free grammars, which can manage dependencies between two segments, such as matching numbers in the format a^n b^n, but not three .

Yes, the language composed of palindromes {w | w = wR} can be generated by a context-free grammar. The grammar should include rules that allow for recursion while maintaining symmetry. For example, a CFG could include rules like S → aSa | bSb | ε, which allow a string to be expanded from the middle outward, ensuring that every 'a' on the left is matched with an 'a' on the right, and likewise for 'b's. The ε production allows it to terminate symmetrically, generating palindromes .

You might also like