Lab 11 - Grammars

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: VN,VT,P,S \langle V_N, V_T, P, S \rangle , where:

  • VN V_N is the non-terminal alphabet

  • VT V_T is the terminal alphabet;

  • P P is the terminal alphabet;

  • S S is the terminal alphabet;

  • V=VNVT V = V_N \cup V_T the alphabet;

  • P(VVNV)×V P \subseteq \left(V^* \cdot V_N \cdot V^*\right) \times V^* is the (finite) set of rewriting rules of production;

  • SVN S \in V_N is a particular element called axiom or initial symbol.

  • A grammar VN,VT,P,S \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=VN,VT,P,S G = \langle V_N, V_T, P, S\rangle be a grammar.

A production rule αβ \alpha \rightarrow \beta is an element of PP , where:

  • αVVNV\alpha \in V^* \cdot V_N \cdot V^* is a sequence of symbols including at least one non-terminal symbol.

  • βV\beta \in V^* is a (potentially empty) sequence of (terminal or non-terminal) symbols.


Chomsky Hierarchy #

Chomsky hierarchyGrammarsLanguagesMiniamal automaton
type 0UnrestrictedRecursively enumerableTuring machine
type 1Context-sensitiveContext-sensitiveLBA
type 2Context-freeContext-freeNDPDA
type 3RegularRegularFSA

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:

  • VN={S,A,B}; V_N = \{S, A, B\}; \hspace{0.2cm}
  • VT=Σ1={a,b} V_T = \Sigma_1 = \{a, b\}

Set of Production rules P: #

  • SA S \rightarrow A
  • SB S \rightarrow B
  • AaB A \rightarrow aB
  • Aϵ A \rightarrow \epsilon
  • BbA B \rightarrow bA
  • Bϵ 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:

  • Ab A \rightarrow b
where AVN A \in V_N and bVT b \in V_T
  • AbB A \rightarrow bB
where A,BVN A, B \in V_N and bVT b \in V_T
  • Aϵ A \rightarrow \epsilon
where A VN \in V_N and ϵ \epsilon denotes the empty string.

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:

  • Ab A \rightarrow b
where AVN A \in V_N and bVT b \in V_T
  • ABbA \rightarrow Bb
where A,BVNA, B \in V_N and bVTb \in V_T
  • AϵA \rightarrow \epsilon
where AVNA \in V_N and ϵ\epsilon denotes the empty string.

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:

  • AbA \rightarrow b
where AVNA \in V_N and bVTb \in V_T
  • AwBA \rightarrow wB
where A,BVNA, B \in V_N and wVTw \in V_T^*
  • AϵA \rightarrow \epsilon
where AVNA \in V_N and ϵ\epsilon denotes the empty string.

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:

  • AbA \rightarrow b
where AVNA \in V_N and bVTb \in V_T
  • ABwA \rightarrow Bw
where A,BVNA, B \in V_N and wVTw \in V_T^*
  • AϵA \rightarrow \epsilon
where AVNA \in V_N and ϵ\epsilon denotes the empty string.

Exercises #

Define Strictly Regular grammars that produce the following languages over the alphabet:

  • Σ1={a,b},Σ2={0,1}\Sigma_1 = \{a, b\} , \Sigma_2 = \{0, 1\}
  • L1={0,1}L_1 = \{0,1\}^*
  • L2={(aabbba)}L_2 = \{(aab \hspace{0.2cm} | \hspace{0.2cm} bba)^*\}

Solutions #

Solutions
L1={0,1} L_1 = \{0,1\}^* VN=SV_N = {S} \hspace{0.2cm} VT=Σ2={0,1} V_T = \Sigma_2 = \{0,1\}

Set of Production rules P:

  • SϵS \rightarrow \epsilon
  • S0SS \rightarrow 0S
  • S1SS \rightarrow 1S
L2={(aabbba)}L_2 = \{(aab \hspace{0.2cm} |\hspace{0.2cm} bba)^*\} VN={S,A,B,F,E}V_N = \{S, A, B, F, E\} \hspace{0.2cm} VT=Σ1={a,b}V_T = \Sigma_1 = \{a, b\}

Set of Production rules P:

  • Sϵ S \rightarrow \epsilon
  • SaAS \rightarrow aA
  • SbBS \rightarrow bB
  • AaFA \rightarrow aF
  • FbSF \rightarrow bS
  • BbEB \rightarrow bE
  • EaSE \rightarrow aS

Homework: #

  • L3={(aabb)aa}L_3 = \{(aa \hspace{0.2cm} | \hspace{0.2cm} bb)^*aa \}
  • L4={(0011)}L_4 = \{(00^* 11^*)\}

