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 Example A context-free grammar : A derivation:

7 7 A context-free grammar : Another derivation:

8 8 (((( ))))

9 9 A context-free grammar : A derivation: Example

10 10 A context-free grammar : Another derivation:

11 11

12 12 A context-free grammar : A derivation: Example

13 13 A context-free grammar : A derivation:

14 14 () ((( ))) (( ))

15 15 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables

16 16 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with

17 17 Derivation Order Leftmost derivation: Rightmost derivation:

18 18 Leftmost derivation: Rightmost derivation:

19 19 Derivation Trees

20 20

21 21

22 22

23 23

24 24 Derivation Tree

25 25 yield Derivation Tree

26 26 Partial Derivation Trees Partial derivation tree

27 27 Partial derivation tree

28 28 Partial derivation tree sentential form yield

29 29 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

30 30 Ambiguity

31 31 leftmost derivation

32 32 leftmost derivation

33 33 Two derivation trees

34 34 The grammar is ambiguous: stringhas two derivation trees

35 35 stringhas two leftmost derivations The grammar is ambiguous:

36 36 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees

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

38 38 Why do we care about ambiguity? take

39 39

40 40

41 41 Correct result:

42 42 We want to remove ambiguity Ambiguity is bad for programming languages

43 43 We fix the ambiguous grammar: New non-ambiguous grammar:

44 44

45 45 Unique derivation tree

46 46 The grammar : is non-ambiguous: Every string has a unique derivation tree

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

48 48 The string has two derivation trees


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

Similar presentations


Ads by Google