Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 37 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
37
Dung lượng
92 KB
Nội dung
Chapter 5: Context-Free Grammar Quan Thanh Tho qttho@cse.hcmut.edu.vn Non-regular Languages • Not all languages are regular • L1 = {anbn | n ≥ 0} is not regular L2 = {(), (()), ((())), } is not regular ↓ some properties of programming languages Context-Free Grammars G = (V, T, S, P) Productions are of the form: A→x A ∈ V and x ∈ (V ∪ T)* CFG and Regular Language • A regular language is also a context-free language • Why the name? Example 5.1 • G = ({S}, {a, b}, S, P) P = { S → aSa S → bSb S→λ } S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa L(G) = {wwR | w ∈ {a, b}*} Example 5.2 • G = ({S, A, B}, {a, b}, S, P) P = { S → abB A → aaBb B → bbAa } A→λ S ⇒ abB ⇒ abbbAa ⇒ abbbaaBba ⇒ abbbaabbAaba ⇒ abbbaabbaba Example 5.3 • L = {anbm | n ≠ m} is context-free G = (?, {a, b}, S, ?) P = { S → AS1 | S1B S1 → aS1b | λ A → aA | a B → bB | b } Example 5.4 • G = ({S}, {a, b}, S, P) P = { S → aSb | SS | λ} S ⇒ aSb ⇒ aaSbb ⇒ aaSSbb ⇒ aaabSbb ⇒ aaababbb L(G) = ? Derivations • G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B → Bb, B → λ}) 1 4 S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab Leftmost and Rightmost 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 10 Restricted Grammar for 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 w∈L(G) or not 23 Theorem 5.1 • For every context-free grammar G, there exists an algorithm that parses any w∈L(G) in a number of steps proportional to |w|3 • Not satisfactory as linear time parsing algorithm (proportional to the length of a string) 24 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 25 Parsing in Simple Grammars (SGrammars) Any w∈L(G) can be parsed in at most |w| steps w = a1a2 an S ⇒ a1A1A2 Am ⇒ a1a2B1B2 BkA1A2 Am 26 S-Grammars and Contemporary Programming Languages Many features of Pascal-like programming languages can be expressed with s-grammars 27 Ambiguity A context-free grammar G is said to be ambiguous if there exits some w∈L(G) that has at least two distinct derivation trees 28 Example 5.9 S → aSb | SS | λ S w = aabb S a a S S λ S S b b λ a a S S λ 29 b b Example 5.10 • 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 30 Example 5.11 • G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) w=a+b*c E→T|E+T T→F|T*F F → I | (E) I →a|b|c 31 Unambiguous Grammar If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous 32 Inherently Ambiguous Grammar 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 33 Example 5.12 • L = {anbncm} ∪ {anbmcm} is inherently ambiguous L = L ∪ L2 S → S1 | S2 S1 → S1c | A S2 → aS2 | B A → aAb | λ B → bBc | λ 34 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 CFGs for more complicated aspects 35 Example 5.14 • S-grammars: ::= if ::= then ::= else 36 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, of Section 5.3 - Linz’s book 37 ... BkA1A2 Am 26 S-Grammars and Contemporary Programming Languages Many features of Pascal-like programming languages can be expressed with s-grammars 27 Ambiguity A context- free grammar G is said... Unambiguous Grammar If L is a context- free language for which there exists an unambiguous grammar, then L is said to be unambiguous 32 Inherently Ambiguous Grammar If L is a context- free language... 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 25 Parsing in Simple Grammars (SGrammars)