Context-Free grammars (type 2) #

Defined by rules of the form AγA \rightarrow \gamma where AA is a non-terminal and γ\gamma is a string of terminals and non-terminals.

Example #

Generate language:

  • anbn,a^nb^n,
where n>0 n>0
  • VN={S};V_N = \{S\}; \hspace{0.2cm}
  • VT=Σ1={a,b} V_T = \Sigma_1 = \{a, b\}

Set of Production rules P: #

  • {SaSbab}\{ S \rightarrow aSb | ab\}

Exercises #

Define context-free grammars that produce the following languages over the alphabet:

  • Σ={a,b}\Sigma = \{a, b\}
  • L1={w{a,b}w=wR}L_1 = \{w ∈ \{a, b\}^∗| w = w^R\}
  • L2={aibjcki,j,k0andi=jori=k}L_2= \{a^ib^jc^k | i, j, k \geq 0 and i=j or i=k \}

Homework:

  • L3=Generatelanguagewithalternatingasandbs L_3 = Generate language with alternating a's and b's
  • L4={anbncmn,m>0}{anbmcmn,m>0}L_4 = \{a^nb^nc^m \mid n,m>0\} \cup \{a^nb^mc^m \mid n,m>0\}

Solutions #

Solutions
L1={w{a,b}w=wR}L_1 = \{w ∈ \{a, b\}^∗ | w = w^R\} VN={S,O,E}V_N = \{S, O, E\} \hspace{0.2cm} VT=Σ={a,b}V_T = \Sigma = \{a, b\}

Set of Production rules P:

  • SOES \rightarrow O \hspace{0.2cm} | \hspace{0.2cm} E
  • EϵaEabEbE \rightarrow \epsilon \hspace{0.2cm} |\hspace{0.2cm} aEa \hspace{0.2cm} \hspace{0.2cm} | \hspace{0.2cm} bEb
  • OabaOabObO \rightarrow a \hspace{0.2cm}|\hspace{0.2cm} b \hspace{0.2cm} | \hspace{0.2cm} aOa\hspace{0.2cm} |\hspace{0.2cm} bOb

or:

  • SaSabSbabeS \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
L2={aibjcki,j,k0andi=jori=k}L_2= \{a^ib^jc^k | i, j, k \geq 0 and i=j or i=k\} VN={S,X,Y,W,Z}V_N = \{S, X, Y, W, Z \} \hspace{0.2cm} VT=Σ={a,b}V_T = \Sigma = \{a, b\}

Set of Production rules P:

  • SXYWS \rightarrow XY \hspace{0.2cm} | \hspace{0.2cm} W
  • XaXbϵX \rightarrow aXb \hspace{0.2cm} | \hspace{0.2cm} \epsilon
  • YcYϵY \rightarrow cY \hspace{0.2cm} | \hspace{0.2cm} \epsilon
  • WaWcZW \rightarrow aWc \hspace{0.2cm} | \hspace{0.2cm} Z
  • ZbZϵZ \rightarrow bZ \hspace{0.2cm} | \hspace{0.2cm} \epsilon
L3,GeneratelanguagewithalternatingasandbsL_3, Generate language with alternating a's and b's VN={S,A,B}V_N = \{ S,A,B\} \hspace{0.2cm} VT=Σ1={a,b}V_T = \Sigma_1 = \{a, b\}
  • SbBaAϵS \rightarrow bB\hspace{0.2cm}|\hspace{0.2cm}aA\hspace{0.2cm}|\hspace{0.2cm}\epsilon
  • AbBϵA \rightarrow bB\hspace{0.2cm}|\hspace{0.2cm} \epsilon
  • BaAϵB \rightarrow aA\hspace{0.2cm}|\hspace{0.2cm} \epsilon
L4={anbncmn,m>0}{anbmcmn,m>0} L_4 = \{a^nb^nc^m \mid n,m>0\} \cup \{a^nb^mc^m \mid n,m>0\} VN={S,M,N,A,C}V_N = \{S,M,N,A,C\} VT=Σ={a,b,c}V_T = \Sigma = \{a,b,c\}
  • SaNbCAbMcS \rightarrow aNbC\hspace{0.2cm}|AbMc
  • NaNbϵN \rightarrow aNb\hspace{0.2cm}| \hspace{0.2cm}\epsilon
  • MbMcϵM \rightarrow bMc\hspace{0.2cm}| \hspace{0.2cm}\epsilon
  • CcCϵC \rightarrow cC\hspace{0.2cm}|\hspace{0.2cm}\epsilon
  • AaAϵA \rightarrow aA\hspace{0.2cm}|\hspace{0.2cm} \epsilon

