fall 2005costas busch - rpi1 context-free languages

Download Fall 2005Costas Busch - RPI1 Context-Free Languages

Post on 22-Dec-2015

214 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Fall 2005Costas Busch - RPI1 Context-Free Languages
  • Slide 2
  • Fall 2005Costas Busch - RPI2 Regular Languages
  • Slide 3
  • Fall 2005Costas Busch - RPI3 Regular Languages Context-Free Languages
  • Slide 4
  • Fall 2005Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
  • Slide 5
  • Fall 2005Costas Busch - RPI5 Context-Free Grammars
  • Slide 6
  • Fall 2005Costas Busch - RPI6 Grammars Grammars express languages Example: the English language
  • Slide 7
  • Fall 2005Costas Busch - RPI7
  • Slide 8
  • Fall 2005Costas Busch - RPI8 A derivation of the dog walks:
  • Slide 9
  • Fall 2005Costas Busch - RPI9 A derivation of a cat runs:
  • Slide 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 }
  • Slide 11
  • Fall 2005Costas Busch - RPI11 Notation VariableTerminal Production Rules
  • Slide 12
  • Fall 2005Costas Busch - RPI12 Another Example Grammar: Derivation of sentence :
  • Slide 13
  • Fall 2005Costas Busch - RPI13 Grammar: Derivation of sentence :
  • Slide 14
  • Fall 2005Costas Busch - RPI14 Other derivations: Grammar:
  • Slide 15
  • Fall 2005Costas Busch - RPI15 Language of the grammar
  • Slide 16
  • Fall 2005Costas Busch - RPI16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
  • Slide 17
  • Fall 2005Costas Busch - RPI17 Example Grammar :
  • Slide 18
  • Fall 2005Costas Busch - RPI18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence
  • Slide 19
  • Fall 2005Costas Busch - RPI19 We write: Instead of:
  • Slide 20
  • Fall 2005Costas Busch - RPI20 In zero or more steps In general we write: If: Trivially:
  • Slide 21
  • Fall 2005Costas Busch - RPI21 Example Grammar Possible Derivations
  • Slide 22
  • Fall 2005Costas Busch - RPI22 Another Grammar Example Grammar : Derivations:
  • Slide 23
  • Fall 2005Costas Busch - RPI23 More Derivations Grammar
  • Slide 24
  • Fall 2005Costas Busch - RPI24 Language of a Grammar For a grammar with start variable : String of terminals
  • Slide 25
  • Fall 2005Costas Busch - RPI25 Example For grammar : Since:
  • Slide 26
  • Fall 2005Costas Busch - RPI26 A Convenient Notation
  • Slide 27
  • Fall 2005Costas Busch - RPI27 Example Context-free grammar : Possible Derivations: (((( )))) Describes parentheses:
  • Slide 28
  • Fall 2005Costas Busch - RPI28 Context-free grammar : Derivations: Example Palindromes of even length
  • Slide 29
  • Fall 2005Costas Busch - RPI29 A context-free grammar : Derivations: Example () ((( ))) (( )) Describes matched parentheses:
  • Slide 30
  • Fall 2005Costas Busch - RPI30 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
  • Slide 31
  • Fall 2005Costas Busch - RPI31 The language of the grammar is:
  • Slide 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
  • Slide 33
  • Fall 2005Costas Busch - RPI33 Derivation Order and Derivation Trees
  • Slide 34
  • Fall 2005Costas Busch - RPI34 Derivation Order Leftmost derivation: Rightmost derivation:
  • Slide 35
  • Fall 2005Costas Busch - RPI35 Leftmost derivation: Rightmost derivation:
  • Slide 36
  • Fall 2005Costas Busch - RPI36 Derivation Trees
  • Slide 37
  • Fall 2005Costas Busch - RPI37
  • Slide 38
  • Fall 2005Costas Busch - RPI38
  • Slide 39
  • Fall 2005Costas Busch - RPI39
  • Slide 40
  • Fall 2005Costas Busch - RPI40 Derivation Tree
  • Slide 41
  • Fall 2005Costas Busch - RPI41 yield Derivation Tree
  • Slide 42
  • Fall 2005Costas Busch - RPI42 Partial Derivation Trees Partial derivation tree
  • Slide 43
  • Fall 2005Costas Busch - RPI43 Partial derivation tree
  • Slide 44
  • Fall 2005Costas Busch - RPI44 Partial derivation tree sentential form yield
  • Slide 45
  • Fall 2005Costas Busch - RPI45 Same derivation tree Sometimes, derivation order doesnt matter Leftmost: Rightmost:
  • Slide 46
  • Fall 2005Costas Busch - RPI46 Ambiguity
  • Slide 47
  • Fall 2005Costas Busch - RPI47 leftmost derivation
  • Slide 48
  • Fall 2005Costas Busch - RPI48 leftmost derivation
  • Slide 49
  • Fall 2005Costas Busch - RPI49 Two derivation trees
  • Slide 50
  • Fall 2005Costas Busch - RPI50 The grammar is ambiguous: stringhas two derivation trees
  • Slide 51
  • Fall 2005Costas Busch - RPI51 stringhas two leftmost derivations The grammar is ambiguous:
  • Slide 52
  • Fall 2005Costas Busch - RPI52 Definition: A context-free grammar is ambiguous if some string has: two different derivation trees OR two leftmost derivations
  • Slide 53
  • Fall 2005Costas Busch - RPI53 Why do we care about ambiguity? take
  • Slide 54
  • Fall 2005Costas Busch - RPI54
  • Slide 55
  • Fall 2005Costas Busch - RPI55 Good derivation Tree Bad derivation Tree
  • Slide 56
  • Fall 2005Costas Busch - RPI56 We want to remove ambiguity Ambiguity is bad for programming languages
  • Slide 57
  • Fall 2005Costas Busch - RPI57 We fix the ambiguous grammar: New unambiguous grammar:
  • Slide 58
  • Fall 2005Costas Busch - RPI58 Unique derivation tree
  • Slide 59
  • Fall 2005Costas Busch - RPI59 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT
  • Slide 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
  • Slide 61
  • Fall 2005Costas Busch - RPI61 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
  • Slide 62
  • Fall 2005Costas Busch - RPI62 The string has always two derivation trees (for any grammar) For example

Recommended

View more >