Context-free grammars and push-down automata
गेट CS95 मिनट में पढ़ें
Context-free grammars
Definition. A CFG is (V,Σ,P,S), where variables V can be replaced according to productions, terminals Σ form output strings, P is the production set, and S is the start variable. A derivation repeatedly replaces one variable; the generated language contains terminal strings derivable from S.
Example. S→aSb | ε generates {aⁿbⁿ:n≥0}.
Exam point. A production such as A→aAb has one variable on its left side, which is the defining CFG restriction.
Solved examples
Example 1 — derive a string. Use S→aSb | ε to derive aaabbb.
S⇒aSb.- Replace the inner S:
aSb⇒aaSbb. - Replace again:
aaSbb⇒aaaSbbb. - Use S→ε:
aaaSbbb⇒aaabbb. - The derivation has three recursive uses and one terminating use.
Example 2 — determine membership. Does the grammar S→aSb|ε generate aabbb?
- Each recursive production adds exactly one a on the left and one b on the right.
- The terminating production removes S without adding symbols.
- Therefore every generated string has equal counts of a and b and all a's precede b's.
- No; the counts differ.
Parse trees and ambiguity
Definition. A parse tree records production applications with variables as internal nodes and terminals as leaves. A grammar is ambiguous if some string has two distinct parse trees, equivalently two distinct leftmost derivations or two distinct rightmost derivations.
Example. E→E+E | E*E | id is ambiguous because id+id*id can group in two ways.
Exam point. Multiple derivation orders can describe the same parse tree; ambiguity requires distinct structures, not merely different replacement order.
Solved examples
Example 1 — two parses. Show ambiguity for id+id*id in the expression grammar above.
- One root split uses
E→E+E; the right child derivesid*id. This givesid+(id*id). - Another root split uses
E→E*E; the left child derivesid+id. This gives(id+id)*id. - Both yield the same terminal string but have different root operators.
- The grammar is ambiguous.
Example 2 — remove ambiguity with precedence. Give a grammar where * binds tighter than + and both are left associative.
- Let
E→E+T | Thandle addition. - Let
T→T*F | Fhandle multiplication. - Let
F→(E) | idhandle atoms. - The separated levels force multiplication below addition in the parse tree; left recursion gives left associativity.
Leftmost and rightmost derivations
Definition. A leftmost derivation always expands the leftmost remaining variable; a rightmost derivation expands the rightmost. For a fixed parse tree, each convention gives a unique derivation sequence.
Example. For S→AB, A→a, B→b, the leftmost sequence is S⇒AB⇒aB⇒ab.
Exam point. Keep terminal symbols fixed while selecting the next variable to expand.
Solved examples
Example 1 — leftmost derivation. Grammar S→AB, A→aA|a, B→b; derive aaab leftmost.
S⇒AB.- Expand A:
AB⇒aAB. - Expand the leftmost A again:
aAB⇒aaAB. - Terminate A:
aaAB⇒aaaB. - Expand B:
aaaB⇒aaab. Answer: the displayed leftmost derivation.
Example 2 — rightmost derivation. For the same grammar, derive aab rightmost.
S⇒AB.- Expand rightmost B:
AB⇒Ab. - Expand A and terminate it as
aA:Ab⇒aAb. - Replace A with a:
aAb⇒aab. - Answer:
S⇒AB⇒Ab⇒aAb⇒aab.
Nullable variables, FIRST, and FOLLOW
Definition. A variable is nullable if it derives ε. FIRST(X) contains terminals that can begin strings derived from X, plus ε if X is nullable. FOLLOW(A) contains terminals that can immediately follow A in some sentential form; $ marks the end after the start variable.
Example. For S→AB, A→a|ε, B→b, FIRST(S)={a,b}.
Exam point. When a prefix is nullable, FIRST must continue to the next symbol; FOLLOW uses FIRST of the suffix and may inherit FOLLOW of the left-hand side.
Solved examples
Example 1 — compute FIRST. Grammar S→AB, A→a|ε, B→b|c.
- FIRST(A)={a,ε}; FIRST(B)={b,c}.
- S begins with A when A produces a.
- Because A can vanish, S may also begin with B's first symbols.
- FIRST(S)={a,b,c}; ε is not included because B is not nullable.
Example 2 — compute FOLLOW. For the grammar above, calculate FOLLOW(A) and FOLLOW(B), with S start.
- In
S→AB, B follows A, so add FIRST(B)={b,c} to FOLLOW(A). - B is at the right end, so FOLLOW(S) is added to FOLLOW(B).
- FOLLOW(S) contains
$. - FOLLOW(A)={b,c}; FOLLOW(B)={$}.
Eliminating ε-productions and unit productions
Definition. An ε-production has right side ε; a unit production has one variable on the right. Standard simplification computes nullable variables, adds alternatives that omit nullable occurrences, removes ε rules while preserving ε at the start when needed, and replaces unit chains by their non-unit consequences.
Example. If A→BC and B is nullable, add A→C before removing B→ε.
Exam point. Preserve whether the original language contains ε by using a fresh start symbol when necessary.
Solved examples
Example 1 — remove ε. Grammar S→AB, A→a|ε, B→b.
- A is nullable.
- For
S→AB, add the alternativeS→Bomitting A. - Remove
A→ε; retainA→a, and productionsS→AB|B,B→b. - The language remains
{ab,b}.
Example 2 — remove a unit rule. Grammar S→A|b, A→a|B, B→c.
- Unit chain from S reaches A and B.
- Non-unit alternatives reachable are
a,c, andb. - Replace S's unit productions with
S→a|b|c; replace A→B by A→c. - No unit production remains.
Chomsky normal form
Definition. A CFG is in CNF if every production is A→BC for variables B,C, or A→a for a terminal a; a special start rule S₀→ε may be allowed when ε belongs to the language. CNF conversion removes useless, ε, and unit rules, then isolates terminals and binarizes long right sides.
Example. A→aBC can become A→X_a D, D→BC, X_a→a.
Exam point. The start variable should not appear on a right side if the form permits a separate ε rule.
Solved examples
Example 1 — binarize. Convert the right side ABC into binary branching.
- Introduce D with
D→BC. - Replace
A→ABCbyA→AD. - The right side now contains exactly two variables.
Example 2 — isolate terminal. Convert A→aB to CNF form.
- Introduce
X_a→a. - Replace the original rule with
A→X_aB. - Both productions satisfy CNF patterns.
Push-down automata
Definition. A PDA is a finite-state machine with a stack. A transition may inspect the input symbol (or ε), inspect/pop the stack top, and push a string; acceptance may be by final state or empty stack. Nondeterministic PDAs recognize exactly the context-free languages.
Example. For {aⁿbⁿ}, push one marker per a, then pop one per b and accept when the stack returns to its bottom marker after input ends.
Exam point. A stack is LIFO; it can match nested structure but cannot generally compare two unbounded counts separated in a way requiring two independent counters.
Solved examples
Example 1 — PDA trace. Sketch a PDA run for aaabbb with bottom symbol Z and marker A.
- Start in qpush with Z. Read a and push A: stack AZ.
- Read the second and third a, pushing each: AAAZ.
- On first b, switch to qpop and pop A; repeat for remaining b symbols.
- After three b's, stack is Z and input is exhausted.
- Accept by final state or empty stack, according to the chosen convention.
| Input consumed | State phase | Stack (top at left) |
|---|---|---|
| ε | push | Z |
| a | push | AZ |
| aa | push | AAZ |
| aaa | push | AAAZ |
| aaab | pop | AAZ |
| aaabb | pop | AZ |
| aaabbb | accept | Z |
Example 2 — reject a count mismatch. Trace aabbb.
- Two a's push two A markers.
- The first two b's pop both markers.
- The third b requires another A but sees Z.
- No valid transition consumes the extra b; reject.
Acceptance by final state versus empty stack
Definition. For nondeterministic PDAs, acceptance by final state and by empty stack define the same class of languages, though a given PDA may need transformation. Empty-stack acceptance requires all input consumed and the stack emptied; final-state acceptance requires all input consumed and a final state reached.
Example. A PDA may use a bottom marker and move to a final state once it sees only that marker after input ends.
आपने इस नोट का मुफ़्त 30% हिस्सा पढ़ लिया
आगे पढ़ने के लिए Google से साइन इन करें। यह मुफ़्त है, एक टैप में होता है, और साथ में मिलता है:
- इस नोट से 6 अभ्यास सवाल
- हर नोट पूरा, साथ में PDF लाइब्रेरी
- आधिकारिक पिछले पेपर समय-सीमा वाले टेस्ट की तरह, व्याख्या के साथ
- डाउट के लिए AI ट्यूटर, हिंदी या अंग्रेज़ी में
- आपकी प्रगति, गलतियाँ और स्टडी प्लान हर डिवाइस पर सेव