0% found this document useful (0 votes)
4 views

Chapter 4 - C (Syntax Free Grammar)

Uploaded by

newsetup48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 4 - C (Syntax Free Grammar)

Uploaded by

newsetup48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Grammar

Recall:

A CFG grammar is G=<Σ, N, S, P> where :


- Σ is a set of terminal symbols
- N is a set of nonterminal symbols
- S is the axiom
- P is the set of productions (rules)
P={p / p : X0→ α1X1α2 … αnXnαn+1 where
∀ i∈[0..n], Xi ∈ N and
∀ i∈[1..n+1], αi∈Σ*.
Derivation :
Let :
- αXβ ∈ (Σ ∪ N)* , X ∈N
- p : X→γ,
then : αXβ → αγβ is a derivation.
p p p
a sequence : α1X1β1 1 α2X2β22 …n αn is represented by α1X1β1 * αn
Left Recursion of grammars

Definition: A grammar G=<Σ, N, S, P> is left recursive if and only if


∃ a series of derivation
p1 pn
A  …  Aα
n= 1 ⇔ direct recursion
n > 1 ⇔ Indirect recursion

Example : G1=<{a,b},{E}, E, {E →Ea, E →b}>


G2=<{a,b},{E, F}, E, {E →Fa, F →Eb, F → b}>

Conversion:
G’ non left recursive
G left recursion
is equivalent to G
L(G’)=L(G)
Left Recursion of grammars

Definition: A grammar G=<Σ, N, S, P> is directly left recursive


if and only if there exist a production p in G of the form :
p
A→Aα

Algorithm to eliminate direct recursion


- Grouping the productions with non terminal as follows:
A → Aα1|…| Aαn|β1 | … | βm
Where the βi do not begin with A
- Replace the A-productions with:
A → β1A’ | … | βmA’
A’ → α1 A’|…|αn A’ | ε
Left recursive grammars

Previous Example:
G=<{a,b,c,d},{E},E,{E→Ea, E→Eb, E→c, E→d}> is left recursive

Transformation of G:

-Grouping : E→Ea | Eb | c | d
-Replace :
G’=<{a,b,c,d},{E, E'},E,
{E→cE' | dE'
E'→aE' | bE' | ε}>
not left recursive
Left recursive grammars

Definition: A grammar G=<Σ, N, S, P> is indirect left recursive if and only if

p1 pn
∃ a series of derivations A  …  Aα

Example : G=<{a,b,c,d},{S,B}, S, {S→Ba | b, B→cB |Sd | c}>


G is indirect left recursive:
S→Ba →Sda

Transformation:
G’ non left recursive
G left recursive
and equivalent to G
L(G’)=L(G)
Left recursive grammars
Principles
- number the non-terminals: A1 … An
- convert the productions with: Ai → Ak α where k>=i Indice k should be
greater or equal i
Note: the productions with the form Ai → β α where β is terminal is not modified

Algorithm
Input : G without cycle and without empty productions and left recursive
sortie: G' not left recursive and equivalent to G
for i=1 to n do {
for j=1 to i-1 do {
replace each production Ai → Aj α with the productions
Ai → β1 α|…|βk α where Aj → β1 |…|βk are the current productions
}
Eliminate the direct left recursion from Ai-productions
}
Left Recursion of gammars

Example : G=<{a,b,c,d},{S,B}, S, {S→Ba | b, B→cB |Sd | c}>


G is indirect left recursion

Transformation:
-numbering : S A1
B A2

-Replace : Ai → Aj α Aj → β1 |…|βk

Ai → β1 α|…|βk α

G=<{a,b,c,d},{S,B}, S, {S→Ba | b,
B→cB |Sd | c}>
Left recursion of grammars

G=<{a,b,c,d},{S,B}, S, {S→Ba | b, B→cB |Sd | c}>

Algorithm:
S↔A1, B↔A2
- i=1, j=1 to 0
- Do nothing
- i=2, j=1 to 1
B→ Sd S→Ba | b

B→Bad | bd
G=<{a,b,c,d},{S,B}, S, {S→Ba | b, B→Bad |bd | cB | c}>
Left Recursion of grammars

G=<{a,b,c,d},{S,B}, S, {S→Ba | b, B→Bad |bd | cB | c}>

Elimination of direct recursion :


G=<{a,b,c,d},{S,B, B'}, S, {S→Ba | b,
B → bdB'| cBB' | cB'
B' →ad B' | ε}>

Exercise : eliminate the recursion in the grammar :


G=<{a,b,c,d},{S,A, B}, S, {S→BA ,
A→Aa |Ab | a
B→SB|bB|ε}>
Left factoring of grammars
Definition: A grammar G=<Σ, N, S, P> is left factoring if and only if
∃ two productions p1 et p2 having the same left member and
a common prefix (terminal symbol) in the right members
We can say:
p1 : X → aα and p2 : X → aβ where a is a terminal symbol

Example : G=<{select, from, where, and, or, a,…,z,0,…,9,',", …},


{Request, ListFields, ListTables, ListConditions}, Request,
{Request →select ListFields From ListTables where ListConditions,
Requete →select ListFields From ListTables, …}>
G is non left factoring
Transformation:
G’ left factoring
G is non left
factoring is equivalent to G
L(G’)=L(G)
Left factoring of grammars
Example : G=<{select, from, where, and, or, a,…,z,0,…,9,',", …},
{Requeste, ListFields, ListTables, ListConditions}, Request,
{Request →select ListFields From ListTables where ListConditions,
Request →select ListFields From ListTables, …}>

G is non left factoring

Transformation:
G'=<{select, from, where, and, or, a,…,z,0,…,9,',", …},
{Request, RequestSeries, ListFields, ListTables, ListConditions}, Request,
{Request →select ListFields From ListTables RequestSeries
RequestSeries → where ListConditions,
RequestSeries → ε, …}>

G' is left factoring and L(G')=L(G)

You might also like