Download presentation
Presentation is loading. Please wait.
Published byReginald Simpson Modified over 9 years ago
1
Prof. Busch - LSU1 Context-Free Languages
2
Prof. Busch - LSU2 Regular Languages Context-Free Languages
3
Prof. Busch - LSU3 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
4
Prof. Busch - LSU4 Context-Free Grammars
5
Prof. Busch - LSU5 Grammars Grammars express languages Example: the English language grammar
6
Prof. Busch - LSU6
7
7 Derivation of string “the dog sleeps”:
8
Prof. Busch - LSU8 Derivation of string “a cat runs”:
9
Prof. Busch - LSU9 Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }
10
Prof. Busch - LSU10 Variables Sequence of Terminals (symbols) Productions Sequence of Variables
11
Prof. Busch - LSU11 Another Example Grammar: Variable Sequence of terminals and variables The right side may be
12
Prof. Busch - LSU12 Grammar: Derivation of string :
13
Prof. Busch - LSU13 Grammar: Derivation of string :
14
Prof. Busch - LSU14 Other derivations: Grammar:
15
Prof. Busch - LSU15 Grammar: Language of the grammar:
16
Prof. Busch - LSU16 We write: Instead of: for zero or more derivation steps A Convenient Notation
17
Prof. Busch - LSU17 in zero or more derivation steps In general we write: If: Trivially:
18
Prof. Busch - LSU18 Example Grammar Possible Derivations
19
Prof. Busch - LSU19 Another convenient notation:
20
Prof. Busch - LSU20 Formal Definitions Set of variables Set of terminal symbols Start variable Set of productions Grammar:
21
Prof. Busch - LSU21 Context-Free Grammar: All productions in are of the form String of variables and terminals Variable
22
Prof. Busch - LSU22 variables terminals productions start variable Example of Context-Free Grammar
23
Prof. Busch - LSU23 For a grammar with start variable String of terminals or Language of a Grammar:
24
Prof. Busch - LSU24 context-free grammar : Example: Since, there is derivation for any
25
Prof. Busch - LSU25 A language is context-free if there is a context-free grammar with Context-Free Language definition:
26
Prof. Busch - LSU26 since context-free grammar : Example: is a context-free language generates
27
Prof. Busch - LSU27 Context-free grammar : Example derivations: Palindromes of even length Another Example
28
Prof. Busch - LSU28 Context-free grammar : Example derivations: () ((( ))) (( )) Describes matched parentheses: Another Example
29
Prof. Busch - LSU29 Derivation Order and Derivation Trees
30
Prof. Busch - LSU30 Derivation Order Consider the following example grammar with 5 productions:
31
Prof. Busch - LSU31 Leftmost derivation order of string : At each step, we substitute the leftmost variable
32
Prof. Busch - LSU32 Rightmost derivation order of string : At each step, we substitute the rightmost variable
33
Prof. Busch - LSU33 Rightmost derivation of : Leftmost derivation of :
34
Prof. Busch - LSU34 Derivation Trees Consider the same example grammar: And a derivation of :
35
Prof. Busch - LSU35 yield
36
Prof. Busch - LSU36 yield
37
Prof. Busch - LSU37 yield
38
Prof. Busch - LSU38 yield
39
Prof. Busch - LSU39 yield Derivation Tree (parse tree)
40
Prof. Busch - LSU40 Give same derivation tree Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation:
41
Prof. Busch - LSU41 Ambiguity
42
Prof. Busch - LSU42 Grammar for mathematical expressions Example strings: Denotes any number
43
Prof. Busch - LSU43 A leftmost derivation for
44
Prof. Busch - LSU44 Another leftmost derivation for
45
Prof. Busch - LSU45 Two derivation trees for
46
Prof. Busch - LSU46 take
47
Prof. Busch - LSU47 Good TreeBad Tree Compute expression result using the tree
48
Prof. Busch - LSU48 Two different derivation trees may cause problems in applications which use the derivation trees: Evaluating expressions In general, in compilers for programming languages
49
Prof. Busch - LSU49 Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa)
50
Prof. Busch - LSU50 stringhas two derivation trees this grammar is ambiguous since Example:
51
Prof. Busch - LSU51 stringhas two leftmost derivations this grammar is ambiguous also because
52
Prof. Busch - LSU52 IF_STMTif EXPR then STMT if EXPR then STMT else STMT Another ambiguous grammar: VariablesTerminals Very common piece of grammar in programming languages
53
Prof. Busch - LSU53 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2 Two derivation trees
54
Prof. Busch - LSU54 In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general ιt is difficult to achieve this
55
Prof. Busch - LSU55 Ambiguous Grammar Non-Ambiguous Grammar Equivalent generates the same language A successful example:
56
Prof. Busch - LSU56 Unique derivation tree for
57
Prof. Busch - LSU57 An un-successful example: every grammar that generates this language is ambiguous is inherently ambiguous:
58
Prof. Busch - LSU58 Example (ambiguous) grammar for :
59
Prof. Busch - LSU59 The string has always two different derivation trees (for any grammar) For example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.