Automata Lectuee5
Automata Lectuee5
FACULTYOFCOMPUTERSANDINFORMATION
جامعة المنوفية
Today Proverb
العلم نور
Knowledge is power
Context-Free Languages &
Grammars
(CFLs & CFGs)
2
Not all languages are regular
• Not all languages are regular
• Applications:
Context-
• Parse trees, compilers Regular
free
• XML (FA/RE)
(PDA/CFG)
Example
• A palindrome is a word that reads identical
from both ends
• E.g., madam, redivider, malayalam, 010010010
• Let L = { w | w is a binary palindrome}
• Is L regular?
• No.
• Proof:
• Let w=0N10N (assuming N to be the p/l constant)
2. A ==> 0 Terminal
3. A ==> 1
4. A ==> 0A0
5. A ==> 1A1 Variable or non-terminal
Same as:
A => 0A0 | 1A1 | 0 | 1 |
How does this grammar work?
Context-Free Grammar (CFG)
• How does the CFG for palindromes work?
• An input string belongs to the language (i.e.,
accepted) iff it can be generated by the CFG
Example: w=01110
G:
A => 0A0 | 1A1 | 0 | 1 |
• G can generate w as follows:
1. A => 0A0
Generating a string from a grammar:
2. => 01A10
1.Pick and choose a sequence of
3. => 01110 productions that would allow us to
generate the string.
2.At every step, substitute one
variable with one of its productions.
Context-Free Grammar (CFG)
Definition
• A context-free grammar G=(V,T,P,S), where:
• V: set of variables or non-terminals
• T: set of terminals (= alphabet U {})
• P: set of productions, each of which is of the form
V ==> 1 | 2 | …
• Where each i is an arbitrary string of variables and
terminals
• S ==> start variable
Example:
CFG for the language of binary palindromes:
• G=({A},{0,1},P,A)
• P: A ==> 0 A 0 | 1 A 1 | 0 | 1 |
Example
• CFG
G = ({A, B, C, S}, {a, b, c}, P, S)
P:
(1) S –> ABC
(2) A –> aA A –> aA | ε
(3) A –> ε
(4) B –> bB B –> bB | ε
(5) B –> ε
(6) C –> cC C –> cC | ε
(7) C –> ε
Is aabc G ?
Example
P: (1) S –> ABC
(2) A –> aA A –> aA | ε
(3) A –> ε
(4) B –> bB B –> bB | ε
(5) B –> ε
(6) C –> cC C –> cC | ε
• Example Derivations: (7) C –> ε
• CFG?
G:
S => 0S1 | A
A => 0A |
OR
CFG?
More examples
• L1 = {0n | n≥0 }
• L2 = {0n | n≥1 }
• L3={0i1j2k | i=j or j=k, where i,j,k≥0}
• L4={0i1j2k | i=j or i=k, where i,j,k≥1}
Applications of CFLs & CFGs
• Compilers use parsers for syntactic
checking
• Parsers can be expressed as CFGs
1. Balancing paranthesis:
• B ==> BB | (B) | Statement
• Statement ==> …
2. If-then-else:
• S ==> SS | if Condition then Statement else Statement | if
Condition then Statement | Statement
• Condition ==> …
• Statement ==> …
3. C paranthesis matching { … }
4. Pascal begin-end matching
5. YACC (Yet Another Compiler-Compiler)
More applications
• Markup languages
• Nested Tag Matching
• HTML
• <html> …<p> … <a href=…> … </a> </p> …
</html>
• XML
• <PC> … <MODEL> … </MODEL> .. <RAM>
… </RAM> … </PC>
Tag-Markup Languages
Roll ==> <ROLL> Class Students </ROLL>
Class ==> <CLASS> Text </CLASS>
Text ==> Char Text | Char
Char ==> a | b | … | z | A | B | .. | Z
Students ==> Student Students |
Student ==> <STUD> Text </STUD>
Here, the left hand side of each production denotes one non-
terminals (e.g., “Roll”, “Class”, etc.)
Those symbols on the right hand side for which no
productions (i.e., substitutions) are defined are terminals
(e.g., ‘a’, ‘b’, ‘|’, ‘<‘, ‘>’, “ROLL”, etc.)
Structure of a production
1. A ==> 1
2. A ==> 2
3. A ==> 3
…
K. A ==> k
CFG Conventions
• Terminal symbols <== a, b, c…
◼ Transitivity:
• L(G) = { w in T* | S ==>*G w }
Left-most & Right-most Derivation Styles
G:
E => E+E | E*E | (E) | F
F => aF | bF | 0F | 1F | E =*=>G a*(ab+10)
Derive the string a*(ab+10) from G:
◼E ◼E
◼==> E * E ◼==> E * E
Left-most
◼==> F * E ◼==> E * (E)
derivation: ◼==> aF * E ◼==> E * (E + E) Right-most
◼==> a * E ◼==> E * (E + F) derivation:
Always ◼==> a * (E) ◼==> E * (E + 1F)
Solution:
• The CFG can be given by,
• Production rule (P):
• S → 0S | 1S
• S→
• The rules are in the combination of 0's and
1's with the start symbol. Since (0+1)*
indicates {, 0, 1, 01, 10, 00, 11, …}.
Example
• Construct a CFG for a language L = {wcwR: w ∈ (a, b)*}.
• Solution:
• The string that can be generated for a given language is
{aacaa, bcb, abcba, bacab,abbcbba, ....}
• The grammar could be:
• S → aSa rule 1
• S → bSb rule 2
• S→c rule 3