Context-free Languages. Let's review some terminology.A derivation is a sequence of strings in (V ? T)* , starting with start symbol S and each string is derived from the previous string by applying a rule from P. S ? w1? w2 ? ... ? wn where if wn is aA? and there is some production A ? v, then
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
1. Context-free Languages A grammar G = (V, S, T, P) is said to be context-free if every production is of the form A ? x, where A ? V and x ? (V ? T)*.The following are acceptable productions:
S ? aSbSc
A ? BabC
A ? ?
I prefer the definition that only allows the ? production S ? ?
Clearly every regular grammar is context-free, since the rules are even more restrictive.
2. Context-free Languages Let's review some terminology.
A derivation is a sequence of strings in (V ? T)* , starting with start symbol S and each string is derived from the previous string by applying a rule from P.
S ? w1? w2 ? ... ? wn where if wn is aAß and there is some production A ? v, then wn would be avß.
If w can be derived from S, we write S w
3. Context-free Languages A derivation tree shows a derivation in the form of a tree.
A sentential form is any string of variables and symbols that can be derived from the start symbol: S w where w ? (V ? T)*.
A sentence is a sentential form that consists entirely of symbols. S w where w ? T*.
4. Context-free Languages The language generated by a grammar G, written symbolically as L(G) is the set of all possible sentences that can be generated by the grammar. L(G) = {w ? T* | S w}
Coming back to our example:
S ? aSb S ? ?
L(G) = {anbn | n = 0}!!!!!
5. Context-free Languages Consider the grammar: S ? abB A ? aaBb B ? bbAa A ? ?
L(G) = {ab(bbaa)nbba(ba)n : n = 0}
6. Context-free Languages Is the language L = {anbm | n ? m} context free?
Not so easy to see, but YES.
L = {anbm | n < m} ? {anbm : n > m}
Let S ? AS1 | S1B S1 ? aS1b | ? A ? aA | a B ? bB | b
7. Context-free Languages Consider the grammar S ? aSb | SS | ?
What language does this grammar generate?
8. Context-free Languages The problem with context-free grammars is that they are more complicated than regular grammars.
Therefore they are harder to analyze.
A regular grammar is quite simple. A right-linear grammar for example will always generate a given string one symbol at a time, going from left to right.
9. Context-free Languages But not so easy with context-free languages.
For example consider S ? aSb | SS | ?
How do you generate abab?
A derivation is a series of “sentential forms” (string of terminals and variables) starting with the string S and yielding a sentence (a sentential form containing only terminals)
10. Context-free Languages A derivation is a series of “sentential forms” (string of terminals and variables) starting with the string S and yielding a sentence (a sentential form containing only terminals)
S ? SS ? aSbS ? aSbaSb ? abaSb ? abab
S ? SS ? SaSb ? Sab ? aSbab ? abab
There are two ways to generate the same string!
11. Context-free Languages There are two ways to generate the same string!
But this example is silly. It’s just a matter of which variable we choose to substitute for and when.
A leftmost (rightmost) derivation is one in which we always substitute for the leftmost (rightmost) variable in each sentential form in the derivation.
12. Context-free Languages In our example, there is only one leftmost derivation.
Both of the derivations shown have the same derivation tree.
S ? SS ? aSbS ? aSbaSb S ? SS ? SaSb ? Sab
? abaSb ? abab ? aSbab ? abab
13. Context-free Languages While the example we just looked at was fairly simple to deal with, but consider the derivations for aabb:
S ? aSb ? aaSbb ? aabb
S ? SS ? S ? aSb ? aaSbb ? aabb
These are really two different leftmost derivations with different derivation trees.
We call such a grammar ambiguous
14. Context-free Languages Homework
5.1/ 1-5, 7a-e, 8a-d, 15, 19
5.2/ 6, 7, 8,11, 12
15. Simplification and Normal Forms We saw last week that context-free grammars (cfg’s) can be difficult to analyze.
To simplify the problem, we present two “normal forms” for cfg’s: Chomsky Normal Form (CNF) and Greibach Normal Form (GNF)
We will go through this material in a fairly general way.
16. Simplification and Normal Forms A normal form is a specific format for productions.
Because it’s specific, it’s easier to analyze
A normal form must allow all context-free languages to be generated by a grammar in that normal form.
17. Simplification and Normal Forms Before discussing the two normal forms, let’s talk a little about making minor simplifications.
There are three things that can make a grammar complicated, and they are not necessary:
useless productions: variables that can’t be generated and variables that can never lead to a sentence.
lambda rules; A ? ?
unit rules: A ? B
18. Simplification and Normal Forms useless productions:
Type 1: Variable X cannot be generated if we start with the start symbol.
Remove all productions that refer to variable X.
Suppose grammar G has n variables. Look at all derivations starting with the start symbol S of length up to n + 1. Any variable not mentioned in any of these derivations is useless.
19. Simplification and Normal Forms useless productions:
Type 2: Variable X does not lead to a sentence.
Remove all productions that refer to variable X.
Suppose grammar G has n variables. Look at all derivations starting with X of length up to n + 1. If no sentence is generated by any of these derivations, X is useless.
20. Simplification and Normal Forms useless productions:
Consider the grammar:
S ? abB A ? aaBb
B ? bbAa
A ? ?
21. Simplification and Normal Forms ?-rules: A ? ?
A can be erased. So whenever it appears on the right side of a production, replace with another production without the A.
22. Simplification and Normal Forms ?-rules: A ? ?
For example, consider the grammar
A ? ?
B ? aAbC
B ? bAbA
23. Simplification and Normal Forms unit rules: A ? B
whenever A ? B, replace the rule with rules A ? ß for each rule in which B ? ß
24. Simplification and Normal Forms unit rules: A ? B
Consider the grammar:
A ? aA |
B ?
25. Simplification and Normal Forms So before converting to one of the normal forms, eliminate these three kinds of rules.
26. Simplification and Normal Forms Chomsky Normal Form (CNF)
every rule is either A ? BC or A ? a
allow S ? ? if ? belongs to the language
27. Simplification and Normal Forms Chomsky Normal Form (CNF)
We can convert “nice” cfg’s to CNF by breaking down complex productions like A ? bbBCaC
into A ? XXBCYC X ? b Y ? a
and then A ? WDV X ? b Y ? a W ? XX D ? BC V ? YC,
and finally A ? RV R ? WD
W ? XX D ? BC V ? YC
28. Simplification and Normal Forms Greibach Normal Form (GNF)
every rule is A ? aBC...F (namely, the right side consists of a terminal followed by zero or more variables)
allow S ? ? if ? belongs to the language
29. Simplification and Normal Forms Greibach Normal Form (GNF)
We can convert “nice” cfg’s to GNF easily as long as no right-hand side begins with a variable.
(In other cases, it can be done, but I won’t force you.)
How? Just substitute a variable that only goes to that terminal for each terminal after the first one on a right-hand side.
A ? aBcbFaaGb
A ? aBXYFZZGY X ? c Y ? b Z ? a
30. Simplification and Normal Forms Since every cfg can be converted into either of the normal forms, if we want to show something regarding a context-free language we can say “Consider a Chomsky Normal Form (or Greibach Normal Form) grammar for this language.”
31. Pushdown Automata Just like regular languages have finite automata to recognize them, context-free languages also have a type of automaton to recognize them
This kind of automaton is called a Pushdown Automaton (pda)
We study the non-deterministic variety (npda) because the deterministic (dpda) variety is less powerful than the npda!!!!
32. Pushdown Automata An npda is simply an non-deterministic finite automaton with a stack added for additional storage.
33. Pushdown Automata A nondeterministic pushdown automaton (npda) is a septuple M = (Q, S, G , d, q0, z, F) , where
Q is a finite set of states,
S is a finite set of symbols (the alphabet),
q0 is one of the states in Q (called the initial state),
F is a subset of Q and represents the final, or accepting states of M.)
G is the set called the stack alphabet,
z is a stack symbol called the stack start symbol and is always pushed onto the stack before we start.
and d: Q ? (S ? {?}) ? G ? 2Q ? G*
that is, the transition function d maps to sets of ordered pairs (state, stack string)
34. Pushdown Automata A nondeterministic finite automaton (npda) is a septuple M = (Q, S, G , d, q0, z, F)
In other words, whenever the npda is in some state q, looking at input symbol a (or perhaps even ignoring the input symbol) and has some given stack symbol at the top of the stack, it can go non-deterministically to any of zero or more states and when it does, replace the top of the stack with zero or more new symbols.
Let's take a look at what this means:
35. Pushdown Automata A nondeterministic finite automaton (ndfa) is a septuple M = (Q, S, G , d, q0, z, F)
36. Pushdown Automata We use the stack to remember things. For example, pushing and later on popping can keep track of equalities of unbounded size.
We always start in the initial state with only z on the stack.
A string is accepted if and only if the npda can wind up in an accepting state after reading the entire string.
The contents of the stack are not relevant; the stack is only used to control which states we can go to.
37. Pushdown Automata How might we recognize anbn?
How might we recognize a marked palindrome?
How about an unmarked palindrome?
this example shows why we need non-determinism.
38. Pushdown Automata How might we recognize anbn?
39. Pushdown Automata How might we recognize a marked palindrome?
40. Pushdown Automata How about an unmarked palindrome?
this example shows why we need non-determinism.
41. Context-free Languages Homework
6.1/ 5, 6, 7, 8
6.2/2, 3, 10, 11
7.1/3a, 4c, 4h, 5