ContextFree Languages Fall 2004 COMP 335 1 Regular
- Slides: 76
Context-Free Languages Fall 2004 COMP 335 1
Regular Languages Fall 2004 COMP 335 2
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 Grammars Fall 2004 COMP 335 5
Example A context-free grammar : A derivation: Fall 2004 COMP 335 6
A context-free grammar : Another derivation: Fall 2004 COMP 335 7
Describes parentheses: Fall 2004 COMP 335 (((( )))) 8
Example A context-free grammar : A derivation: Fall 2004 COMP 335 9
A context-free grammar : Another derivation: Fall 2004 COMP 335 10
Fall 2004 COMP 335 11
Example A context-free grammar : A derivation: Fall 2004 COMP 335 12
A context-free grammar : A derivation: Fall 2004 COMP 335 13
Describes matched parentheses: Fall 2004 () ((( ))) (( )) COMP 335 14
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
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
Leftmost derivation: Rightmost derivation: Fall 2004 COMP 335 19
Derivation Trees Fall 2004 COMP 335 20
Fall 2004 COMP 335 21
Fall 2004 COMP 335 22
Fall 2004 COMP 335 23
Fall 2004 COMP 335 24
Derivation Tree Fall 2004 COMP 335 25
Derivation Tree yield Fall 2004 COMP 335 26
Partial Derivation Trees Partial derivation tree Fall 2004 COMP 335 27
Partial derivation tree Fall 2004 COMP 335 28
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 30
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
Example: Parser derivation input ? Fall 2004 COMP 335 33
Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1 Fall 2004 COMP 335 34
Fall 2004 COMP 335 35
Phase 2 Phase 1 Fall 2004 COMP 335 36
Phase 2 Phase 3 Fall 2004 COMP 335 37
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 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 40
Time for phase 2: possible derivations Fall 2004 COMP 335 41
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!!! Fall 2004 COMP 335 43
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
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 time (we will show it in the next class) Fall 2004 COMP 335 47
Ambiguity Fall 2004 COMP 335 48
leftmost derivation Fall 2004 COMP 335 49
leftmost derivation Fall 2004 COMP 335 50
Two derivation trees Fall 2004 COMP 335 51
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 53
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 more leftmost (or rightmost) derivations Fall 2004 COMP 335 55
Why do we care about ambiguity? take Fall 2004 COMP 335 56
Fall 2004 COMP 335 57
Fall 2004 COMP 335 58
Correct result: Fall 2004 COMP 335 59
• 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 335 61
Fall 2004 COMP 335 62
Unique derivation tree Fall 2004 COMP 335 63
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 65
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 67
The string Fall 2004 has two derivation trees COMP 335 68
Compilers Fall 2004 COMP 335 69
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
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 | { 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 * 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 + 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 * mult a, 2, 5 add b, 10, a E 5 COMP 335 76
- Compsci 335
- Every linear grammar generates a regular language
- Proof by contradiction
- Decision properties of regular languages
- Decision properties of regular languages
- Right linear grammar to left linear grammar
- Decision properties of regular languages
- Properties of regular languages
- Closure under union
- Regular and irregular languages
- Pumping lemma non regular languages examples
- Decision properties of regular languages
- Builder design pattern uml diagram
- Cmsc335
- Cpsc 335
- 30 tac 335
- Cpsc 335
- Cmsc 335
- Sim 335
- Csc 335
- 30 tac 335
- What is adoloscence
- Cpsc 335
- 30 tac 335
- Konsep biaya hidup
- Rr 11-2004
- Isi undang-undang nomor 4 tahun 2004
- Dgc 2004
- Bildungsplan deutsch
- Roman numerals 2004
- 2004
- Florida building code 2004
- Established 2004
- Idea 2004 changes
- Maj 2004 biologi
- Copyright 2004
- Hg 974 din 2004
- 2025-2004
- Circolare miur 4099/a/4 2004
- Deped school calendar 2003-2004
- 2025 - 2004
- 2004 ford ranger 2.3 firing order
- Sysc 3120
- Caltech richard murray
- Struktur kurikulum kbk 2004
- Additional support for learning act 2004
- Tbss tornado
- Vaidyanathan committee report
- Ice cube 2004
- Ryan ross 2004
- 2004
- Sysc 2004 course outline
- The age discrimination act 2004
- Curva de oferta a largo plazo de una empresa competitiva
- 14/4/2004
- Lancaster 2004
- Copyright 2004
- 2004 movie
- Mtpdp
- Legge urbanistica regione campania 16/2004
- 699/2004
- Torch infection
- Dose response curve definition apes
- Stewart nozette
- 1/5/2004
- Pipetron
- 2030-2004
- Nos x games brasil em maio de 2004
- 2004 in latin numbers
- Légende amérindienne sirop d érable
- Clara morgane 2004
- Mart avi
- Landasan kurikulum 2004
- Idea 2004 summary
- 2004
- Age discrimination act 2004
- Letra de algarismo romano