Left Linear Grammar
Explore left linear grammars — the mirror image of right linear grammars — where every production places the non-terminal at the left end, and understand how they relate to right linear grammars and regular languages.
Definition
A left linear grammar (also called a left regular grammar) is a grammar G = (V, T, P, S) where every production rule has one of these three forms:
| Form | Meaning |
|---|---|
| A → Ba | Non-terminal B on the left, terminal a on the right |
| A → a | Just a single terminal (or any string of terminals) |
| A → ε | The empty string |
The defining feature: the non-terminal always appears at the left end of the right-hand side (if it appears at all).
Like right linear grammars, left linear grammars also generate exactly the regular languages. The two types of grammars are equally powerful — they differ only in the direction from which they build strings.
Example 1 — Strings of a's Ending in b
Language: L = {aⁿb | n ≥ 0} = {b, ab, aab, aaab, ...}
Left Linear Grammar:
A → ε: A can be the empty string (generating zero a's)A → Aa: A generates one more 'a' on the right (builds up a string of a's)S → Ab: Append 'b' to whatever A generates
Derivation of "aab":
Why does this work?
A → Aa | ε makes A generate any string in {aⁿ | n ≥ 0}:
- A → ε → A = "" (zero a's)
- A → Aa → εa → A = "a" (one a)
- A → Aa → Aaa → εaa → A = "aa" (two a's)
Then S → Ab appends a 'b', giving {aⁿb | n ≥ 0} ✓
Notice how the grammar builds from the inside out, growing the string of a's to the right of A, then finally removing A when done. This is the opposite of right linear grammars, which build left to right one step at a time.
Example 2 — Strings Starting with ab
Language: L = {ab · w | w ∈ {a, b}*} — strings that start with "ab" Examples: ab, aba, abb, abaa, abab, abba, ...
Left Linear Grammar:
Hmm, this is getting complex. Let me use a cleaner approach.
Because left-linear grammars build strings from right to left (the non-terminal is on the left side), it is often easier to think of them as "suffix grammars" — they describe what comes at the END of the derivation and build leftward.
Cleaner grammar for ab · {a,b}*:
Derivation of "abba":
Reading the derivation: The rightmost characters are appended first. S reads right-to-left over the string. A marks the point where the prefix "ab" is finally produced.
Left vs Right Linear — Side by Side
The same regular language can always be expressed by both a left linear grammar and a right linear grammar. The difference is purely structural — the direction from which the non-terminal "moves" through the string.
Language: L = {aⁿb | n ≥ 0}
Language: L = {a, b}* (all strings over {a, b}, including ε)
Formal Comparison Table
| Property | Right Linear Grammar | Left Linear Grammar |
|---|---|---|
| Production form | A → aB or A → a | A → Ba or A → a |
| Non-terminal position | Right end | Left end |
| String built | Left to right | Right to left (or inside-out) |
| Language class generated | Regular | Regular |
| Equivalent automaton | NFA (forward reading) | NFA (reverse reading) |
| Interconvertible? | Yes — reverse the string, swap types | Yes |
The Conversion Trick
Every left linear grammar can be converted to an equivalent right linear grammar by reversing the language:
- Replace each left linear rule
A → BawithA' → aB'(mirror the rule) - Reverse the roles of start and accepting states
This works because reversing a regular language always gives another regular language.
Regular Grammars — The Full Picture
Both right linear and left linear grammars are collectively called regular grammars. They sit at the bottom of the Chomsky Hierarchy (Type 3) and are the least powerful class of grammars.
Why Regular Grammars Matter in Compiler Design
The lexical analysis (scanner/tokeniser) phase of a compiler uses regular grammars:
The regular grammar describes the structure of each token type. The compiler tool lex (or flex) takes these descriptions and automatically builds the DFA that recognises them.
Key Takeaway
| Grammar Type | Can describe | Cannot describe |
|---|---|---|
| Regular (left or right linear) | Token patterns, simple repetition | Nested structures, balanced brackets |
| Context-Free | Most programming language syntax | aⁿbⁿcⁿ, context-sensitive rules |
| Context-Sensitive | More complex languages | Turing-complete computations |
Regular grammars are simple, fast, and sufficient for lexical analysis. More power (context-free, context-sensitive) is needed for parsing full programs.