672021 Dr Sandip Mal 1 ContextFree Languages 672021

  • Slides: 54
Download presentation
6/7/2021 Dr. Sandip Mal 1

6/7/2021 Dr. Sandip Mal 1

Context-Free Languages 6/7/2021 Dr. Sandip Mal 2

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 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 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

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

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 7

Grammar: Derivation of string 6/7/2021 : Dr. Sandip Mal 8

Grammar: Derivation of string 6/7/2021 : Dr. Sandip Mal 8

Grammar: Other derivations: 6/7/2021 Dr. Sandip Mal 9

Grammar: Other derivations: 6/7/2021 Dr. Sandip Mal 9

Grammar: Language of the grammar: 6/7/2021 Dr. Sandip Mal 10

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

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.

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

Example Grammar 6/7/2021 Possible Derivations Dr. Sandip Mal 13

Another convenient notation: 6/7/2021 Dr. Sandip Mal 14

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.

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

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

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

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

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

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

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

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 () ((( )))

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 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

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

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

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

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.

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 30

yield 6/7/2021 Dr. Sandip Mal 31

yield 6/7/2021 Dr. Sandip Mal 31

yield 6/7/2021 Dr. Sandip Mal 32

yield 6/7/2021 Dr. Sandip Mal 32

yield 6/7/2021 Dr. Sandip Mal 33

yield 6/7/2021 Dr. Sandip Mal 33

Derivation Tree (parse tree) yield 6/7/2021 Dr. Sandip Mal 34

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

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

Ambiguity 6/7/2021 Dr. Sandip Mal 36

Grammar for mathematical expressions Example strings: Denotes any number 6/7/2021 Dr. Sandip Mal 37

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

A leftmost derivation for 6/7/2021 Dr. Sandip Mal 38

Another leftmost derivation for 6/7/2021 Dr. Sandip Mal 39

Another leftmost derivation for 6/7/2021 Dr. Sandip Mal 39

Two derivation trees for 6/7/2021 Dr. Sandip Mal 40

Two derivation trees for 6/7/2021 Dr. Sandip Mal 40

take 6/7/2021 Dr. Sandip Mal 41

take 6/7/2021 Dr. Sandip Mal 41

Good Tree Bad Tree Compute expression result using the tree 6/7/2021 Dr. Sandip Mal

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:

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:

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

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

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

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 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

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.

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

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

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

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

The string has always two different derivation trees (for any grammar) For example 6/7/2021 Dr. Sandip Mal 54