Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2005Costas Busch - RPI1 Context-Free Languages.

Similar presentations


Presentation on theme: "Fall 2005Costas Busch - RPI1 Context-Free Languages."— Presentation transcript:

1 Fall 2005Costas Busch - RPI1 Context-Free Languages

2 Fall 2005Costas Busch - RPI2 Regular Languages

3 Fall 2005Costas Busch - RPI3 Regular Languages Context-Free Languages

4 Fall 2005Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

5 Fall 2005Costas Busch - RPI5 Context-Free Grammars

6 Fall 2005Costas Busch - RPI6 Grammars Grammars express languages Example: the English language

7 Fall 2005Costas Busch - RPI7

8 Fall 2005Costas Busch - RPI8 A derivation of “the dog walks”:

9 Fall 2005Costas Busch - RPI9 A derivation of “a cat runs”:

10 Fall 2005Costas Busch - RPI10 Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }

11 Fall 2005Costas Busch - RPI11 Notation VariableTerminal Production Rules

12 Fall 2005Costas Busch - RPI12 Another Example Grammar: Derivation of sentence :

13 Fall 2005Costas Busch - RPI13 Grammar: Derivation of sentence :

14 Fall 2005Costas Busch - RPI14 Other derivations: Grammar:

15 Fall 2005Costas Busch - RPI15 Language of the grammar

16 Fall 2005Costas Busch - RPI16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

17 Fall 2005Costas Busch - RPI17 Example Grammar :

18 Fall 2005Costas Busch - RPI18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence

19 Fall 2005Costas Busch - RPI19 We write: Instead of:

20 Fall 2005Costas Busch - RPI20 In zero or more steps In general we write: If: Trivially:

21 Fall 2005Costas Busch - RPI21 Example Grammar Possible Derivations

22 Fall 2005Costas Busch - RPI22 Another Grammar Example Grammar : Derivations:

23 Fall 2005Costas Busch - RPI23 More Derivations Grammar

24 Fall 2005Costas Busch - RPI24 Language of a Grammar For a grammar with start variable : String of terminals

25 Fall 2005Costas Busch - RPI25 Example For grammar : Since:

26 Fall 2005Costas Busch - RPI26 A Convenient Notation

27 Fall 2005Costas Busch - RPI27 Example Context-free grammar : Possible Derivations: (((( )))) Describes parentheses:

28 Fall 2005Costas Busch - RPI28 Context-free grammar : Derivations: Example Palindromes of even length

29 Fall 2005Costas Busch - RPI29 A context-free grammar : Derivations: Example () ((( ))) (( )) Describes matched parentheses:

30 Fall 2005Costas Busch - RPI30 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable

31 Fall 2005Costas Busch - RPI31 The language of the grammar is:

32 Fall 2005Costas Busch - RPI32 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with

33 Fall 2005Costas Busch - RPI33 Derivation Order and Derivation Trees

34 Fall 2005Costas Busch - RPI34 Derivation Order Leftmost derivation: Rightmost derivation:

35 Fall 2005Costas Busch - RPI35 Leftmost derivation: Rightmost derivation:

36 Fall 2005Costas Busch - RPI36 Derivation Trees

37 Fall 2005Costas Busch - RPI37

38 Fall 2005Costas Busch - RPI38

39 Fall 2005Costas Busch - RPI39

40 Fall 2005Costas Busch - RPI40 Derivation Tree

41 Fall 2005Costas Busch - RPI41 yield Derivation Tree

42 Fall 2005Costas Busch - RPI42 Partial Derivation Trees Partial derivation tree

43 Fall 2005Costas Busch - RPI43 Partial derivation tree

44 Fall 2005Costas Busch - RPI44 Partial derivation tree sentential form yield

45 Fall 2005Costas Busch - RPI45 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

46 Fall 2005Costas Busch - RPI46 Ambiguity

47 Fall 2005Costas Busch - RPI47 leftmost derivation

48 Fall 2005Costas Busch - RPI48 leftmost derivation

49 Fall 2005Costas Busch - RPI49 Two derivation trees

50 Fall 2005Costas Busch - RPI50 The grammar is ambiguous: stringhas two derivation trees

51 Fall 2005Costas Busch - RPI51 stringhas two leftmost derivations The grammar is ambiguous:

52 Fall 2005Costas Busch - RPI52 Definition: A context-free grammar is ambiguous if some string has: two different derivation trees OR two leftmost derivations

53 Fall 2005Costas Busch - RPI53 Why do we care about ambiguity? take

54 Fall 2005Costas Busch - RPI54

55 Fall 2005Costas Busch - RPI55 Good derivation Tree Bad derivation Tree

56 Fall 2005Costas Busch - RPI56 We want to remove ambiguity Ambiguity is bad for programming languages

57 Fall 2005Costas Busch - RPI57 We fix the ambiguous grammar: New unambiguous grammar:

58 Fall 2005Costas Busch - RPI58 Unique derivation tree

59 Fall 2005Costas Busch - RPI59 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

60 Fall 2005Costas Busch - RPI60 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

61 Fall 2005Costas Busch - RPI61 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:

62 Fall 2005Costas Busch - RPI62 The string has always two derivation trees (for any grammar) For example


Download ppt "Fall 2005Costas Busch - RPI1 Context-Free Languages."

Similar presentations


Ads by Google