Download presentation
Presentation is loading. Please wait.
Published byMerryl York Modified over 9 years ago
1
Fall 2004COMP 3351 Context-Free Languages
2
Fall 2004COMP 3352 Regular Languages
3
Fall 2004COMP 3353 Regular Languages Context-Free Languages
4
Fall 2004COMP 3354 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
5
Fall 2004COMP 3355 Context-Free Grammars
6
Fall 2004COMP 3356 Example A context-free grammar : A derivation:
7
Fall 2004COMP 3357 A context-free grammar : Another derivation:
8
Fall 2004COMP 3358 (((( )))) Describes parentheses:
9
Fall 2004COMP 3359 A context-free grammar : A derivation: Example
10
Fall 2004COMP 33510 A context-free grammar : Another derivation:
11
Fall 2004COMP 33511
12
Fall 2004COMP 33512 A context-free grammar : A derivation: Example
13
Fall 2004COMP 33513 A context-free grammar : A derivation:
14
Fall 2004COMP 33514 () ((( ))) (( )) Describes matched parentheses:
15
Fall 2004COMP 33515 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
16
Fall 2004COMP 33516
17
Fall 2004COMP 33517 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
18
Fall 2004COMP 33518 Derivation Order Leftmost derivation: Rightmost derivation:
19
Fall 2004COMP 33519 Leftmost derivation: Rightmost derivation:
20
Fall 2004COMP 33520 Derivation Trees
21
Fall 2004COMP 33521
22
Fall 2004COMP 33522
23
Fall 2004COMP 33523
24
Fall 2004COMP 33524
25
Fall 2004COMP 33525 Derivation Tree
26
Fall 2004COMP 33526 yield Derivation Tree
27
Fall 2004COMP 33527 Partial Derivation Trees Partial derivation tree
28
Fall 2004COMP 33528 Partial derivation tree
29
Fall 2004COMP 33529 Partial derivation tree sentential form yield
30
Fall 2004COMP 33530 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
31
Fall 2004COMP 33531 So far, we concentrated on generative aspect of grammars. How about analytical aspect? Parsing…..
32
Fall 2004COMP 33532 grammar Parser input string derivation
33
Fall 2004COMP 33533 Example: Parser derivation input ?
34
Fall 2004COMP 33534 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
35
Fall 2004COMP 33535
36
Fall 2004COMP 33536 Phase 2 Phase 1
37
Fall 2004COMP 33537 Phase 2 Phase 3
38
Fall 2004COMP 33538 Final result of exhaustive search Parser derivation input (top-down parsing)
39
Fall 2004COMP 33539 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string is Why?
40
Fall 2004COMP 33540 Time for phase 1: possible derivations For grammar with rules
41
Fall 2004COMP 33541 Time for phase 2: possible derivations
42
Fall 2004COMP 33542 Time for phase : possible derivations: which is exponential in the length of w
43
Fall 2004COMP 33543 Total time needed for parsing : Extremely bad!!! phase 1 phase 2 phase 2|w|
44
Fall 2004COMP 33544 There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair
45
Fall 2004COMP 33545 S-grammar example: Each string has a unique derivation
46
Fall 2004COMP 33546 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:
47
Fall 2004COMP 33547 For general context-free grammars: There exists a parsing algorithm that parses a string in time (we will show it in the next class)
48
Fall 2004COMP 33548 Ambiguity
49
Fall 2004COMP 33549 leftmost derivation
50
Fall 2004COMP 33550 leftmost derivation
51
Fall 2004COMP 33551 Two derivation trees
52
Fall 2004COMP 33552 The grammar is ambiguous: since there is a string, namely, which has two derivation trees
53
Fall 2004COMP 33553 has two leftmost derivations The grammar is ambiguous: since string
54
Fall 2004COMP 33554 Definition: A context-free grammar is ambiguous if some string has two or more distinct derivation trees
55
Fall 2004COMP 33555 Alternatively we may say: Ambiguity of a grammar implies the existence of two or more leftmost (or rightmost) derivations
56
Fall 2004COMP 33556 Why do we care about ambiguity? take
57
Fall 2004COMP 33557
58
Fall 2004COMP 33558
59
Fall 2004COMP 33559 Correct result:
60
Fall 2004COMP 33560 We want to remove ambiguity Ambiguity is bad for programming languages
61
Fall 2004COMP 33561 We may be able to fix the ambiguity: New non-ambiguous grammar:
62
Fall 2004COMP 33562
63
Fall 2004COMP 33563 Unique derivation tree
64
Fall 2004COMP 33564 The grammar : is non-ambiguous: Every string has a unique derivation tree
65
Fall 2004COMP 33565 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT
66
Fall 2004COMP 33566 if expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2
67
Fall 2004COMP 33567 Inherent Ambiguity Some context-free languages have only ambiguous grammars Example:
68
Fall 2004COMP 33568 The string has two derivation trees
69
Fall 2004COMP 33569 Compilers
70
Fall 2004COMP 33570 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
71
Fall 2004COMP 33571 Lexical analyzer parser Compiler program machine code input output
72
Fall 2004COMP 33572 A parser knows the grammar of the programming language
73
Fall 2004COMP 33573 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
74
Fall 2004COMP 33574 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
75
Fall 2004COMP 33575 10 E 25 E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 derivation derivation tree EE EE + *
76
Fall 2004COMP 33576 10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.