Context-Sensitive grammars (type 1) #

The rules of the form αAβαγβ\alpha A \beta \rightarrow \alpha \gamma \beta , where AA is a non-terminal and α\alpha , β\beta and γ\gamma are strings of terminals and non-terminals.

  • γ\gamma must be non-empty
  • The rule Sϵ S\rightarrow\epsilon is allowed if S does not appear on the right side of any rule

Example #

Generate language:

  • {AnBnCnn>0}\{A^nB^nC^n | n > 0\}

Set of production rules: #

  • SaBCS \rightarrow aBC
  • SaSBCS \rightarrow aSBC
  • CBCZCB \rightarrow CZ
  • CZBZCZ \rightarrow BZ
  • BZBCBZ \rightarrow BC
  • aBabaB \rightarrow ab
  • bBbbbB \rightarrow bb
  • bCbcbC \rightarrow bc
  • cCcccC \rightarrow cc

Exercises #

Define context-sensitive grammars that produce the following languages:

  • L1={aibjcidji,j1}L_1 = \{a^ib^jc^id^j \hspace{0.2cm}|\hspace{0.2cm} i,j \geq 1\}
  • L2={WWW{a,b}}L_2 = \{WW \hspace{0.2cm}|\hspace{0.2cm} W ∈ \{a, b\}^∗\}

