Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.

Similar presentations


Presentation on theme: "1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages."— Presentation transcript:

1 1 Context-Free Languages

2 2 Regular Languages

3 3 Context-Free Languages

4 4 Pushdown Automata Context-Free Grammars stack automaton

5 5 Context-Free Grammars

6 6 A context-free grammar G:

7 7 (((( ))))

8 8 A context-free grammar G:

9 9

10 10 A context-free grammar G:

11 11 () ((( ))) (( ))

12 12 Definition of Context-Free Grammars Any grammar G=(V,T,S,P) P: production rules of the form V: variables T: terminal symbols S: start variable : string of variables and terminals

13 13 Definition of Context-Free Languages A language L is context-free if and only if there is a grammar G with L = L(G)

14 14 Derivation Order Leftmost derivation: Rightmost derivation:

15 15 Derivation Trees

16 16

17 17

18 18

19 19

20 20 Derivation Tree

21 21 yield sentence Derivation Tree

22 22 Partial Derivation Trees Partial derivation tree

23 23 Partial derivation tree sentential form yield

24 24 Same Derivation Tree Often, derivation order doesn’t matter

25 25 Ambiguity

26 26

27 27

28 28

29 29 The grammar is ambiguous: string has two derivation trees

30 30 The grammar is ambiguous: stringhas two leftmost derivations

31 31 In general: A context-free grammar is ambiguous if some string has: two or more derivation trees

32 32 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations rightmost

33 33 Why do we care about ambiguity? take

34 34

35 35

36 36 The right solution:

37 37 We want to remove the ambiguity Ambiguity is bad for programming languages Therefore:

38 38 We fix the ambiguous grammar: New grammar:

39 39

40 40 Unique derivation tree

41 41 The grammar Is not ambiguous

42 42 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:

43 43 The string has two derivation trees


Download ppt "1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages."

Similar presentations


Ads by Google