Lab Session #11 #
Agenda #
Generative Grammars Chomsky Hierarchy Context-Free Grammars: Backus-Naur Form
Models #
Automata (operational models): #
- Models suitable to recognize/accept, translate, compute language: receive an input string and process it.
Grammar (generative models): #
- Models suitable to describe how to generate a language: set of rules to build phrases of a language.
Grammars #
A grammar is a set of rules to produce strings. #
A grammar is a tuple: \( \langle V_N, V_T, P, S \rangle \) , where:
\( V_N \) is the non-terminal alphabet
\( V_T \) is the terminal alphabet;
\( P \) is the terminal alphabet;
\( S \) is the terminal alphabet;
\( V = V_N \cup V_T \) the alphabet;
\( P \subseteq \left(V^* \cdot V_N \cdot V^*\right) \times V^* \) is the (finite) set of rewriting rules of production;
\( S \in V_N \) is a particular element called axiom or initial symbol.
A grammar \( \langle V_N, V_T, P, S\rangle\) generates a language on V_T.
Terminal symbols are elementary symbols - cannot be broken down into smaller units i.e. cannot be changed using the production rules of the grammar.
Non-terminal symbols - can be replaced by groups of terminal and non-terminal symbols according to the production rules.
Production Rule #
Let \( G = \langle V_N, V_T, P, S\rangle \)
be a grammar.
A production rule \( \alpha \rightarrow \beta\) is an element of \(P\) , where:
\(\alpha \in V^* \cdot V_N \cdot V^*\) is a sequence of symbols including at least one non-terminal symbol.
\(\beta \in V^*\) is a (potentially empty) sequence of (terminal or non-terminal) symbols.
Chomsky Hierarchy #
Chomsky hierarchy | Grammars | Languages | Miniamal automaton |
---|---|---|---|
type 0 | Unrestricted | Recursively enumerable | Turing machine |
type 1 | Context-sensitive | Context-sensitive | LBA |
type 2 | Context-free | Context-free | NDPDA |
type 3 | Regular | Regular | FSA |
Strictly Regular grammars (type 3) #
Production rules are restricted to a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal, possibly
- followed by a single non-terminal - right grammar
- preceded by a single non-terminal - left grammar
Example: #
Generate language with the strings of alternating a’s and b’s:
- \( V_N = \{S, A, B\}; \hspace{0.2cm} \)
- \( V_T = \Sigma_1 = \{a, b\} \)
Set of Production rules P: #
- \( S \rightarrow A \)
- \( S \rightarrow B \)
- \( A \rightarrow aB \)
- \( A \rightarrow \epsilon \)
- \( B \rightarrow bA \)
- \( B \rightarrow \epsilon \)
Strictly Regular grammars #
Strictly Right regular grammar #
A right regular grammar is a formal grammar, such that all the production rules in P are of one of the following forms:
- \( A \rightarrow b \)
- \( A \rightarrow bB \)
- \( A \rightarrow \epsilon \)
Strictly Left regular grammar #
A left regular grammar is a formal grammar, such that all the production rules in P are of one of the following forms:
- \( A \rightarrow b\)
- \(A \rightarrow Bb\)
- \(A \rightarrow \epsilon\)
Extended regular grammars #
Extended Right regular grammar #
A right regular grammar is a formal grammar, such that all the production rules in P are of one of the following forms:
- \(A \rightarrow b\)
- \(A \rightarrow wB\)
- \(A \rightarrow \epsilon\)
Extended Left regular grammar #
A left regular grammar is a formal grammar, such that all the production rules in P are of one of the following forms:
- \(A \rightarrow b\)
- \(A \rightarrow Bw\)
- \(A \rightarrow \epsilon\)
Exercises #
Define Strictly Regular grammars that produce the following languages over the alphabet:
- \(\Sigma_1 = \{a, b\} , \Sigma_2 = \{0, 1\}\)
- \(L_1 = \{0,1\}^*\)
- \(L_2 = \{(aab \hspace{0.2cm} | \hspace{0.2cm} bba)^*\}\)
Solutions #
Solutions
Set of Production rules P:
- \(S \rightarrow \epsilon\)
- \(S \rightarrow 0S\)
- \(S \rightarrow 1S\)
Set of Production rules P:
- \( S \rightarrow \epsilon\)
- \(S \rightarrow aA\)
- \(S \rightarrow bB\)
- \(A \rightarrow aF\)
- \(F \rightarrow bS\)
- \(B \rightarrow bE\)
- \(E \rightarrow aS\)
Homework: #
- \(L_3 = \{(aa \hspace{0.2cm} | \hspace{0.2cm} bb)^*aa \}\)
- \(L_4 = \{(00^* 11^*)\}\)
Context-Free grammars (type 2) #
Defined by rules of the form \(A \rightarrow \gamma\)
where \(A\)
is a non-terminal and \(\gamma\)
is a string of terminals and non-terminals.
Exercises #
Define context-free grammars that produce the following languages over the alphabet:
- \(\Sigma = \{a, b\}\)
- \(L_1 = \{w ∈ \{a, b\}^∗| w = w^R\}\)
- \(L_2= \{a^ib^jc^k | i, j, k \geq 0 and i=j or i=k \} \)
Homework:
- \( L_3 = Generate language with alternating a's and b's \)
- \(L_4 = \{a^nb^nc^m \mid n,m>0\} \cup \{a^nb^mc^m \mid n,m>0\}\)
Solutions #
Solutions
Set of Production rules P:
- \(S \rightarrow O \hspace{0.2cm} | \hspace{0.2cm} E\)
- \(E \rightarrow \epsilon \hspace{0.2cm} |\hspace{0.2cm} aEa \hspace{0.2cm} \hspace{0.2cm} | \hspace{0.2cm} bEb\)
- \(O \rightarrow a \hspace{0.2cm}|\hspace{0.2cm} b \hspace{0.2cm} | \hspace{0.2cm} aOa\hspace{0.2cm} |\hspace{0.2cm} bOb\)
or:
- \(S \rightarrow aSa \hspace{0.2cm} |\hspace{0.2cm} bSb \hspace{0.2cm}|\hspace{0.2cm} a \hspace{0.2cm}|\hspace{0.2cm} b\hspace{0.2cm} |\hspace{0.2cm} e \)
Set of Production rules P:
- \(S \rightarrow XY \hspace{0.2cm} | \hspace{0.2cm} W\)
- \(X \rightarrow aXb \hspace{0.2cm} | \hspace{0.2cm} \epsilon\)
- \(Y \rightarrow cY \hspace{0.2cm} | \hspace{0.2cm} \epsilon\)
- \(W \rightarrow aWc \hspace{0.2cm} | \hspace{0.2cm} Z\)
- \(Z \rightarrow bZ \hspace{0.2cm} | \hspace{0.2cm} \epsilon\)
- \(S \rightarrow bB\hspace{0.2cm}|\hspace{0.2cm}aA\hspace{0.2cm}|\hspace{0.2cm}\epsilon\)
- \(A \rightarrow bB\hspace{0.2cm}|\hspace{0.2cm} \epsilon\)
- \(B \rightarrow aA\hspace{0.2cm}|\hspace{0.2cm} \epsilon\)
- \(S \rightarrow aNbC\hspace{0.2cm}|AbMc\)
- \(N \rightarrow aNb\hspace{0.2cm}| \hspace{0.2cm}\epsilon\)
- \(M \rightarrow bMc\hspace{0.2cm}| \hspace{0.2cm}\epsilon\)
- \(C \rightarrow cC\hspace{0.2cm}|\hspace{0.2cm}\epsilon\)
- \(A \rightarrow aA\hspace{0.2cm}|\hspace{0.2cm} \epsilon\)
Context-Sensitive grammars (type 1) #
The rules of the form \(\alpha A \beta \rightarrow \alpha \gamma \beta\) , where \(A\) is a non-terminal and \(\alpha\) , \(\beta\) and \(\gamma\) are strings of terminals and non-terminals.
- \(\gamma\) must be non-empty
- The rule \( S\rightarrow\epsilon\) is allowed if S does not appear on the right side of any rule
Exercises #
Define context-sensitive grammars that produce the following languages:
- \(L_1 = \{a^ib^jc^id^j \hspace{0.2cm}|\hspace{0.2cm} i,j \geq 1\} \)
- \(L_2 = \{WW \hspace{0.2cm}|\hspace{0.2cm} W ∈ \{a, b\}^∗\}\)
Homework:
- \(L_3 = \{W ∈ \{a, b, c\}^∗| \#(a) = \#(b) = \#(c) and \#(a) ≥1 \} \)
Solutions #
Solutions
Set of Production rules P:
- \(S \rightarrow XY\)
- \(X \rightarrow aXC\hspace{0.2cm} |\hspace{0.2cm} aC\)
- \(Y \rightarrow BYd\hspace{0.2cm} | \hspace{0.2cm}Bd\)
- \(CB \rightarrow CZ\)
- \(CZ \rightarrow BZ\)
- \(BZ \rightarrow BC\)
- \(aB \rightarrow ab\)
- \(Cd\rightarrow cd\)
Set of Production rules P:
- \(S \rightarrow \epsilon \hspace{0.2cm}| \hspace{0.2cm}CD\hspace{0.2cm}|\hspace{0.2cm} EF\)
- \(C \rightarrow aCA \hspace{0.2cm}|\hspace{0.2cm} bCB \hspace{0.2cm}|\hspace{0.2cm} a\)
- \(D \rightarrow aEA \hspace{0.2cm}|\hspace{0.2cm} bEB \hspace{0.2cm}| \hspace{0.2cm}b\)
- \(AD \rightarrow XD\)
- \(BD \rightarrow YD\)
- \(AF \rightarrow XF\)
- \(BF \rightarrow YF\)
- \(AX \rightarrow VX \)
- \(VX \rightarrow VW \)
- \(VW \rightarrow VA\)
- \(VA \rightarrow XA \)
- \(BX \rightarrow WX\)
- \(WX \rightarrow WV \)
Continue..on next slide
Set of Production rules P:
- \(WV \rightarrow WB \)
- \(WB \rightarrow XB \)
- \(AY \rightarrow UY \)
- \(UY \rightarrow UZ \)
- \(UZ \rightarrow UA \)
- \(UA \rightarrow YA \)
- \(BY \rightarrow ZY \)
- \(ZY \rightarrow ZU \)
- \(ZU \rightarrow ZB \)
- \(ZB \rightarrow YB \)
- \(aD \rightarrow aa \)
- \(bD \rightarrow ba \)
- \(aF \rightarrow ab \)
- \(bF \rightarrow bb \)
- \(aX \rightarrow aa \)
- \(bX \rightarrow ba \)
- \(aY \rightarrow ab \)
- \(bY \rightarrow bb \)
Set of Production rules P:
- \(S \rightarrow SABC\hspace{0.2cm}|\hspace{0.2cm}ABC\)
- \(A\rightarrow a\)
- \(AB\rightarrow AQ \)
- \(AQ \rightarrow BQ\)
- \(BQ \rightarrow BA\)
- \(AC \rightarrow AR\)
- \(AR \rightarrow CR\)
- \(CR \rightarrow CA\)
- \(B \rightarrow b\)
- \(BA \rightarrow BP\)
- \(BP \rightarrow AP\)
- \(AP \rightarrow AB\)
- \(BC \rightarrow CZ\)
- \(BZ \rightarrow CZ\)
- \(CZ \rightarrow CB\)
- \(C \rightarrow c\)
- \(CA \rightarrow CX\)
- \(CX \rightarrow AX\)
- \(AX \rightarrow AC\)
- \(CB \rightarrow CY\)
- \(CY \rightarrow BY\)
- \(BY \rightarrow BC\)
Unrestricted grammars (type 0) #
The rules of the form \(\alpha \rightarrow \beta\)
, where \(\alpha\)
and \(\beta\)
are strings of non-terminals and terminals.
The grammars without any limitation on production rules.
\(\alpha\)
at least have one non-terminal
\(\alpha\)
cannot be an empty string
Exercises #
Generate Unrestricted grammars for below languages:
- \(L_1 = \{W | W = a^i\) and \( i = 2^ k \) and \( k > 0\} \)
- \(L_2 = \{a^nb^mc^nd^m \hspace{0.2cm}|\hspace{0.2cm} n>0, m>0\}\)
Solutions #
Solutions
Set of Production rules P:
- \(S \rightarrow LaaYR\)
- \(aX \rightarrow Xaa\)
- \(LX \rightarrow LY \hspace{0.2cm}|\hspace{0.2cm} \epsilon\)
- \(L \rightarrow \epsilon\)
- \(Ya \rightarrow aaY\)
- \(YR \rightarrow XR \hspace{0.2cm}|\hspace{0.2cm} \epsilon\)
- \(R \rightarrow \epsilon\)
Set of Production rules P:
- \(S\rightarrow XY\)
- \(X\rightarrow aXC\hspace{0.2cm}|\hspace{0.2cm}aC\)
- \(Y\rightarrow BYd \hspace{0.2cm}| \hspace{0.2cm} Bd\)
- \(CB\rightarrow BC\)
- \(aB\rightarrow ab\)
- \(bB\rightarrow bb\)
- \(Cd\rightarrow cd\)
- \(Cc\rightarrow cc\)
Homework: #
- \(L_3 = \{ a^n b^{2n}c^{3n} \hspace{0.2cm}| \hspace{0.2cm} n ≥ 1\} \)