Homework:

  • L3={W{a,b,c}#(a)=#(b)=#(c)and#(a)1}L_3 = \{W ∈ \{a, b, c\}^∗| \#(a) = \#(b) = \#(c) and \#(a) ≥1 \}

Solutions #

Solutions
L1={aibjcidji,j1}L_1 = \{a^ib^jc^id^j \hspace{0.2cm}|\hspace{0.2cm} i,j \geq 1\} VN={S,X,B,C,Y,Z};V_N = \{S,X,B,C, Y,Z\}; \hspace{0.2cm} VT=Σ={a,b}V_T = \Sigma = \{a,b\}

Set of Production rules P:

  • SXYS \rightarrow XY
  • XaXCaCX \rightarrow aXC\hspace{0.2cm} |\hspace{0.2cm} aC
  • YBYdBdY \rightarrow BYd\hspace{0.2cm} | \hspace{0.2cm}Bd
  • CBCZCB \rightarrow CZ
  • CZBZCZ \rightarrow BZ
  • BZBCBZ \rightarrow BC
  • aBabaB \rightarrow ab
  • CdcdCd\rightarrow cd
L2={WWW{a,b}}L_2= \{WW \hspace{0.2cm}|\hspace{0.2cm} W ∈ \{a, b\}^∗\} VN={S,A,B,C,D,E}V_N = \{S, A, B, C, D, E\} \hspace{0.2cm} VT=Σ={a,b}V_T = \Sigma = \{a, b\}

Set of Production rules P:

  • SϵCDEFS \rightarrow \epsilon \hspace{0.2cm}| \hspace{0.2cm}CD\hspace{0.2cm}|\hspace{0.2cm} EF
  • CaCAbCBaC \rightarrow aCA \hspace{0.2cm}|\hspace{0.2cm} bCB \hspace{0.2cm}|\hspace{0.2cm} a
  • DaEAbEBbD \rightarrow aEA \hspace{0.2cm}|\hspace{0.2cm} bEB \hspace{0.2cm}| \hspace{0.2cm}b
  • ADXDAD \rightarrow XD
  • BDYDBD \rightarrow YD
  • AFXFAF \rightarrow XF
  • BFYFBF \rightarrow YF
  • AXVXAX \rightarrow VX
  • VXVWVX \rightarrow VW
  • VWVAVW \rightarrow VA
  • VAXAVA \rightarrow XA
  • BXWXBX \rightarrow WX
  • WXWVWX \rightarrow WV

Continue..on next slide

L1={WWW{a,b}} L_1 = \{WW \hspace{0.2cm}|\hspace{0.2cm} W ∈ \{a, b\}^∗\} VN={S,A,B,C,D,E} V_N = \{S, A, B, C, D, E\} \hspace{0.2cm} VT=Σ={a,b} V_T = \Sigma = \{a, b\}

Set of Production rules P:

  • WVWBWV \rightarrow WB
  • WBXBWB \rightarrow XB
  • AYUYAY \rightarrow UY
  • UYUZUY \rightarrow UZ
  • UZUAUZ \rightarrow UA
  • UAYAUA \rightarrow YA
  • BYZYBY \rightarrow ZY
  • ZYZUZY \rightarrow ZU
  • ZUZBZU \rightarrow ZB
  • ZBYBZB \rightarrow YB
  • aDaaaD \rightarrow aa
  • bDbabD \rightarrow ba
  • aFabaF \rightarrow ab
  • bFbbbF \rightarrow bb
  • aXaaaX \rightarrow aa
  • bXbabX \rightarrow ba
  • aYabaY \rightarrow ab
  • bYbbbY \rightarrow bb
L3={ww{a,b,c}#(a)=#(b)=#(c)1} L_{3}=\left\{w \mid w \in\{a, b, c\}^{*} \wedge \#(a)=\#(b)=\#(c) \geq 1\right\} VN={S,A,B,C,P,Q,R,X,Y,Z} V_N = \{S,A,B,C,P,Q,R,X,Y,Z\} VT=Σ={a,b,c} V_T = \Sigma = \{a, b, c\}

Set of Production rules P:

  • SSABCABCS \rightarrow SABC\hspace{0.2cm}|\hspace{0.2cm}ABC
  • AaA\rightarrow a
  • ABAQAB\rightarrow AQ
  • AQBQAQ \rightarrow BQ
  • BQBABQ \rightarrow BA
  • ACARAC \rightarrow AR
  • ARCRAR \rightarrow CR
  • CRCACR \rightarrow CA
  • BbB \rightarrow b
  • BABPBA \rightarrow BP
  • BPAPBP \rightarrow AP
  • APABAP \rightarrow AB
  • BCCZBC \rightarrow CZ
  • BZCZBZ \rightarrow CZ
  • CZCBCZ \rightarrow CB
  • CcC \rightarrow c
  • CACXCA \rightarrow CX
  • CXAXCX \rightarrow AX
  • AXACAX \rightarrow AC
  • CBCYCB \rightarrow CY
  • CYBYCY \rightarrow BY
  • BYBCBY \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

Example: #

Generate language:

  • {AnBnCnn>0}\{A^nB^nC^n | n > 0\}

Set of productions rules: #

  • SaBCS \rightarrow aBC
  • SaSBCS \rightarrow aSBC
  • CBBCCB \rightarrow BC
  • aBabaB \rightarrow ab
  • bBbbbB \rightarrow bb
  • bCbcbC \rightarrow bc
  • cCcccC \rightarrow cc

Exercises #

Generate Unrestricted grammars for below languages:

  • L1={WW=aiL_1 = \{W | W = a^i and i=2k i = 2^ k and k>0} k > 0\}
  • L2={anbmcndmn>0,m>0}L_2 = \{a^nb^mc^nd^m \hspace{0.2cm}|\hspace{0.2cm} n>0, m>0\}

Solutions #

Solutions
L1={W:W=aiandi=2kandk>0} L_1 = \{W : W = a^i and i = 2^ k and k > 0\} VN={S,L,X,Y};V_N = \{S, L, X, Y\}; VT=Σ={a,b,c}V_T = \Sigma = \{a, b, c\}

Set of Production rules P:

  • SLaaYRS \rightarrow LaaYR
  • aXXaaaX \rightarrow Xaa
  • LXLYϵLX \rightarrow LY \hspace{0.2cm}|\hspace{0.2cm} \epsilon
  • LϵL \rightarrow \epsilon
  • YaaaYYa \rightarrow aaY
  • YRXRϵYR \rightarrow XR \hspace{0.2cm}|\hspace{0.2cm} \epsilon
  • RϵR \rightarrow \epsilon
L2={anbmcndmn>0,m>0}L_2=\{a^nb^mc^nd^m \hspace{0.2cm}|\hspace{0.2cm} n>0, m>0\} VN={S,A,B,C,X,Y};V_N = \{S, A, B, C, X, Y\}; VT=Σ={a,b,c}V_T = \Sigma = \{a, b, c\}

Set of Production rules P:

  • SXYS\rightarrow XY
  • XaXCaCX\rightarrow aXC\hspace{0.2cm}|\hspace{0.2cm}aC
  • YBYdBdY\rightarrow BYd \hspace{0.2cm}| \hspace{0.2cm} Bd
  • CBBCCB\rightarrow BC
  • aBabaB\rightarrow ab
  • bBbbbB\rightarrow bb
  • CdcdCd\rightarrow cd
  • CcccCc\rightarrow cc

Homework: #

  • L3={anb2nc3nn1}L_3 = \{ a^n b^{2n}c^{3n} \hspace{0.2cm}| \hspace{0.2cm} n ≥ 1\}