760 likes | 792 Views
Context-Free Languages. Regular Languages. Context-Free Languages. Regular Languages. Context-Free Languages. Context-Free Grammars. Pushdown Automata. stack. automaton. Context-Free Grammars. Example. A context-free grammar :. A derivation:. A context-free grammar :.
E N D
Context-Free Languages COMP 335
Regular Languages COMP 335
Context-Free Languages Regular Languages COMP 335
Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton COMP 335
Context-Free Grammars COMP 335
Example A context-free grammar : A derivation: COMP 335
A context-free grammar : Another derivation: COMP 335
(((( )))) Describes parentheses: COMP 335
Example A context-free grammar : A derivation: COMP 335
A context-free grammar : Another derivation: COMP 335
Example A context-free grammar : A derivation: COMP 335
A context-free grammar : A derivation: COMP 335
Describes matched parentheses: () ((( ))) (( )) COMP 335
Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable String of variables and terminals COMP 335
Definition: Context-Free Languages • A language is context-free • if and only if • there is a context-freegrammar • with COMP 335
Leftmost derivation: Rightmost derivation: Derivation Order COMP 335
Leftmost derivation: Rightmost derivation: COMP 335
Derivation Trees COMP 335
Derivation Tree COMP 335
Derivation Tree yield COMP 335
Partial Derivation Trees Partial derivation tree COMP 335
Partial derivation tree COMP 335
sentential form Partial derivation tree yield COMP 335
Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree COMP 335
So far, we concentrated on generative aspect of grammars.How about analytical aspect?Parsing….. COMP 335
Parser input string derivation grammar COMP 335
Example: Parser derivation input ? COMP 335
Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1 COMP 335
Phase 2 Phase 1 COMP 335
Phase 2 Phase 3 COMP 335
Final result of exhaustive search (top-down parsing) Parser input derivation COMP 335
Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string is Why? COMP 335
For grammar with rules Time for phase 1: possible derivations COMP 335
Time for phase 2: possible derivations COMP 335
Time for phase : possible derivations: which is exponential in the length of w COMP 335
Total time needed for parsing : phase 1 phase 2|w| phase 2 Extremely bad!!! COMP 335
There exist faster algorithms for specialized grammars S-grammar: symbol string of variables appears once Pair COMP 335
S-grammar example: Each string has a unique derivation COMP 335
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 : COMP 335
For general context-free grammars: There exists a parsing algorithm that parses a string in time (we will show it in the next class) COMP 335
Ambiguity COMP 335
leftmost derivation COMP 335
leftmost derivation COMP 335