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⟩
, where:
VN
is the non-terminal alphabet
VT
is the terminal alphabet;
P
is the terminal alphabet;
S
is the terminal alphabet;
V=VN∪VT
the alphabet;
P⊆(V∗⋅VN⋅V∗)×V∗
is the (finite) set of rewriting rules of production;
S∈VN
is a particular element called axiom or initial symbol.
A grammar ⟨VN,VT,P,S⟩
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⟩
be a grammar.
A production rule α→β
is an element of P
, where:
α∈V∗⋅VN⋅V∗
is a sequence of symbols including at least one non-terminal symbol.
β∈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:
- VN={S,A,B};
- VT=Σ1={a,b}
Set of Production rules P:
#
- A→aB
- A→ϵ
- B→bA
- B→ϵ
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:
where
A∈VN
and
b∈VT- A→bB
where
A,B∈VN
and
b∈VT- A→ϵ
where A
∈VN
and
ϵ
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:
where
A∈VN
and
b∈VTwhere
A,B∈VN
and
b∈VT- A→ϵ
where
A∈VN
and
ϵ
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:
where
A∈VN
and
b∈VTwhere
A,B∈VN
and
w∈VT∗- A→ϵ
where
A∈VN
and
ϵ
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:
where
A∈VN
and
b∈VTwhere
A,B∈VN
and
w∈VT∗- A→ϵ
where
A∈VN
and
ϵ
denotes the empty string.
Exercises
#
Define Strictly Regular grammars that produce the following languages over the alphabet:
- Σ1={a,b},Σ2={0,1}
- L1={0,1}∗
- L2={(aab∣bba)∗}
Solutions
#
Solutions
L1={0,1}∗
VN=S
VT=Σ2={0,1}Set of Production rules P:
- S→ϵ
L2={(aab∣bba)∗}
VN={S,A,B,F,E}
VT=Σ1={a,b}Set of Production rules P:
- S→ϵ
Homework:
#
- L3={(aa∣bb)∗aa}
- L4={(00∗11∗)}
Context-Free grammars (type 2)
#
Defined by rules of the form A→γ
where A
is a non-terminal and γ
is a string of terminals and non-terminals.
Example
#
Generate language:
where
n>0- VN={S};
- VT=Σ1={a,b}
Set of Production rules P:
#
- {S→aSb∣ab}
Exercises
#
Define context-free grammars that produce the following languages over the alphabet:
- Σ={a,b}
- L1={w∈{a,b}∗∣w=wR}
- L2={aibjck∣i,j,k≥0andi=jori=k}
Homework:
- L3=Generatelanguagewithalternatinga′sandb′s
- L4={anbncm∣n,m>0}∪{anbmcm∣n,m>0}
Solutions
#
Solutions
L1={w∈{a,b}∗∣w=wR}
VN={S,O,E}
VT=Σ={a,b}Set of Production rules P:
- S→O∣E
- E→ϵ∣aEa∣bEb
- O→a∣b∣aOa∣bOb
or:
- S→aSa∣bSb∣a∣b∣e
L2={aibjck∣i,j,k≥0andi=jori=k}
VN={S,X,Y,W,Z}
VT=Σ={a,b}Set of Production rules P:
- S→XY∣W
- X→aXb∣ϵ
- Y→cY∣ϵ
- W→aWc∣Z
- Z→bZ∣ϵ
L3,Generatelanguagewithalternatinga′sandb′s
VN={S,A,B}
VT=Σ1={a,b}- S→bB∣aA∣ϵ
- A→bB∣ϵ
- B→aA∣ϵ
L4={anbncm∣n,m>0}∪{anbmcm∣n,m>0}
VN={S,M,N,A,C}
VT=Σ={a,b,c}- S→aNbC∣AbMc
- N→aNb∣ϵ
- M→bMc∣ϵ
- C→cC∣ϵ
- A→aA∣ϵ
Context-Sensitive grammars (type 1)
#
The rules of the form αAβ→αγβ
, where A
is a non-terminal and α
, β
and γ
are strings of terminals and non-terminals.
- γ
must be non-empty
- The rule S→ϵ
is allowed if S does not appear on the right side of any rule
Example
#
Generate language:
- {AnBnCn∣n>0}
Set of production rules:
#
- S→aBC
- S→aSBC
- CB→CZ
- CZ→BZ
- BZ→BC
- aB→ab
- bB→bb
- bC→bc
- cC→cc
Exercises
#
Define context-sensitive grammars that produce the following languages:
- L1={aibjcidj∣i,j≥1}
- L2={WW∣W∈{a,b}∗}
Homework:
- L3={W∈{a,b,c}∗∣#(a)=#(b)=#(c)and#(a)≥1}
Solutions
#
Solutions
L1={aibjcidj∣i,j≥1}
VN={S,X,B,C,Y,Z};
VT=Σ={a,b}Set of Production rules P:
- X→aXC∣aC
- Y→BYd∣Bd
- CB→CZ
- CZ→BZ
- BZ→BC
- aB→ab
- Cd→cd
L2={WW∣W∈{a,b}∗}
VN={S,A,B,C,D,E}
VT=Σ={a,b}Set of Production rules P:
- S→ϵ∣CD∣EF
- C→aCA∣bCB∣a
- D→aEA∣bEB∣b
- AD→XD
- BD→YD
- AF→XF
- BF→YF
- AX→VX
- VX→VW
- VW→VA
- VA→XA
- BX→WX
- WX→WV
Continue..on next slide
L1={WW∣W∈{a,b}∗}
VN={S,A,B,C,D,E}
VT=Σ={a,b}Set of Production rules P:
- WV→WB
- WB→XB
- AY→UY
- UY→UZ
- UZ→UA
- UA→YA
- BY→ZY
- ZY→ZU
- ZU→ZB
- ZB→YB
- aD→aa
- bD→ba
- aF→ab
- bF→bb
- aX→aa
- bX→ba
- aY→ab
- bY→bb
L3={w∣w∈{a,b,c}∗∧#(a)=#(b)=#(c)≥1}
VN={S,A,B,C,P,Q,R,X,Y,Z}
VT=Σ={a,b,c}Set of Production rules P:
- S→SABC∣ABC
- AB→AQ
- AQ→BQ
- BQ→BA
- AC→AR
- AR→CR
- CR→CA
- BA→BP
- BP→AP
- AP→AB
- BC→CZ
- BZ→CZ
- CZ→CB
- CA→CX
- CX→AX
- AX→AC
- CB→CY
- CY→BY
- BY→BC
Unrestricted grammars (type 0)
#
The rules of the form α→β
, where α
and β
are strings of non-terminals and terminals.
The grammars without any limitation on production rules.
α
at least have one non-terminal
α
cannot be an empty string
Example:
#
Generate language:
- {AnBnCn∣n>0}
Set of productions rules:
#
- S→aBC
- S→aSBC
- CB→BC
- aB→ab
- bB→bb
- bC→bc
- cC→cc
Exercises
#
Generate Unrestricted grammars for below languages:
- L1={W∣W=ai
and i=2k
and k>0}
- L2={anbmcndm∣n>0,m>0}
Solutions
#
Solutions
L1={W:W=aiandi=2kandk>0}
VN={S,L,X,Y};
VT=Σ={a,b,c}Set of Production rules P:
- S→LaaYR
- aX→Xaa
- LX→LY∣ϵ
- L→ϵ
- Ya→aaY
- YR→XR∣ϵ
- R→ϵ
L2={anbmcndm∣n>0,m>0}
VN={S,A,B,C,X,Y};
VT=Σ={a,b,c}Set of Production rules P:
- X→aXC∣aC
- Y→BYd∣Bd
- CB→BC
- aB→ab
- bB→bb
- Cd→cd
- Cc→cc
Homework:
#
- L3={anb2nc3n∣n≥1}