Chapter 4 - C (Syntax Free Grammar)
Chapter 4 - C (Syntax Free Grammar)
Recall:
Conversion:
G’ non left recursive
G left recursion
is equivalent to G
L(G’)=L(G)
Left Recursion of 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
p1 pn
∃ a series of derivations A … Aα
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
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
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
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 → ε, …}>