costas busch - rpi1 context-free languages. costas busch - rpi2 regular languages

Download Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages

Post on 21-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Costas Busch - RPI1 Context-Free Languages
  • Slide 2
  • Costas Busch - RPI2 Regular Languages
  • Slide 3
  • Costas Busch - RPI3 Regular Languages Context-Free Languages
  • Slide 4
  • Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
  • Slide 5
  • Costas Busch - RPI5 Context-Free Grammars
  • Slide 6
  • Costas Busch - RPI6 Example A context-free grammar : A derivation:
  • Slide 7
  • Costas Busch - RPI7 A context-free grammar : Another derivation:
  • Slide 8
  • Costas Busch - RPI8 (((( )))) Describes parentheses:
  • Slide 9
  • Costas Busch - RPI9 A context-free grammar : A derivation: Example
  • Slide 10
  • Costas Busch - RPI10 A context-free grammar : Another derivation:
  • Slide 11
  • Costas Busch - RPI11
  • Slide 12
  • Costas Busch - RPI12 A context-free grammar : A derivation: Example
  • Slide 13
  • Costas Busch - RPI13 A context-free grammar : A derivation:
  • Slide 14
  • Costas Busch - RPI14 () ((( ))) (( )) Describes matched parentheses:
  • Slide 15
  • Costas Busch - RPI15 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
  • Slide 16
  • Costas Busch - RPI16
  • Slide 17
  • Costas Busch - RPI17 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
  • Slide 18
  • Costas Busch - RPI18 Derivation Order Leftmost derivation: Rightmost derivation:
  • Slide 19
  • Costas Busch - RPI19 Leftmost derivation: Rightmost derivation:
  • Slide 20
  • Costas Busch - RPI20 Derivation Trees
  • Slide 21
  • Costas Busch - RPI21
  • Slide 22
  • Costas Busch - RPI22
  • Slide 23
  • Costas Busch - RPI23
  • Slide 24
  • Costas Busch - RPI24
  • Slide 25
  • Costas Busch - RPI25 Derivation Tree
  • Slide 26
  • Costas Busch - RPI26 yield Derivation Tree
  • Slide 27
  • Costas Busch - RPI27 Partial Derivation Trees Partial derivation tree
  • Slide 28
  • Costas Busch - RPI28 Partial derivation tree
  • Slide 29
  • Costas Busch - RPI29 Partial derivation tree sentential form yield
  • Slide 30
  • Costas Busch - RPI30 Same derivation tree Sometimes, derivation order doesnt matter Leftmost: Rightmost:
  • Slide 31
  • Costas Busch - RPI31 Ambiguity
  • Slide 32
  • Costas Busch - RPI32 leftmost derivation
  • Slide 33
  • Costas Busch - RPI33 leftmost derivation
  • Slide 34
  • Costas Busch - RPI34 Two derivation trees
  • Slide 35
  • Costas Busch - RPI35 The grammar is ambiguous: stringhas two derivation trees
  • Slide 36
  • Costas Busch - RPI36 stringhas two leftmost derivations The grammar is ambiguous:
  • Slide 37
  • Costas Busch - RPI37 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees
  • Slide 38
  • Costas Busch - RPI38 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
  • Slide 39
  • Costas Busch - RPI39 Why do we care about ambiguity? take
  • Slide 40
  • Costas Busch - RPI40
  • Slide 41
  • Costas Busch - RPI41
  • Slide 42
  • Costas Busch - RPI42 Correct result:
  • Slide 43
  • Costas Busch - RPI43 We want to remove ambiguity Ambiguity is bad for programming languages
  • Slide 44
  • Costas Busch - RPI44 We fix the ambiguous grammar: New non-ambiguous grammar:
  • Slide 45
  • Costas Busch - RPI45
  • Slide 46
  • Costas Busch - RPI46 Unique derivation tree
  • Slide 47
  • Costas Busch - RPI47 The grammar : is non-ambiguous: Every string has a unique derivation tree
  • Slide 48
  • Costas Busch - RPI48 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT
  • Slide 49
  • Costas Busch - RPI49 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 50
  • Costas Busch - RPI50 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
  • Slide 51
  • Costas Busch - RPI51 The string has two derivation trees
  • Slide 52
  • Costas Busch - RPI52 Compilers
  • Slide 53
  • Costas Busch - RPI53 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code
  • Slide 54
  • Costas Busch - RPI54 Lexical analyzer parser Compiler program machine code input output
  • Slide 55
  • Costas Busch - RPI55 A parser knows the grammar of the programming language
  • Slide 56
  • Costas Busch - RPI56 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT
  • Slide 57
  • Costas Busch - RPI57 The parser finds the derivation of a particular input 10 + 2 * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 input derivation
  • Slide 58
  • Costas Busch - RPI58 10 E 25 E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 derivation derivation tree EE EE + *
  • Slide 59
  • Costas Busch - RPI59 10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code
  • Slide 60
  • Costas Busch - RPI60 Parsing
  • Slide 61
  • Costas Busch - RPI61 grammar Parser input string derivation
  • Slide 62
  • Costas Busch - RPI62 Example: Parser derivation input ?
  • Slide 63
  • Costas Busch - RPI63 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
  • Slide 64
  • Costas Busch - RPI64
  • Slide 65
  • Costas Busch - RPI65 Phase 2 Phase 1
  • Slide 66
  • Costas Busch - RPI66 Phase 2 Phase 3
  • Slide 67
  • Costas Busch - RPI67 Final result of exhaustive search Parser derivation input (top-down parsing)
  • Slide 68
  • Costas Busch - RPI68 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :
  • Slide 69
  • Costas Busch - RPI69 Time for phase 1: possible derivations For grammar with rules
  • Slide 70
  • Costas Busch - RPI70 Time for phase 2: possible derivations
  • Slide 71
  • Costas Busch - RPI71 Time for phase : possible derivations
  • Slide 72
  • Costas Busch - RPI72 Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|
  • Slide 73
  • Costas Busch - RPI73 There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair
  • Slide 74
  • Costas Busch - RPI74 S-grammar example: Each string has a unique derivation
  • Slide 75
  • Costas Busch - RPI75 In the exhaustive search parsing there is only one choice in each phase For S-grammars: Total time for parsing string : Time for a phase:
  • Slide 76
  • Costas Busch - RPI76 For general context-free grammars: There exists a parsing algorithm that parses a string in time (we will show it in the next class)

Recommended

View more >