Processing math: 100%
Close
Close Window

OpenDSA Stand-alone Modules

Chapter 0 modules

Show Source |    | About   «  0.275. PDAs and Context Free Languages   ::   Contents   ::   0.277. Properties of Context-Free Languages  »

0.276. Deterministic Pushdown Automata

0.276.1. Deterministic Pushdown Automata

Definition: A PDA M=(Q,Σ,Γ,δ,q0,z,F) is deterministic if for every qQ, aΣ{λ}, bΓ:

1. δ(q,a,b) contains at most one element
2. if δ(q,λ,b) is not empty, then δ(q,c,b) must be empty for every cΣ.

Definition: L is a deterministic context-free language (DCFL) if and only if there exists a deterministic PDA M such that L=L(M).

Example

The language L={anbn|n0} is a deterministic CFL.

Proof: The PDA M=({q0,q1,q2},{a,b},{0,1},δ,q0,0,{q0}) with
δ(q0,a,0)={(q1,10)},
δ(q1,a,1)={(q1,11)},
δ(q1,b,1)={(q2,λ)},
δ(q2,b,1)={(q2,λ)},
δ(q2,λ,0)={(q0,λ)}

accepts the given language. It satisfies the conditions for being deterministic.

Note that this machine DOES have λ transitions. The key point is that there is still only one choice (because of what is sitting on the top of the stack). In that sense, it is not merely a “free ride” transition.

Example

Our previous PDA for {wwR|wΣ+},Σ={a,b} is nondeterministic.

It contains these transitions:
δ(q0,a,a)={(q0,aa)}
δ(q0,λ,a)={(q1,a)}

This violates our conditions for determinism. (Do you see why?)

Now, this fact that we have a PDA that is not deterministic certainly does not prove that {wwR|wΣ+},Σ={a,b} is not a deterministic CFL.

But, there are CFL’s that are not deterministic. And we will see that this is one of them. This makes intuitive sense. How can we, deterministically, know when to switch from w to wR when scanning from left to right through the input?

Example:

L={anbn|n1}{anb2n|n1} is a CFL and not a DCFL.

Obviously, both languages are CFL. And obviously, their union is CFL. But imagine how the “obvious” PDA works: The start state transitions to the “correct” machine to recognize a sting in either language. But how can we do this deterministically? We would need a completely different approach.

While that is not a proof that the language is not deterministic, here is one.

Theorem: L1L2 is not a DCFL (because anbncn is not a CFL).

Proof::

L={anbn:n1}{anb2n:n1}
It is easy to construct a NPDA for {anbn:n1} and a NPDA for {anb2n:n1}. These two can be joined together by a new start state and λ-transitions to create a NPDA for L. Thus, L is CFL.
Now show L is not a DCFL. Assume that there is a deterministic PDA M such that L=L(M). We will construct a PDA that recognizes a language that is not a CFL and derive a contradiction.
Construct a PDA M as follows:

Todo

type: Figure

Show figure

1. Create two copies of M:M1 and M2. The same state in M1 and M2 are called cousins.
2. Remove accept status from accept states in M1, remove initial status from initial state in M2. In our new PDA, we will start in M1 and accept in M2.
3. Outgoing arcs from old accept states in M1, change to end up in the cousin of its destination in M2. This joins M1 and M2 into one PDA. There must be an outgoing arc since you must recognize both anbn and anb2n. After reading n b’s, must accept if no more b’s and continue if there are more b’s.
4. Modify all transitions that read a b and have their destinations in M2 to read a c.
This is the construction of our new PDA.
When we read anbn and end up in an old accept state in M1, then we will transfer to M2 and read the rest of anb2n. Only the b’s in M2 have been replaced by c’s, so the new machine accepts anbncn.
The language accepted by our new PDA is anbncn. But this is not a CFL. Contradiction! Thus there is no deterministic PDA M such that L(M)=L. Q.E.D.

Based on this information, we now can update our model of the Formal Languages Universe.

lt8hier

0.276.2. Grammars for Deterministic Context-free Languages

Now we know that:

  • All CFL can be generated by a CFG, and implemented by a NPDA.

  • Not all CFL can be generated using a DPDA.

  • So some CFG are associated with only non-deterministic PDAs. Nondeterminism gives us something more in terms of capability.

Why do we care? Because we want to parse efficiently. This means, we want to quickly determine if a given string can be generated from a given grammar. Determinism seems to be a fundamental requirement if we hope to do this.

Think of a PDA as a parsing device. No backtracking requires that we can make a decision at every step on what to do next. This is the same as knowing which grammar production comes next. Clearly this is linear time when the grammar has been simplified (no unit productions), because every derivation step consumes an input character. Well… except for λ productions. But we will see soon that these are not really a problem for linear-time processing.

0.276.2.1. Top-down Parsing with Lookahead

Start with the start symbol Scan left-to-right through the string. At each step, we want only to follow one rule when we look at the current character. Perhaps we don’t see a production for the current character, but instead pop something off the stack (λ production). This is why λ productions are still linear, if we don’t put too much on the stack when we process a character.

0.276.2.2. S-grammars

Recall that an S-grammar has all productions of the form:
Aax
where AV, aT, and xV AND any pair (A,a) can occur in at most one rule.

Obviously this can be parsed efficiently. But, s-grammars are more restrictive than we want. Lots of useful language constructs cannot be defined using an s-grammar. We want to generalize as much as we can to capture a broader subset of CFLs

0.276.2.3. LL(k) Grammars

LL means “left-to-right” and “left-most derivation” is constructed. k means that we can look ahead at most k1 characters. Every s-grammar is LL, but so are more grammars.

Consider this grammar:
SaSbab

This is not an s-grammar. But, this is an LL grammar. By looking at the next two characters, we always know which rule to apply.

If we see ab, then apply Sab.
<< What gets consumed, what goes on the stack? >>
Otherwise, apply SaSb
<< What gets consumed, what goes on the stack? >>
Consider this grammar:
SSSaSbab

This is a useful grammar! It captures nested parentheses. This is not an LL(k) grammar for any k. (Can you see why not?)

Just because the grammar is not LL(k) does not mean that the language might not be deterministic. The reasoning for why this was not LL(k) should help you to see how to fix the grammar.

Consider this grammar: SaSbSλ
This is LL.
Example: Derive w=abab.
SaSbSabSabaSbSababSabab.
When the input next input symbol is a, we must use SaSbS.
When the input next input symbol is b or string is empty, we must use Sλ.
One last problem: This grammar accepts the empty string. If we don’t like that, then there is an easy fix. Just define a new start symbol that avoids the λ production.
S0aSbS
SaSbSλ

   «  0.275. PDAs and Context Free Languages   ::   Contents   ::   0.277. Properties of Context-Free Languages  »

nsf
Close Window