ContextFree Languages Fall 2004 COMP 335 1 Regular

  • Slides: 76
Download presentation
Context-Free Languages Fall 2004 COMP 335 1

Context-Free Languages Fall 2004 COMP 335 1

Regular Languages Fall 2004 COMP 335 2

Regular Languages Fall 2004 COMP 335 2

Context-Free Languages Regular Languages Fall 2004 COMP 335 3

Context-Free Languages Regular Languages Fall 2004 COMP 335 3

Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Fall 2004 COMP 335 4

Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Fall 2004 COMP 335 4

Context-Free Grammars Fall 2004 COMP 335 5

Context-Free Grammars Fall 2004 COMP 335 5

Example A context-free grammar : A derivation: Fall 2004 COMP 335 6

Example A context-free grammar : A derivation: Fall 2004 COMP 335 6

A context-free grammar : Another derivation: Fall 2004 COMP 335 7

A context-free grammar : Another derivation: Fall 2004 COMP 335 7

Describes parentheses: Fall 2004 COMP 335 (((( )))) 8

Describes parentheses: Fall 2004 COMP 335 (((( )))) 8

Example A context-free grammar : A derivation: Fall 2004 COMP 335 9

Example A context-free grammar : A derivation: Fall 2004 COMP 335 9

A context-free grammar : Another derivation: Fall 2004 COMP 335 10

A context-free grammar : Another derivation: Fall 2004 COMP 335 10

Fall 2004 COMP 335 11

Fall 2004 COMP 335 11

Example A context-free grammar : A derivation: Fall 2004 COMP 335 12

Example A context-free grammar : A derivation: Fall 2004 COMP 335 12

A context-free grammar : A derivation: Fall 2004 COMP 335 13

A context-free grammar : A derivation: Fall 2004 COMP 335 13

Describes matched parentheses: Fall 2004 () ((( ))) (( )) COMP 335 14

Describes matched parentheses: Fall 2004 () ((( ))) (( )) COMP 335 14

Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable

Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable Fall 2004 String of variables and terminals COMP 335 15

Fall 2004 COMP 335 16

Fall 2004 COMP 335 16

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

Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with Fall 2004 COMP 335 17

Derivation Order Leftmost derivation: Rightmost derivation: Fall 2004 COMP 335 18

Derivation Order Leftmost derivation: Rightmost derivation: Fall 2004 COMP 335 18

Leftmost derivation: Rightmost derivation: Fall 2004 COMP 335 19

Leftmost derivation: Rightmost derivation: Fall 2004 COMP 335 19

Derivation Trees Fall 2004 COMP 335 20

Derivation Trees Fall 2004 COMP 335 20

Fall 2004 COMP 335 21

Fall 2004 COMP 335 21

Fall 2004 COMP 335 22

Fall 2004 COMP 335 22

Fall 2004 COMP 335 23

Fall 2004 COMP 335 23

Fall 2004 COMP 335 24

Fall 2004 COMP 335 24

Derivation Tree Fall 2004 COMP 335 25

Derivation Tree Fall 2004 COMP 335 25

Derivation Tree yield Fall 2004 COMP 335 26

Derivation Tree yield Fall 2004 COMP 335 26

Partial Derivation Trees Partial derivation tree Fall 2004 COMP 335 27

Partial Derivation Trees Partial derivation tree Fall 2004 COMP 335 27

Partial derivation tree Fall 2004 COMP 335 28

Partial derivation tree Fall 2004 COMP 335 28

sentential form Partial derivation tree yield Fall 2004 COMP 335 29

sentential form Partial derivation tree yield Fall 2004 COMP 335 29

Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree Fall 2004 COMP 335

Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree Fall 2004 COMP 335 30

So far, we concentrated on generative aspect of grammars. How about analytical aspect? Parsing….

So far, we concentrated on generative aspect of grammars. How about analytical aspect? Parsing…. . Fall 2004 COMP 335 31

input string Fall 2004 Parser grammar COMP 335 derivation 32

input string Fall 2004 Parser grammar COMP 335 derivation 32

Example: Parser derivation input ? Fall 2004 COMP 335 33

Example: Parser derivation input ? Fall 2004 COMP 335 33

Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1 Fall

Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1 Fall 2004 COMP 335 34

Fall 2004 COMP 335 35

Fall 2004 COMP 335 35

Phase 2 Phase 1 Fall 2004 COMP 335 36

Phase 2 Phase 1 Fall 2004 COMP 335 36

Phase 2 Phase 3 Fall 2004 COMP 335 37

Phase 2 Phase 3 Fall 2004 COMP 335 37

Final result of exhaustive search (top-down parsing) Parser input derivation Fall 2004 COMP 335

Final result of exhaustive search (top-down parsing) Parser input derivation Fall 2004 COMP 335 38

Time complexity of exhaustive search Suppose there are no productions of the form Number

Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string Why? Fall 2004 COMP 335 is 39

For grammar with rules Time for phase 1: possible derivations Fall 2004 COMP 335

For grammar with rules Time for phase 1: possible derivations Fall 2004 COMP 335 40

Time for phase 2: possible derivations Fall 2004 COMP 335 41

Time for phase 2: possible derivations Fall 2004 COMP 335 41

Time for phase : possible derivations: which is exponential in the length of w

Time for phase : possible derivations: which is exponential in the length of w Fall 2004 COMP 335 42

