Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2006Costas Busch - RPI1 Context-Free Languages.

Similar presentations


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

1 Fall 2006Costas Busch - RPI1 Context-Free Languages

2 Fall 2006Costas Busch - RPI2 Regular Languages Context-Free Languages

3 Fall 2006Costas Busch - RPI3 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

4 Fall 2006Costas Busch - RPI4 Context-Free Grammars

5 Fall 2006Costas Busch - RPI5 Grammars Grammars express languages Example: the English language grammar

6 Fall 2006Costas Busch - RPI6

7 Fall 2006Costas Busch - RPI7 Derivation of string “the dog walks”:

8 Fall 2006Costas Busch - RPI8 Derivation of string “a cat runs”:

9 Fall 2006Costas Busch - RPI9 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” }

10 Fall 2006Costas Busch - RPI10 Variables Sequence of Terminals (symbols) Productions Sequence of Variables

11 Fall 2006Costas Busch - RPI11 Another Example Grammar: Variable Sequence of terminals and variables The right side may be

12 Fall 2006Costas Busch - RPI12 Grammar: Derivation of string :

13 Fall 2006Costas Busch - RPI13 Grammar: Derivation of string :

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

15 Fall 2006Costas Busch - RPI15 Grammar: Language of the grammar:

16 Fall 2006Costas Busch - RPI16 We write: Instead of: for zero or more derivation steps A Convenient Notation

17 Fall 2006Costas Busch - RPI17 in zero or more derivation steps In general we write: If: Trivially:

18 Fall 2006Costas Busch - RPI18 Example Grammar Possible Derivations

19 Fall 2006Costas Busch - RPI19 Another convenient notation:

20 Fall 2006Costas Busch - RPI20 Formal Definitions Set of variables Set of terminal symbols Start variable Set of productions Grammar:

21 Fall 2006Costas Busch - RPI21 Context-Free Grammar: All productions in are of the form String of variables and terminals Variable

22 Fall 2006Costas Busch - RPI22 variables terminals productions start variable Example of Context-Free Grammar

23 Fall 2006Costas Busch - RPI23 For a grammar with start variable String of terminals or Language of a Grammar:

24 Fall 2006Costas Busch - RPI24 context-free grammar : Example: Since, there is derivation for any

25 Fall 2006Costas Busch - RPI25 A language is context-free if there is a context-free grammar with Context-Free Language:

26 Fall 2006Costas Busch - RPI26 since context-free grammar : Example: is a context-free language generates

27 Fall 2006Costas Busch - RPI27 Context-free grammar : Example derivations: Palindromes of even length Another Example

28 Fall 2006Costas Busch - RPI28 Context-free grammar : Example derivations: () ((( ))) (( )) Describes matched parentheses: Another Example

29 Fall 2006Costas Busch - RPI29 Derivation Order and Derivation Trees

30 Fall 2006Costas Busch - RPI30 Derivation Order Consider the following example grammar with 5 productions:

31 Fall 2006Costas Busch - RPI31 Leftmost derivation order of string : At each step, we substitute the leftmost variable

32 Fall 2006Costas Busch - RPI32 Rightmost derivation order of string : At each step, we substitute the rightmost variable

33 Fall 2006Costas Busch - RPI33 Rightmost derivation of : Leftmost derivation of :

34 Fall 2006Costas Busch - RPI34 Derivation Trees Consider the same example grammar: And a derivation of :

35 Fall 2006Costas Busch - RPI35 yield

36 Fall 2006Costas Busch - RPI36 yield

37 Fall 2006Costas Busch - RPI37 yield

38 Fall 2006Costas Busch - RPI38 yield

39 Fall 2006Costas Busch - RPI39 yield Derivation Tree (parse tree)

40 Fall 2006Costas Busch - RPI40 Give same derivation tree Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation:

41 Fall 2006Costas Busch - RPI41 Ambiguity

42 Fall 2006Costas Busch - RPI42 Grammar for mathematical expressions Example strings: Denotes any number

43 Fall 2006Costas Busch - RPI43 A leftmost derivation for

44 Fall 2006Costas Busch - RPI44 Another leftmost derivation for

45 Fall 2006Costas Busch - RPI45 Two derivation trees for

46 Fall 2006Costas Busch - RPI46 take

47 Fall 2006Costas Busch - RPI47 Good TreeBad Tree Compute expression result using the tree

48 Fall 2006Costas Busch - RPI48 Two different derivation trees may cause problems in applications which use the derivation trees: Evaluating expressions In general, in compilers for programming languages

49 Fall 2006Costas Busch - RPI49 Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa)

50 Fall 2006Costas Busch - RPI50 stringhas two derivation trees this grammar is ambiguous since Example:

51 Fall 2006Costas Busch - RPI51 stringhas two leftmost derivations this grammar is ambiguous also because

52 Fall 2006Costas Busch - RPI52 IF_STMTif EXPR then STMT if EXPR then STMT else STMT Another ambiguous grammar: VariablesTerminals Very common piece of grammar in programming languages

53 Fall 2006Costas Busch - RPI53 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2 Two derivation trees

54 Fall 2006Costas Busch - RPI54 In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general we cannot do so

55 Fall 2006Costas Busch - RPI55 Ambiguous Grammar Non-Ambiguous Grammar Equivalent generates the same language A successful example:

56 Fall 2006Costas Busch - RPI56 Unique derivation tree for

57 Fall 2006Costas Busch - RPI57 An un-successful example: every grammar that generates this language is ambiguous is inherently ambiguous:

58 Fall 2006Costas Busch - RPI58 Example (ambiguous) grammar for :

59 Fall 2006Costas Busch - RPI59 The string has always two different derivation trees (for any grammar) For example


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

Similar presentations


Ads by Google