Grammars 1 Grammars express languages Example the English

  • Slides: 33
Download presentation
Grammars 1

Grammars 1

Grammars express languages Example: the English language 2

Grammars express languages Example: the English language 2

3

3

A derivation of “the boy walks”: 4

A derivation of “the boy walks”: 4

A derivation of “a dog runs”: 5

A derivation of “a dog runs”: 5

Language of the grammar: L = { “a boy runs”, “a boy walks”, “the

Language of the grammar: L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” } 6

Notation Variable or Non-terminal Production rule Terminal 7

Notation Variable or Non-terminal Production rule Terminal 7

Another Example Grammar: Derivation of sentence : 8

Another Example Grammar: Derivation of sentence : 8

Derivation of sentence : 9

Derivation of sentence : 9

Other derivations: 10

Other derivations: 10

Language of the grammar 11

Language of the grammar 11

More Notation Grammar Set of variables Set of terminal symbols Start variable Set of

More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules 12

Example Grammar : 13

Example Grammar : 13

More Notation Sentential Form: A sentence that contains Variables and terminals Example: Sentential Forms

More Notation Sentential Form: A sentence that contains Variables and terminals Example: Sentential Forms sentence 14

We write: Instead of: 15

We write: Instead of: 15

In general we write: If: 16

In general we write: If: 16

By default: 17

By default: 17

Examples Grammar: 18

Examples Grammar: 18

Another Example Grammar G: Derivations: 19

Another Example Grammar G: Derivations: 19

20

20

Language of a Grammar For a grammar with start variable 21

Language of a Grammar For a grammar with start variable 21

Example For grammar G: Since: 22

Example For grammar G: Since: 22

A Convenient Notation 23

A Convenient Notation 23

Linear Grammars 24

Linear Grammars 24

A Linear Grammars with at most one variable on the right side of a

A Linear Grammars with at most one variable on the right side of a production Examples: 25

A Non-Linear Grammar 26

A Non-Linear Grammar 26

Another Linear Grammar 27

Another Linear Grammar 27

Right-Linear Grammars All productions have the form: or Example: 28

Right-Linear Grammars All productions have the form: or Example: 28

Left-Linear Grammars All productions have form: or Example: 29

Left-Linear Grammars All productions have form: or Example: 29

Regular Grammars 30

Regular Grammars 30

Regular Grammars Definition: A regular grammar is any right-linear or left-linear grammar Examples: 31

Regular Grammars Definition: A regular grammar is any right-linear or left-linear grammar Examples: 31

Observation Regular grammars generate regular languages Examples: Grammar G: 32

Observation Regular grammars generate regular languages Examples: Grammar G: 32

Theorem A Language is regular if and only if there is a grammar such

Theorem A Language is regular if and only if there is a grammar such that 33