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 Grammars Grammars express languages Example: the English language

7 7

8 8 A derivation of “the dog walks”:

9 9 A derivation of “a cat runs”:

10 10 Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

11 11 Notation VariableTerminal Production Rules

12 12 Another Example Grammar: Derivation of sentence :

13 13 Grammar: Derivation of sentence :

14 14 Other derivations:

15 15 Language of the grammar

16 16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

17 17 Example Grammar :

18 18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence

19 19 We write: Instead of:

20 20 In general we write: If:

21 21 By default:

22 22 Example Grammar Derivations

23 23 Grammar Example Derivations

24 24 Another Grammar Example Grammar : Derivations:

25 25 More Derivations

26 26 Language of a Grammar For a grammar with start variable : String of terminals

27 27 Example For grammar : Since:

28 28 A Convenient Notation

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

30 30 A context-free grammar : Another derivation:

31 31 (((( )))) Describes parentheses:

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

33 33 A context-free grammar : Another derivation:

34 34

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

36 36 A context-free grammar : A derivation:

37 37 () ((( ))) (( )) Describes matched parentheses:

38 38 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable

39 39

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

41 41 Derivation Order Leftmost derivation: Rightmost derivation:

42 42 Leftmost derivation: Rightmost derivation:

43 43 Derivation Trees

44 44

45 45

46 46

47 47

48 48 Derivation Tree

49 49 yield Derivation Tree

50 50 Partial Derivation Trees Partial derivation tree

51 51 Partial derivation tree

52 52 Partial derivation tree sentential form yield

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

54 54 Ambiguity

55 55 leftmost derivation

56 56 leftmost derivation

57 57 Two derivation trees

58 58 The grammar is ambiguous: stringhas two derivation trees

59 59 stringhas two leftmost derivations The grammar is ambiguous:

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

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

62 62 Why do we care about ambiguity? take

63 63

64 64

65 65 Correct result:

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

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

68 68

69 69 Unique derivation tree

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

71 71 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

72 72 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

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

74 74 The string has two derivation trees


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

Similar presentations


Ads by Google