Course Notes on Formal Languages and Compilers
Course Notes on Formal Languages and Compilers
Course Notes on Formal Languages and Compilers
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
22<br />
Backward Deterministic Regular Grammars<br />
5<br />
A regular grammar is called backward deterministic if all rules have different<br />
right h<strong>and</strong> sides. These grammars have the property that the derivati<strong>on</strong> can<br />
be easily c<strong>on</strong>structed by working backwards - see previous examples.<br />
Recogniti<strong>on</strong> Algorithm for Backward Deterministic Regular Grammars<br />
10<br />
Say we wish to recognise whether a string<br />
a 1 a 2 ..........a n ∈ L(G)<br />
where each a j ∈ T<br />
We proceed as follows:<br />
15<br />
20<br />
25<br />
1) Initially set u to Aa 1 a 2 ..........a n where A is the n<strong>on</strong>terminal<br />
such that A→ε.<br />
If A exists it is unique.<br />
If no such A exists then L(G)= ∅,<br />
<strong>and</strong> therefore a 1 a 2 ..........a n ∉L(G).<br />
2) While u can be reduced by a rule, do the reducti<strong>on</strong> (<strong>and</strong> repeat process)<br />
that is if u has the form Aa j a j+1 ........... a n <strong>and</strong> B→Aa j is a rule, then reduce<br />
u to Ba j+1 .........a n . Note, if such a rule exists, it is unique as all rules have<br />
different right h<strong>and</strong> sides.<br />
3) Once step 2 can't be repeated we can say<br />
a 1 a 2 ..........a n ∈L(G) if <strong>and</strong> <strong>on</strong>ly if u∈S<br />
(the start set of G).<br />
30<br />
Theorem: C<strong>on</strong>versi<strong>on</strong> to backward deterministic form<br />
Any regular grammar G can be transformed into a backward deterministic<br />
regular grammar G' such that L(G') = L(G).<br />
35<br />
40<br />
(What this means is that any regular language is easily rec<strong>on</strong>gnised by a<br />
sequential syntax analysis process).<br />
Proof: Let G = (N, T, P, S) be a regular grammar. A backward deterministic<br />
regular grammar. G' = (N', T, P', S') is c<strong>on</strong>structed from G using the<br />
following method which is called the set c<strong>on</strong>structi<strong>on</strong>.<br />
A n<strong>on</strong> terminal of G' is a n<strong>on</strong> empty set of n<strong>on</strong> terminals of G,<br />
45<br />
that is, N' = {U: U ⊆ N <strong>and</strong> U ≠ ∅}.<br />
Equivalently, U ∈ N' if <strong>and</strong> <strong>on</strong>ly if U ⊆ N <strong>and</strong> U ≠ ∅.