Total time needed for parsing phase 1 phase 2 : phase 2|w| Extremely bad!!!

Total time needed for parsing phase 1 phase 2 : phase 2|w| Extremely bad!!! Fall 2004 COMP 335 43

There exist faster algorithms for specialized grammars S-grammar: symbol appears once Pair Fall 2004

There exist faster algorithms for specialized grammars S-grammar: symbol appears once Pair Fall 2004 string of variables COMP 335 44

S-grammar example: Each string has a unique derivation Fall 2004 COMP 335 45

S-grammar example: Each string has a unique derivation Fall 2004 COMP 335 45

For S-grammars: In the exhaustive search parsing there is only one choice in each

For S-grammars: In the exhaustive search parsing there is only one choice in each phase Time for a phase: Total time for parsing string Fall 2004 COMP 335 : 46

For general context-free grammars: There exists a parsing algorithm that parses a string in

For general context-free grammars: There exists a parsing algorithm that parses a string in time (we will show it in the next class) Fall 2004 COMP 335 47

Ambiguity Fall 2004 COMP 335 48

Ambiguity Fall 2004 COMP 335 48

leftmost derivation Fall 2004 COMP 335 49

leftmost derivation Fall 2004 COMP 335 49

leftmost derivation Fall 2004 COMP 335 50

leftmost derivation Fall 2004 COMP 335 50

Two derivation trees Fall 2004 COMP 335 51

Two derivation trees Fall 2004 COMP 335 51

The grammar is ambiguous: since there is a string, namely , which has two

The grammar is ambiguous: since there is a string, namely , which has two derivation trees Fall 2004 COMP 335 52

The grammar is ambiguous: since string has two leftmost derivations Fall 2004 COMP 335

The grammar is ambiguous: since string has two leftmost derivations Fall 2004 COMP 335 53

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

Definition: A context-free grammar if some string is ambiguous has two or more distinct derivation trees Fall 2004 COMP 335 54

Alternatively we may say: Ambiguity of a grammar implies the existence of two or

Alternatively we may say: Ambiguity of a grammar implies the existence of two or more leftmost (or rightmost) derivations Fall 2004 COMP 335 55

Why do we care about ambiguity? take Fall 2004 COMP 335 56

Why do we care about ambiguity? take Fall 2004 COMP 335 56

Fall 2004 COMP 335 57

Fall 2004 COMP 335 57

Fall 2004 COMP 335 58

Fall 2004 COMP 335 58

Correct result: Fall 2004 COMP 335 59

Correct result: Fall 2004 COMP 335 59

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

• Ambiguity is bad for programming languages • We want to remove ambiguity Fall 2004 COMP 335 60

We may be able to fix the ambiguity: New non-ambiguous grammar: Fall 2004 COMP

We may be able to fix the ambiguity: New non-ambiguous grammar: Fall 2004 COMP 335 61

Fall 2004 COMP 335 62

Fall 2004 COMP 335 62

Unique derivation tree Fall 2004 COMP 335 63

Unique derivation tree Fall 2004 COMP 335 63

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

The grammar : is non-ambiguous: Every string has a unique derivation tree Fall 2004 COMP 335 64

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT Fall 2004 COMP 335

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT Fall 2004 COMP 335 65

if expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT

if expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT if expr 1 if then expr 2 STMT then stmt 1 else STMT else stmt 2 IF_STMT if expr 1 if Fall 2004 then expr 2 then stmt 1 COMP 335 66

Inherent Ambiguity Some context-free languages have only ambiguous grammars Example: Fall 2004 COMP 335

Inherent Ambiguity Some context-free languages have only ambiguous grammars Example: Fall 2004 COMP 335 67

The string Fall 2004 has two derivation trees COMP 335 68

The string Fall 2004 has two derivation trees COMP 335 68

Compilers Fall 2004 COMP 335 69

Compilers Fall 2004 COMP 335 69

Machine Code Program v = 5; if (v>5) x = 12 + v; while

Machine Code Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }. . . Fall 2004 Compiler COMP 335 Add v, v, 0 cmp v, 5 jmplt ELSE THEN: add x, 12, v ELSE: WHILE: cmp x, 3. . . 70

Compiler Lexical analyzer parser input output machine code program Fall 2004 COMP 335 71

Compiler Lexical analyzer parser input output machine code program Fall 2004 COMP 335 71

A parser knows the grammar of the programming language Fall 2004 COMP 335 72

A parser knows the grammar of the programming language Fall 2004 COMP 335 72

Parser PROGRAM STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT |

Parser PROGRAM STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT Fall 2004 COMP 335 73

The parser finds the derivation of a particular input derivation input 10 + 2

The parser finds the derivation of a particular input derivation input 10 + 2 * 5 Fall 2004 Parser E -> E + E |E*E | INT COMP 335 E => E + E * E => 10 + E*E => 10 + 2 * 5 74

derivation tree derivation E E => E + E * E => 10 +

derivation tree derivation E E => E + E * E => 10 + E*E => 10 + 2 * 5 Fall 2004 E 10 + E 2 COMP 335 E * E 5 75

derivation tree E E 10 machine code + E 2 Fall 2004 E *

derivation tree E E 10 machine code + E 2 Fall 2004 E * mult a, 2, 5 add b, 10, a E 5 COMP 335 76