leftmost derivation


Let G=(Σ,N,P,σ) be a context-free grammar. Recall that a word v is generated by G if it is

  • •

    a word over the set T:=Σ-N of terminals, and

  • •

    derivablePlanetmathPlanetmath from the starting symbol σ.

The second condition simply says that there is a finite sequencePlanetmathPlanetmath of derivation steps, starting from σ, and ending at v:

σ=v0→v1→⋯→vn=v

For each derivation step vi→vi+1, there is a production X→w in P such that

vi = a⁢X⁢b,
vi+1 = a⁢w⁢b.

Note that X is a non-terminal (an element of N), and a,b,w are words over Σ.

Definition. Using the notations above, if X is the leftmost non-terminal occurring in vi, then we say the derivation step vi→vi+1 is leftmost. Dually, vi→vi+1 is rightmost if X is the rightmost non-terminal occurring in vi.

Equivalently, vi→vi+1 is leftmost (or rightmost) if a (or b) is a word over T.

A derivation is said to be leftmost (or rightmost) if each of its derivation steps is leftmost (or rightmost).

Example. Let G be the grammar consisting of a,b as terminals, σ,X,Y,Z as non-terminals (with σ as the starting symbol), and σ→X⁢Y, X→a, Y→b, σ→Z⁢Y, and Z→X⁢σ as productions. G is clearly context-free.

The word a2⁢b2=a⁢a⁢b⁢b can be generated by G by the following three derivations:

  1. 1.

    σ→Z⁢Y→X⁢σ⁢Y→X⁢X⁢Y⁢Y→X⁢a⁢Y⁢Y→X⁢a⁢Y⁢b→X⁢a⁢b⁢b→a⁢a⁢b⁢b,

  2. 2.

    σ→Z⁢Y→X⁢σ⁢Y→a⁢σ⁢Y→a⁢X⁢Y⁢Y→a⁢a⁢Y⁢Y→a⁢a⁢b⁢Y→a⁢a⁢b⁢b,

  3. 3.

    σ→Z⁢Y→Z⁢b→X⁢σ⁢b→X⁢X⁢Y⁢b→X⁢X⁢b⁢b→X⁢a⁢b⁢b→a⁢a⁢b⁢b.

The second is leftmost, the third is rightmost, and the first is neither.

Note that in every derivation, the first and last derivation steps are always leftmost and rightmost.

Remarks.

  • •

    One of the main properties of a context-free grammar G is that every word generated by G is derivable by a leftmost (correspondingly rightmost) derivation, which can be used to show that for every context-free language L, there is a pushdown automaton accepting every word in L, and conversely that the set of words accepted by a pushdown automaton is context-free.

  • •

    Leftmost derivations may be defined for any arbitrary formal grammar G satisfying the condition

    (*) no terminal symbols occur on the left side of any production.

    Given two words u,v, we define u⇒Lv if there is a production A→B such that u=x⁢A⁢y and v=x⁢B⁢y such that x is a terminal word. By taking the reflexive transitive closure of ⇒L, we have the leftmost derivation ⇒L*. It can be shown that for any grammar G satisfying condition (*), the languagePlanetmathPlanetmath LL⁢(G) consisting of terminal words generated by G via leftmost derivations is always context-free.

References

  • 1 S. Ginsburg The Mathematical Theory of Context-Free Languages, McGraw-Hill, New York (1966).
  • 2 D. C. Kozen Automata and Computability, Springer, New York (1997).
Title leftmost derivation
Canonical name LeftmostDerivation
Date of creation 2013-03-22 18:54:50
Last modified on 2013-03-22 18:54:50
Owner CWoo (3771)
Last modified by CWoo (3771)
Numerical id 7
Author CWoo (3771)
Entry type Definition
Classification msc 68Q45
Classification msc 68Q42
Defines rightmost derivation