Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 of programming languages

2 2 Context-Free Grammars G = (V, T, S, P) Productions are of the form: A  x A  V and x  (V  T) *

3 3 Context-Free Grammars A regular language is also a context-free language.

4 4 Context-Free Grammars A regular language is also a context-free language. Why the name?

5 5 Example G = ({S}, {a, b}, S, P) P = { S  aSa S  bSb S  } S  aSa  aaSaa  aabSbaa  aabbaa

6 6 Example G = ({S}, {a, b}, S, P) P = { S  aSa S  bSb S  } S  aSa  aaSaa  aabSbaa  aabbaa L(G) = {ww R | w  {a, b}*}

7 7 Example G = ({S, A, B}, {a, b}, S, P) P = { S  abB A  aaBb B  bbAa A  } S  abB  abbbAa  abbbaaBba  abbbaabbAaba  abbbaabbaba

8 8 Example L = {a n b m | n  m} is context-free. G = (?, {a, b}, S, ?)

9 9 Example L = {a n b m | n  m} is context-free. G = (?, {a, b}, S, ?) P = { S  AS 1 | S 1 B S 1  aS 1 b | A  aA | a B  bB | b }

10 10 Example G = ({S}, {a, b}, S, P) P = { S  aSb | SS | } S  aSb  aaSbb  aaSSbb  aaabSbb  aaababbb

11 11 Example G = ({S}, {a, b}, S, P) P = { S  aSb | SS | } S  aSb  aaSbb  aaSSbb  aaabSbb  aaababbb L(G) = ?

12 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 12345 14253

13 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.

14 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 12345 14253

15 15 Derivation Trees A  abABc A bacBA ordered tree

16 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 A  V and its children are labeled a 1, a 2,..., a n iff P contains the production A  a 1 a 2... a n. 5.A leaf labeled has no siblings.

17 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 A  V and its children are labeled a 1, a 2,..., a n iff P contains the production A  a 1 a 2... a n. 5.A leaf labeled has no siblings.

18 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.

19 19 Derivation Trees S  aAB A  bBb B  A | yield: abbbb S b a B b A B bb A B

20 20 Derivation Trees S  aAB A  bBb B  A | S b a B b A B bb A B partial derivation tree

21 21 Sentential Forms & Derivation Trees Let G = (V, T, S, P) be a context-free grammar.  w  L(G) iff there exists a derivation tree of G whose yield is w.  If t G is a partial derivation tree of G whose root label is S, then the yield of t G is a sentential form of G.

22 22 Membership and Parsing Membership algorithm: tells if w  L(G). Parsing: finding a sequence of productions by which w  L(G) is derived.

23 23 Membership and Parsing S  SS | aSb | bSa | w = aabb

24 24 Exhaustive Search Parsing Top-down parsing. S  SS | aSb | bSa | w = aabb 1. S  SS S  SS  SSS S  aSb  aSSb 2. S  aSb S  SS  aSbS S  aSb  aaSbb 3. S  bSa S  SS  bSaS S  aSb  abSab 4. S  S  SS  S S  aSb  ab S  aSb  aaSbb  aabb

25 25 Exhaustive Search Parsing It is not efficient. If w  L(G) then it may never terminate, due to: A  B A 

26 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 w  L(G) or not.

27 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 w  L(G) or not. Proof: no more than |w| rounds are involved.

28 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 w  L(G) or not. Proof: no more than |w| rounds are involved. Complexity: |P| |w|.

29 29 Theorem 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)

30 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.

31 31 Simple Grammars (S-Grammars) Any w  L(G) can be parsed in at most |w| steps. w = a 1 a 2... a n S  a 1 A 1 A 2... A m  a 1 a 2 B 1 B 2... B k A 1 A 2... A m

32 32 Simple Grammars (S-Grammars) Many features of Pascal-like programming languages can be expressed with s-grammars.

33 33 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.

34 34 Ambiguity S  aSb | SS | w = aabb S a a b b S S S a a b b S S S S

35 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

36 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

37 37 Ambiguity If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous.

38 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.

39 39 Ambiguity L = {a n b n c m }  {a n b m c m } is inherently ambiguous. L = L 1  L 2 S  S 1 | S 2 S 1  S 1 c | A S 2  aS 2 | B A  aAb | B  bBc |

40 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.

41 41 CFGs and Programming Languages S-grammars: ::= if ::= then ::= else

42 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.


Download ppt "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."

Similar presentations


Ads by Google