Download presentation
Presentation is loading. Please wait.
Published byCaroline Rodgers Modified over 9 years ago
1
1 Context-Free Languages
2
2 Regular Languages
3
3 Context-Free Languages
4
4 Pushdown Automata Context-Free Grammars stack automaton
5
5 Context-Free Grammars
6
6 Grammars Grammars express languages Example: the English language
7
7
8
8 A derivation of “the dog walks”:
9
9 A derivation of “a cat runs”:
10
10 Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }
11
11 Notation VariableTerminal Production Rules
12
12 Another Example Grammar: Derivation of sentence :
13
13 Grammar: Derivation of sentence :
14
14 Other derivations:
15
15 Language of the grammar
16
16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
17
17 Example Grammar :
18
18 We write: Instead of:
19
19 In general we write: If:
20
20 By default:
21
21 Example Grammar Derivations
22
22 Another Grammar Example Grammar : Derivations:
23
23 More Derivations
24
24 Language of a Grammar For a grammar with start variable : String of terminals
25
25 Example For grammar : Since:
26
26 A Convenient Notation
27
27 (((( )))) Describes parentheses:
28
28 A context-free grammar : A derivation: Example
29
29 A context-free grammar : Another derivation:
30
30
31
31 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
32
32 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
33
33 Derivation Order Leftmost derivation: Rightmost derivation:
34
34 Leftmost derivation: Rightmost derivation:
35
35 Ambiguity
36
36 leftmost derivation
38
38 stringhas two leftmost derivations The grammar is ambiguous:
39
39 Definition: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
40
40 We want to remove ambiguity Ambiguity is bad for programming languages It leads to two or more alternative ways to parse statements. This is a disaster for compilers, since they do not know how to resolve such ambiguities.
41
41 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
42
42 The language is not context-free Fact: (This can be can proven using a pumping lemma for context-free languages)
43
43 The Language Hierarchy Regular Languages Context-Free Languages ? ?
44
44 Regular Languages Context-Free Languages Languages accepted by Turing Machines
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.