![Page 1: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/1.jpg)
1
Context-Free Languages
• Not all languages are regular.
• L1 = {anbn | n 0} is not regular.
L2 = {(), (()), ((())), ...} is not regular.
some properties of programming languages
![Page 2: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/2.jpg)
2
Context-Free Grammars
G = (V, T, S, P)
Productions are of the form:
A x
A V and x (V T)*
![Page 3: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/3.jpg)
3
Context-Free Grammars
• A regular language is also a context-free language.
![Page 4: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/4.jpg)
4
Context-Free Grammars
• A regular language is also a context-free language.
• Why the name?
![Page 5: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/5.jpg)
5
Example
• G = ({S}, {a, b}, S, P)
P = { S aSa
S bSb
S }
S aSa aaSaa aabSbaa aabbaa
![Page 6: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/6.jpg)
6
Example
• G = ({S}, {a, b}, S, P)
P = { S aSa
S bSb
S }
S aSa aaSaa aabSbaa aabbaa
L(G) = {wwR | w {a, b}*}
![Page 7: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/7.jpg)
7
Example
• G = ({S, A, B}, {a, b}, S, P)
P = { S abB
A aaBb
B bbAa
A }
S abB abbbAa abbbaaBba abbbaabbAaba abbbaabbaba
![Page 8: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/8.jpg)
8
Example
• L = {anbm | n m} is context-free.
G = (?, {a, b}, S, ?)
![Page 9: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/9.jpg)
9
Example
• L = {anbm | n m} is context-free.
G = (?, {a, b}, S, ?)
P = { S AS1 | S1B
S1 aS1b |
A aA | a
B bB | b }
![Page 10: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/10.jpg)
10
Example
• G = ({S}, {a, b}, S, P)
P = { S aSb | SS | }
S aSb aaSbb aaSSbb aaabSbb aaababbb
![Page 11: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/11.jpg)
11
Example
• G = ({S}, {a, b}, S, P)
P = { S aSb | SS | }
S aSb aaSbb aaSSbb aaabSbb aaababbb
L(G) = ?
![Page 12: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/12.jpg)
12
Derivations
• G = ({S, A, B}, {a, b}, S, {S AB, A aaA, A , B Bb, B }) 1 2 3 4 5
S AB aaAB aaB aaBb aab
S AB ABb aaABb aaAb aab
1 2 3 4 5
1 4 2 5 3
![Page 13: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/13.jpg)
13
Derivations
• Leftmost: in each step the leftmost variable in the sentential form is replaced.
• Rightmost: in each step the rightmost variable in the sentential form is replaced.
![Page 14: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/14.jpg)
14
Example
• G = ({S, A, B}, {a, b}, S, {S AB, A aaA, A , B Bb, B }) 1 2 3 4 5
S AB aaAB aaB aaBb aab leftmost
S AB ABb aaABb aaAb aab
1 2 3 4 5
1 4 2 5 3
![Page 15: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/15.jpg)
15
Derivation Trees
A abABc
A
ba cBA
ordered tree
![Page 16: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/16.jpg)
16
Derivation Trees
• Let G = (V, T, S, P) be a context-free grammar.
An ordered tree is a derivation tree iff:
1. The root is labeled S.
2. Every leaf has a label from T {}.
3. Every interior vertex has a label from V.
4. A vertex has label AV and its children are labeled a1, a2, ..., an iff
P contains the production A a1 a2 ... an.
5. A leaf labeled has no siblings.
![Page 17: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/17.jpg)
17
Derivation Trees
• Let G = (V, T, S, P) be a context-free grammar.
An ordered tree is a partial derivation tree iff:
1. The root is labeled S.
2. Every leaf has a label from T {}.
Every leaf has a label from V T {}.
1. Every interior vertex has a label from V.
2. A vertex has label AV and its children are labeled a1, a2, ..., an iff
P contains the production A a1 a2 ... an.
5. A leaf labeled has no siblings.
![Page 18: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/18.jpg)
18
Derivation Trees
The string of symbols obtained by reading the leaves of a tree from left to
right (omitting any 's encountered) is called the yield of the tree.
![Page 19: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/19.jpg)
19
Derivation Trees
S aAB
A bBb
B A |
yield: abbbb
S
b
a
Bb
A
B
b b
A
B
![Page 20: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/20.jpg)
20
Derivation Trees
S aAB
A bBb
B A |
S
b
a
Bb
A
B
b b
A
B
partial derivation tree
![Page 21: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/21.jpg)
21
Sentential Forms & Derivation Trees
• Let G = (V, T, S, P) be a context-free grammar.
wL(G) iff there exists a derivation tree of G whose yield is w.
If tG is a partial derivation tree of G whose root label is S,
then the yield of tG is a sentential form of G.
![Page 22: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/22.jpg)
22
Membership and Parsing
• Membership algorithm: tells if wL(G).
• Parsing: finding a sequence of productions by which
wL(G) is derived.
![Page 23: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/23.jpg)
23
Membership and Parsing
S SS | aSb | bSa | w = aabb
![Page 24: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/24.jpg)
24
Exhaustive Search Parsing
• Top-down parsing.
• S SS | aSb | bSa | w = aabb
1. S SS S SS SSS S aSb aSSb2. S aSb S SS aSbS S aSb aaSbb3. S bSa S SS bSaS S aSb abSab4. S S SS S S aSb ab
S aSb aaSbb aabb
![Page 25: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/25.jpg)
25
Exhaustive Search Parsing
• It is not efficient.
• If w L(G) then it may never terminate, due to:
A B
A
![Page 26: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/26.jpg)
26
Exhaustive Search Parsing
Suppose G = (V, T, S, P) is a context-free grammar which
does not have any rule of the form A B or A .
Then the exhaustive search parsing method can decide if
wL(G) or not.
![Page 27: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/27.jpg)
27
Exhaustive Search Parsing
Suppose G = (V, T, S, P) is a context-free grammar which
does not have any rule of the form A B or A .
Then the exhaustive search parsing method can decide if
wL(G) or not.
Proof: no more than |w| rounds are involved.
![Page 28: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/28.jpg)
28
Exhaustive Search Parsing
Suppose G = (V, T, S, P) is a context-free grammar which
does not have any rule of the form A B or A .
Then the exhaustive search parsing method can decide if
wL(G) or not.
Proof: no more than |w| rounds are involved.
Complexity: |P||w|.
![Page 29: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/29.jpg)
29
Theorem
• For every context-free grammar G, there exists an algorithm that parses any wL(G) in a number of steps proportional to |w|3.
• Not satisfactory as linear time parsing algorithm (proportional to the length of a string)
![Page 30: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/30.jpg)
30
Simple Grammars (S-Grammars)
G = (V, T, S, P)
Productions are of the form:
A ax
A V, a T, and x V*, and any pair (A,a) can occur
in at most one rule.
![Page 31: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/31.jpg)
31
Simple Grammars (S-Grammars)
Any wL(G) can be parsed in at most |w| steps.
w = a1a2 ... an
S a1A1A2 ... Am a1a2B1B2 ... BkA1A2 ... Am
![Page 32: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/32.jpg)
32
Simple Grammars (S-Grammars)
Many features of Pascal-like programming languages can
be expressed with s-grammars.
![Page 33: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/33.jpg)
33
Ambiguity
A context-free grammar G is said to be ambiguous if
there exits some wL(G) that has at least two distinct
derivation trees.
![Page 34: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/34.jpg)
34
AmbiguityS aSb | SS |
w = aabb
S
a
a b
b
S
S
S
a
a b
b
S
S
S
S
![Page 35: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/35.jpg)
35
Ambiguity
• G = ({E, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c
E I
E E + E
E E * E
E (E)
I a | b | c
![Page 36: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/36.jpg)
36
Ambiguity
• G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c
E T | E + T
T F | T * F
E E * E
F I | (E)
I a | b | c
![Page 37: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/37.jpg)
37
Ambiguity
If L is a context-free language for which there exists an
unambiguous grammar, then L is said to be unambiguous.
![Page 38: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/38.jpg)
38
Ambiguity
If L is a context-free language for which there exists an
unambiguous grammar, then L is said to be unambiguous.
If every grammar that generates L is ambiguous, then L is
called inherently ambiguous.
![Page 39: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/39.jpg)
39
Ambiguity
• L = {anbncm} {anbmcm} is inherently ambiguous.
L = L1 L2
S S1 | S2
S1 S1c | A S2 aS2 | B
A aAb | B bBc |
![Page 40: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/40.jpg)
40
CFGs and Programming Languages
• Important uses of formal languages: to define precisely a programming language.
to construct an efficient translator for it.
• RLs are used for simple patterns, while CFLs for more complicated aspects.
![Page 41: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/41.jpg)
41
CFGs and Programming Languages
• S-grammars:
<if-statement> ::= if <expression> <then_clause> <else_clause>
<then_clause> ::= then <statement>
<else_clause> ::= else <statement>
![Page 42: 1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n 0} is not regular. L 2 = {(), (()), ((())),...} is not regular. some properties](https://reader036.vdocuments.us/reader036/viewer/2022062804/56649ed45503460f94be4d21/html5/thumbnails/42.jpg)
42
Homework
• Exercises: 2, 3, 4, 6, 7, 9, 15, 17, 22 of Section 5.1 - Linz’s book.
• Exercises: 1, 2, 5, 6, 8, 10, 11, 12, 13, 15 of Section 5.2 - Linz’s book.
• Exercises: 1, 2, 3 of Section 5.3 - Linz’s book.