672021 Dr Sandip Mal 1 ContextFree Languages 672021
- Slides: 54
6/7/2021 Dr. Sandip Mal 1
Context-Free Languages 6/7/2021 Dr. Sandip Mal 2
Context-Free Languages Regular Languages 6/7/2021 Dr. Sandip Mal 3
Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton 6/7/2021 Dr. Sandip Mal 4
Context-Free Grammars 6/7/2021 Dr. Sandip Mal 5
Example Sequence of terminals and variables Grammar: Variable 6/7/2021 The right side may be Dr. Sandip Mal 6
Grammar: Derivation of string 6/7/2021 : Dr. Sandip Mal 7
Grammar: Derivation of string 6/7/2021 : Dr. Sandip Mal 8
Grammar: Other derivations: 6/7/2021 Dr. Sandip Mal 9
Grammar: Language of the grammar: 6/7/2021 Dr. Sandip Mal 10
A Convenient Notation We write: for zero or more derivation steps Instead of: 6/7/2021 Dr. Sandip Mal 11
In general we write: If: in zero or more derivation steps Trivially: 6/7/2021 Dr. Sandip Mal 12
Example Grammar 6/7/2021 Possible Derivations Dr. Sandip Mal 13
Another convenient notation: 6/7/2021 Dr. Sandip Mal 14
Formal Definitions Grammar: Set of variables 6/7/2021 Set of terminal symbols Start variable Dr. Sandip Mal Set of productions 15
Context-Free Grammar: All productions in are of the form Variable 6/7/2021 String of variables and terminals Dr. Sandip Mal 16
Example of Context-Free Grammar productions variables 6/7/2021 terminals Dr. Sandip Mal start variable 17
Language of a Grammar: For a grammar with start variable String of terminals or 6/7/2021 Dr. Sandip Mal 18
Example: context-free grammar : Since, there is derivation for any 6/7/2021 Dr. Sandip Mal 19
Context-Free Language: A language is context-free if there is a context-free grammar with 6/7/2021 Dr. Sandip Mal 20
Example: is a context-free language since context-free grammar : generates 6/7/2021 Dr. Sandip Mal 21
Another Example Context-free grammar : Example derivations: Palindromes of even length 6/7/2021 Dr. Sandip Mal 22
Another Example Context-free grammar : Example derivations: Describes matched parentheses: 6/7/2021 () ((( ))) (( )) Dr. Sandip Mal 23
Derivation Order and Derivation Trees 6/7/2021 Dr. Sandip Mal 24
Derivation Order Consider the following example grammar with 5 productions: 6/7/2021 Dr. Sandip Mal 25
Leftmost derivation order of string : At each step, we substitute the leftmost variable 6/7/2021 Dr. Sandip Mal 26
Rightmost derivation order of string : At each step, we substitute the rightmost variable 6/7/2021 Dr. Sandip Mal 27
Leftmost derivation of Rightmost derivation of 6/7/2021 Dr. Sandip Mal : : 28
Derivation Trees Consider the same example grammar: And a derivation of 6/7/2021 : Dr. Sandip Mal 29
yield 6/7/2021 Dr. Sandip Mal 30
yield 6/7/2021 Dr. Sandip Mal 31
yield 6/7/2021 Dr. Sandip Mal 32
yield 6/7/2021 Dr. Sandip Mal 33
Derivation Tree (parse tree) yield 6/7/2021 Dr. Sandip Mal 34
Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation: Give same derivation tree 6/7/2021 Dr. Sandip Mal 35
Ambiguity 6/7/2021 Dr. Sandip Mal 36
Grammar for mathematical expressions Example strings: Denotes any number 6/7/2021 Dr. Sandip Mal 37
A leftmost derivation for 6/7/2021 Dr. Sandip Mal 38
Another leftmost derivation for 6/7/2021 Dr. Sandip Mal 39
Two derivation trees for 6/7/2021 Dr. Sandip Mal 40
take 6/7/2021 Dr. Sandip Mal 41
Good Tree Bad Tree Compute expression result using the tree 6/7/2021 Dr. Sandip Mal 42
Two different derivation trees may cause problems in applications which use the derivation trees: • Evaluating expressions • In general, in compilers for programming languages 6/7/2021 Dr. Sandip Mal 43
Ambiguous Grammar: A context-free grammar if there is a string is ambiguous which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa) 6/7/2021 Dr. Sandip Mal 44
Example: this grammar is ambiguous since string 6/7/2021 has two derivation trees Dr. Sandip Mal 45
this grammar is ambiguous also because string 6/7/2021 has two leftmost derivations Dr. Sandip Mal 46
Another ambiguous grammar: IF_STMT if EXPR then STMT else STMT Variables Terminals Very common piece of grammar in programming languages 6/7/2021 Dr. Sandip Mal 47
If expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT if expr 1 if then expr 2 STMT then expr 1 if 6/7/2021 then expr 2 else stmt 2 Two derivation trees IF_STMT if stmt 1 STMT then else stmt 2 stmt 1 Dr. Sandip Mal 48
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 we cannot do so 6/7/2021 Dr. Sandip Mal 49
A successful example: Equivalent Ambiguous Grammar Non-Ambiguous Grammar generates the same language 6/7/2021 Dr. Sandip Mal 50
Unique derivation tree for 6/7/2021 Dr. Sandip Mal 51
An un-successful example: is inherently ambiguous: every grammar that generates this language is ambiguous 6/7/2021 Dr. Sandip Mal 52
Example (ambiguous) grammar for 6/7/2021 Dr. Sandip Mal : 53
The string has always two different derivation trees (for any grammar) For example 6/7/2021 Dr. Sandip Mal 54
- Blessed mal mal
- 672021
- 672021
- No devuelvas mal por mal ni insulto por insulto
- Language
- Pumping lemma for context-free languages examples
- Formal languages and automata theory tutorial
- What languages come from latin
- Languages
- Dispersal hypothesis ap human geography
- Sign language
- European day of languages quiz
- Closure properties of context free languages
- Defence centre for languages and culture
- Reasons for studying concepts of programming languages
- Programming languages
- Interrelated data meaning
- Express languages
- European day of languages quiz
- Ror programming language
- Closure properties of regular languages
- Sound device poetry
- History of computer languages
- Scholars mother tongue
- Carol can speak three foreign languages
- Xiaoman pan
- Turing unrecognizable languages
- Languages known in resume
- Love language test
- Languages 2021
- Balto slavic languages
- Redcap multiple languages
- Key issue 3 why do individual languages vary among places
- Elsa gunter uiuc
- Tandem language
- Closure properties of regular expression
- Languages 5e
- Types of languages
- Formal relational query languages
- Languages
- Hardware description language
- Find the id name dept_name
- Category 1 languages
- Adam doupe cse 340
- Facts about european language day
- Proto language
- Gcse modern foreign languages
- Languages spoken in india
- Regular grammar generates regular language
- Love language list
- Regular and irregular languages
- Decision properties of regular languages
- Introduction to programming languages
- Are all languages equally complex
- Strongly typed vs weakly typed