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