Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars...
-
Upload
bertha-daniel -
Category
Documents
-
view
227 -
download
0
Transcript of Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars...
![Page 1: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/1.jpg)
AUTOMATATHEORY
V
![Page 2: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/2.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 2
Automata Theory V
Context-Free Grammarsand
Languages
![Page 3: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/3.jpg)
A context-free language (CFL) is a formal language that can be defined by a context-free grammar (CFG) . The set of all context-free languages is identical to the set of languages accepted by pushdown automata.
Dept. of Computer Science & IT, FUUAST Automata Theory 3
Automata Theory V
DEF.
The class of context-free languages generalizes the class of regular languages, i.e., every regular language is a context-free language.
The reverse of this is not true, i.e., every context-free language is not necessarily regular. For example, as we will see {0k1k | k>=0} is context-free but not regular.
![Page 4: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/4.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 4
Automata Theory V
Formally, a Context-Free Grammar (CFG) is a 4-tuple:
G = (V, T, P, S)
V - A finite set of variables or non-terminals
T - A finite set of terminals (V and T do not intersect)
P - A finite set of productions, each of the form A –> α, where A is in V and α is in (V U T)* , α may be ε
S - A starting non-terminal (S is in V)
![Page 5: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/5.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 5
Automata Theory V
Definition of Context-Free Grammars
![Page 6: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/6.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 6
Automata Theory V
Definition of Context-Free Grammars ….. Continued
Grammar G = (V, T, P, S)
![Page 7: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/7.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 7
Automata Theory V
Example:
Palindrome
The grammar Gpal for the palindrome is represented by Gpal = ( {P}, {0,1}, A, P ). A represents above productions
![Page 8: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/8.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 8
Automata Theory V
Right-Linear Grammar
Context-Free Grammar G = (V, T, P, S)
Productions forms:V T*V orV T*
Left-Linear Grammar
Productions forms:V VT* orV T*
![Page 9: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/9.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 9
Automata Theory V
Examples:
Grammar G = ({E,I}, T, P, E)
E = (a+b)(a+b+0+1)*T = {+, *, (,), a, b, 0, 1}
1. E I2. E E + E3. E E * E4. E (E)
5. I a6. I b7. I Ia8. I Ib9. I I010.I I1
Production Rules:
Derivations:
![Page 10: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/10.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 10
Automata Theory V
Grammar G = ( {S}, {a, b}, P, S)
Production Rules:S aSaS bSbS
Derivation:S aSa aaSaa aabSbaa aabbaa
![Page 11: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/11.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 11
Automata Theory V
Leftmost DerivationsAt each step of derivation we replace the leftmost variable by one of its production bodies
Grammar G = ( {S}, {a, b}, P, S)
Production Rules:1. S aSS2. S b
DerivationS aSS (Rule 1) aaSSS (Rule 1) aabSS (Rule 2) aabaSSS (Rule 1) aababSS (Rule 2) aababbS (Rule 2) aababbb (Rule 2)
![Page 12: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/12.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 12
Automata Theory V
Rightmost DerivationsAt each step of derivation we replace the rightmost variable by one of its production bodies
Grammar G = ( {S}, {a, b}, P, S)
Production Rules:1. S aSS2. S b
DerivationS aSS (Rule 1) aSb (Rule 2) aaSSb (Rule 1) aaSaSSb (Rule 1) aaSaSbb (Rule 2) aaSabbb (Rule 2) aababbb (Rule 2)
![Page 13: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/13.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 13
Automata Theory V
Sentential Forms Strings produced by derivation from the start symbol are termed as ‘Sentential Forms’. If the derivation is leftmost then the strings are left-sentential form.If the derivation is rightmost then the strings are right-sentential form. Derivation Trees or Parse Trees
![Page 14: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/14.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 14
Automata Theory V
Derivations:
![Page 15: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/15.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 15
Automata Theory V
Given a CFG with production rules as:S aAA aBB bBB a and productions as:S aA aaB aabB aaba
Draw parse tree
![Page 16: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/16.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 16
Automata Theory V
Given a CFG with production rules as:S aABA BbaB bBB c and productions as:S aAB a(Bba)B acbaB acba(bB) acbabc
Draw parse tree
![Page 17: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/17.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 17
Automata Theory V
Given a CFG with production rules as:S aSbS ab and productions as:S aSb aaSbb aaabbb
Draw parse tree
![Page 18: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/18.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 18
Automata Theory V
Ambiguity1. Ambiguity can be removed by introducing operator precedence2. Ambiguity can be reduced by allowing association or grouping
from left or right
![Page 19: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/19.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 19
Automata Theory V
Ambiguity
![Page 20: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/20.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 20
Automata Theory V
Application of Context-Free Grammars1) Compiler Parsers2) HTML3) XML
![Page 21: Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.](https://reader035.fdocuments.us/reader035/viewer/2022081511/56649ed45503460f94be4dcc/html5/thumbnails/21.jpg)
Dept. of Computer Science & IT, FUUAST Automata Theory 21
Automata Theory V
End of Chapter 5