TOC IA 2
TOC IA 2
A string is in the language of the grammar if it can be derived from the start variable by applying
the rules.
Example:
Consider the grammar G = ({S, A, B}, {a, b}, R, S), where R contains the following rules:
1. S → aSb
2. S → ε
This grammar generates the language {anbn | n ≥ 0}, which consists of strings with an equal
number of 'a's and 'b's. Here's how the string "aabb" is derived:
1. S (Start)
2. aSb (Apply rule 1)
3. aaSbb (Apply rule 1 again)
4. aabb (Apply rule 2)
2.Obtain the CFG to generate strings consisting any number of a’s and b’s where L = {
|w| mod 3 =0 }.
To generate strings consisting of any number of 'a's and 'b's where the length of the string is
divisible by 3, we can use the following CFG:
1. S→ε
2. S → AAA
3. A→a
4. A→b
Explanation:
Examples:
• ε (from rule 1)
• aaa (from rules 2 and 3)
• bbb (from rules 2 and 4)
• aababb (from rules 2, 3, and 4)
This grammar ensures that all the generated strings will have a length divisible by 3.
3.Let G be the Grammer
S→aB| bA
A→aS| bAA| a
B→ bS| aBB| b.
For string aaabbabbba .Construct LMD,RMD and parse tree.
LMD and RMD are like step-by-step instructions for building the string "aaabbabbba" using
grammar G.
The parse tree shows you the hierarchical structure of the string, like a family tree.
E → +EE
→ +*EEE
→ +*-EEEE
→ +*-xyEEE
→ +*-xyxyEE
→ +*-xyxyx
→ +*-xyxyxy
Rightmost Derivation (RMD) for the string +*—xyxy:
E → +EE
→ +E-EE
→ +E-Exy
→ +E-Exyx
→ +E-Exyxy
→ +*EE-Exyxy
→ +*-EE-Exyxy
→ +*-xyxy
E
/|\
+ E E
| /|\
* - E E
| x y
x y
E → (E)
→ (E+E)
→ (id+E)
→ (id+E*E)
→ (id+id*E)
→ (id+id*id)
E → (E)
→ (E+E)
→ (E+E*E)
→ (E+E*id)
→ (E+id*id)
→ (id+id*id)
E
|
(E)
|
E+E
/ \
id E*E
/ \
id id
These derivations and parse trees demonstrate how strings can be generated and their structure
represented using the given grammars. Note that the sources provided do not contain examples
using these exact grammars. The provided answers draw on the general concepts of derivations
and parse trees discussed in the sources, and apply them to the specific grammars in the query.
Consider the string "aabb". This string can be derived in two different ways using the given
grammar:
Derivation 1:
• S → SS
• S → aSbS → aabbS → aabbε → aabb
Derivation 2:
• S → aSb
• S → aaSbb → aabb
The existence of two distinct leftmost derivations (and parse trees) for the same string proves the
grammar is ambiguous.
The grammar is ambiguous. Let's prove this by demonstrating two different parse trees for the
input string "a + b * c":
E
/ \
E *
/ \ \
E + E
| | |
I I I
| | |
a b c
E
/ \
E +
/ \ \
I * I
| / \ |
a E E c
| |
I I
| |
b c
The existence of two distinct parse trees for the same input string "a + b * c" demonstrates
that the grammar is ambiguous.
• Question 6: The grammar allows for multiple ways to generate the same sequence
of 'a's and 'b's, making it unclear how to parse strings like "aabb".
• Question 7: The lack of explicit precedence rules for the operators '+' and '*' in the
grammar allows for two different ways to structure the expression "a + b * c", leading
to potential ambiguity in evaluating the expression.
8. What is ambiguous grammar? Show that the following grammar is ambiguous. For
w=ibtibtibtaea S → iCtS| iCtSeS| a C → b
An ambiguous grammar is a context-free grammar for which there exists a string that can have
more than one leftmost derivation or parse tree.
• Both derivations lead to the same string, but they differ in the order of application of
the production rules.
• This demonstrates that the grammar is ambiguous as the string w=ibtibtibtaea can
be derived in multiple ways.
S->aSbS
S->bSaS
S->€
An ambiguous grammar is a context-free grammar where a single string can have more than one
leftmost derivation, leading to multiple parse trees.
This means that the grammar doesn't provide a unique interpretation for every string it generates,
making it difficult to understand the intended meaning.
Example: In the given grammar, the string "abab" can be derived in two ways:
To demonstrate the ambiguity of the given grammar, consider the string 'aaa'. Two distinct
leftmost derivations are possible for this string, indicating the presence of ambiguity:
The existence of these two different derivations for the same string proves that the given
grammar is ambiguous.
Context-Free Grammar (CFG): A CFG is a formal grammar in which every production rule is
of the form A → α, where A is a single non-terminal symbol, and α is a string of terminals and/or
non-terminals. CFGs are used to generate context-free languages.
Left Recursion: Left recursion in a CFG occurs when a non-terminal symbol can derive a string
that starts with itself. It can cause issues in parsing and needs to be eliminated.
Example: In the given grammar, the production 'E -> E+T' demonstrates left recursion as 'E'
appears as the first symbol on the right-hand side.
E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id
Explanation:
This modified grammar generates the same language but without left recursion.
Think of an engineering student who builds complex machines using simpler components.
Similarly, we'll break down the PDA into smaller parts and create rules based on its behavior:
Steps:
1. Variables: For every pair of states (p, q) in the PDA, create a variable [pXq], where X
is a stack symbol.
2. Start Variable: The start variable is [q0Z0q1] (starting state with initial stack symbol
leading to the final state).
3. Rules: For each transition in the PDA, create corresponding rules:
o If the PDA reads an input 'a' and pushes 'Y' onto the stack while moving from
state 'p' to 'q', add a rule: [pXq] -> a[pYq].
o If the PDA pops 'X' from the stack without reading input and moves from state
'p' to 'q', add a rule: [pXq] -> ε (epsilon, meaning empty string).
o Combine these rules for complex transitions.
This resulting CFG generates the same language as the given PDA.
S -> aAS
A -> a | b
Explanation:
S -> aAS
A -> bAA | a
Explanation:
• Similar to the previous example, for each 'a' read, the PDA pushes an 'A' onto the
stack.
• It stays in state q0 for any number of 'b' inputs, pushing two 'A's for each 'b' (rule 2).
This corresponds to the production A -> bAA.
16.Construct PDA for the given CFG, and test whether 010000 is acceptable by this
PDA.
S → 0BB
B → 0S | 1S | 0 .
Constructing a PDA and Testing a String
To construct a PDA for the given CFG and test if the string 010000 is acceptable:
Explanation:
The PDA uses the stack to store symbols that are currently being derived by the CFG. It starts by
pushing the start symbol onto the stack. Then, it repeatedly pops symbols from the stack and
replaces them with symbols from the right-hand side of a production. The PDA accepts the input
string if it is able to empty the stack.
Explanation: The PDA simulates the leftmost derivation of the CFG. It pushes the start symbol
on the stack. For every input symbol, it pops a matching terminal symbol from the stack and
pushes the right-hand side of a production onto the stack.
18.Convert the grammar with following production to PDA accepted by empty stack:
S→ 0S1 | A
A→1A0 | S | ε
Explanation: The PDA pushes the start symbol on the stack. For every input, it pops a matching
terminal symbol from the stack. It accepts the string if it can empty the stack.
Explanation: The PDA pushes the input symbols onto the stack until it encounters 'c'. Then, it
starts popping symbols from the stack, matching them with the remaining input symbols. If the
stack is empty after processing the entire input, the string is accepted.
20.Define PDA. Design a PDA to accept the following language. L={a^nb^n | n>=0} Draw
the transition diagram for the constructed PDA. Show the ID’s for the string aaabbb.
Definition of a PDA:
A Pushdown Automaton (PDA) is a computational model that extends a Finite Automaton (FA)
by adding a stack. This stack allows the PDA to store and retrieve information, giving it more
computational power than an FA.
Explanation:
The PDA pushes an 'A' onto the stack for each 'a' it reads. Then, when it reads 'b's, it pops 'A's
from the stack. Finally, if the number of 'a's and 'b's is equal (indicated by an empty stack), it
moves to the accepting state.
1. Understand the problem: The task is to design a PDA that accepts strings with an
equal number of 'a's followed by an equal number of 'b's.
2. Choose the states:
o q0: The initial state where the PDA starts.
o q1: The state for processing 'b's after reading 'a's.
o q2: The accepting state, signifying that the input is valid.
3. Choose the stack alphabet:
o A: Used to count the 'a's.
o Z: The initial stack symbol.
4. Define the transitions: These rules define how the PDA moves between states and
manipulates the stack based on the input:
o For each 'a' read, push an 'A' to count it.
oFor each 'b' read, pop an 'A' if it's on top of the stack.
oIf all 'a's are matched with 'b's, move to the accepting state.
5. Draw the transition diagram: This visually represents the PDA's operation.
6. Test the PDA: Use a string like "aaabbb" and trace the IDs to see if the PDA accepts
it.
7. Explain the logic: The PDA uses the stack to remember the count of 'a's and
ensures that an equal number of 'b's follow them.
Proof:
Example:
S -> aSb
S -> ε
1. Start in state q0 with the stack containing only the start symbol Z0.
2. For each 'a' read from the input:
o Push an 'A' onto the stack and remain in state q0.
3. When the first 'b' is read:
o Transition to state q1 and pop an 'A' from the stack.
4. For each subsequent 'b' read:
o Pop an 'A' from the stack and remain in state q1.
5. If the input is empty and the stack contains only Z0:
o Transition to state q2 (the accept state).
Module 4
22.Explain Pumping lemma for context free language. Also explain application of CFG
The pumping lemma for context-free languages is a tool used to prove that certain languages are
not context-free. It states that any sufficiently long string in a context-free language can be
"pumped" - divided into specific parts that can be repeated to generate infinitely many other
strings that also belong to the language.
Formal Definition:
If A is a context-free language, then there is a number p (the pumping length) where if s is any
string in A of length at least p, then s may be divided into five pieces s = uvxyz satisfying these
conditions:
Applications of CFGs
Context-free grammars have many applications in computer science and other fields:
Is L = {0ⁿ1ⁿ2ⁿ | n ≥ 1} Context-Free?
The language L = {0ⁿ1ⁿ2ⁿ | n ≥ 1} is not context-free. This can be proven using the pumping
lemma.
24.Explain Pumping Lemma for the non context free languages. Show that the given
language is non context free; L = {ww |w €{0, 1}*} is not CFL.
25.Use Pumping Lemma to show that aⁱbʲcᵏ for i<j<k for CFL.
The language aⁱbʲcᵏ where i < j < k is not context-free. This can be proven using the pumping
lemma.
Proof: