Download presentation
Presentation is loading. Please wait.
Published byAshley Beasley Modified over 8 years ago
1
Context-Free Languages
2
Regular Languages
3
Context-Free Languages
4
Pushdown Automata Context-Free Grammars stack automaton
5
Context-Free Grammars
6
Example A context-free grammar : A derivation:
7
A context-free grammar : Another derivation:
8
(((( )))) Describes parentheses:
9
A context-free grammar : A derivation: Example
10
A context-free grammar : Another derivation:
12
A context-free grammar : A derivation: Example
13
A context-free grammar : A derivation:
14
() ((( ))) (( )) Describes matched parentheses:
15
Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
17
Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
18
Derivation Order Leftmost derivation: Rightmost derivation:
19
Leftmost derivation: Rightmost derivation:
20
Derivation Trees
25
Derivation Tree
26
yield Derivation Tree
27
Partial Derivation Trees Partial derivation tree
29
sentential form yield
30
Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
31
Ambiguity
32
leftmost derivation
34
Two derivation trees
35
The grammar is ambiguous: stringhas two derivation trees
36
stringhas two leftmost derivations The grammar is ambiguous:
37
Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees
38
In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
39
Why do we care about ambiguity? take
42
Correct result:
43
We want to remove ambiguity Ambiguity is bad for programming languages
44
We fix the ambiguous grammar: New non-ambiguous grammar:
46
Unique derivation tree
47
The grammar : is non-ambiguous: Every string has a unique derivation tree
48
Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT
49
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
Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
51
The string has two derivation trees
52
Compilers
53
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
Lexical analyzer parser Compiler program machine code input output
55
A parser knows the grammar of the programming language
56
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
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
10 E 25 E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 derivation derivation tree EE EE + *
59
10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code
60
Parsing
61
grammar Parser input string derivation
62
Example: Parser derivation input ?
63
Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
65
Phase 2 Phase 1
66
Phase 2 Phase 3
67
Final result of exhaustive search Parser derivation input (top-down parsing)
68
Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :
69
Time for phase 1: possible derivations For grammar with rules
70
Time for phase 2: possible derivations
71
Time for phase : possible derivations
72
Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|
73
There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair
74
S-grammar example: Each string has a unique derivation
75
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
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
© 2024 SlidePlayer.com. Inc.
All rights reserved.