CSCI 3136 Principles of Programming Languages Syntactic Analysis - - PowerPoint PPT Presentation

csci 3136 principles of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSCI 3136 Principles of Programming Languages Syntactic Analysis - - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Syntactic Analysis and Context-Free Grammars - 2 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 102 Language defined by a CFG L ( G ) = { w | S w } 2 / 102 Language


slide-1
SLIDE 1

CSCI 3136 Principles of Programming Languages

Syntactic Analysis and Context-Free Grammars - 2

Summer 2013 Faculty of Computer Science Dalhousie University

1 / 102

slide-2
SLIDE 2

Language defined by a CFG

L(G) = {w|S ⇒∗ w}

2 / 102

slide-3
SLIDE 3

Language defined by a CFG

L(G) = {w|S ⇒∗ w} If G is a grammar, the language of the grammar, denoted as L(G), is the set of terminal strings that have derivations from the start symbol.

3 / 102

slide-4
SLIDE 4

Language defined by a CFG

L(G) = {w|S ⇒∗ w} If G is a grammar, the language of the grammar, denoted as L(G), is the set of terminal strings that have derivations from the start symbol.

  • Languages defined by CFGs are context-free languages

4 / 102

slide-5
SLIDE 5

Language defined by a CFG

L(G) = {w|S ⇒∗ w} If G is a grammar, the language of the grammar, denoted as L(G), is the set of terminal strings that have derivations from the start symbol.

  • Languages defined by CFGs are context-free languages
  • Example: Our ‘Jim-ate-cheese’ grammar generates the

following language: L(G) = {“Jim ate cheese”, “Jim ate Jim”, “cheese ate cheese”, “cheese ate Jim”, “big Jim ate cheese”, “big Jim ate Jim”, “big cheese ate cheese”, · · · }

5 / 102

slide-6
SLIDE 6

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

6 / 102

slide-7
SLIDE 7

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)?

7 / 102

slide-8
SLIDE 8

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ

8 / 102

slide-9
SLIDE 9

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)?

9 / 102

slide-10
SLIDE 10

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01

10 / 102

slide-11
SLIDE 11

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)?

11 / 102

slide-12
SLIDE 12

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)? because S ⇒ 0S1 ⇒ 00S11 ⇒ 0011

12 / 102

slide-13
SLIDE 13

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)? because S ⇒ 0S1 ⇒ 00S11 ⇒ 0011
  • Is 0n1n in L(G)?

13 / 102

slide-14
SLIDE 14

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)? because S ⇒ 0S1 ⇒ 00S11 ⇒ 0011
  • Is 0n1n in L(G)? because S ⇒∗ 0n1n

14 / 102

slide-15
SLIDE 15

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)? because S ⇒ 0S1 ⇒ 00S11 ⇒ 0011
  • Is 0n1n in L(G)? because S ⇒∗ 0n1n

What language is defined by the following CFG? S → ǫ S → 0S1

15 / 102

slide-16
SLIDE 16

CF Languages: Example (1)

G = ({S}, {0, 1}, {S → 0S1|ǫ}, S)

  • Is ǫ in L(G)? because S ⇒ ǫ
  • Is 01 in L(G)? because S ⇒ 0S1 ⇒ 01
  • Is 0011 in L(G)? because S ⇒ 0S1 ⇒ 00S11 ⇒ 0011
  • Is 0n1n in L(G)? because S ⇒∗ 0n1n

What language is defined by the following CFG? S → ǫ S → 0S1 L(G) = {0n1n|n ≥ 0}

16 / 102

slide-17
SLIDE 17

CF Languages: Example (2)

What language is defined by the following CFG? S → ǫ S → 0S0 S → 1S1

17 / 102

slide-18
SLIDE 18

CF Languages: Example (2)

What language is defined by the following CFG? S → ǫ S → 0S0 S → 1S1 L(G) = {wwR|w ∈ {0, 1}∗} where wR is w written backwards

18 / 102

slide-19
SLIDE 19

CF Languages: Example (2)

What language is defined by the following CFG? S → ǫ S → 0S0 S → 1S1 L(G) = {wwR|w ∈ {0, 1}∗} where wR is w written backwards Neither of these two languages is regular.

19 / 102

slide-20
SLIDE 20

CF Languages: Example (2)

What language is defined by the following CFG? S → ǫ S → 0S0 S → 1S1 L(G) = {wwR|w ∈ {0, 1}∗} where wR is w written backwards Neither of these two languages is regular. There are more context-free languages than regular ones.

20 / 102

slide-21
SLIDE 21

Classes of Chomsky Hierarchy

21 / 102

slide-22
SLIDE 22

Regular grammar (1)

22 / 102

slide-23
SLIDE 23

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:

23 / 102

slide-24
SLIDE 24

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ

24 / 102

slide-25
SLIDE 25

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ

25 / 102

slide-26
SLIDE 26

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

26 / 102

slide-27
SLIDE 27

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

In a left regular grammar (or left linear grammar), all rules obey the forms:

27 / 102

slide-28
SLIDE 28

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

In a left regular grammar (or left linear grammar), all rules obey the forms:

  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → Ca, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

28 / 102

slide-29
SLIDE 29

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

In a left regular grammar (or left linear grammar), all rules obey the forms:

  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → Ca, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

A (context-free) grammar is regular if it is a left or right regular grammar.

29 / 102

slide-30
SLIDE 30

Regular grammar (1)

A right regular grammar (or right linear grammar) is a formal grammar (N, Σ, P, S) such that all the production rules in P are of

  • ne of the following forms:
  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → aC, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

In a left regular grammar (or left linear grammar), all rules obey the forms:

  • 1. B → a, where B ∈ N and a ∈ Σ
  • 2. B → Ca, where B, C ∈ N and a ∈ Σ
  • 3. B → ǫ, where B ∈ N and ǫ denotes the empty string

A (context-free) grammar is regular if it is a left or right regular grammar. Regular grammars are too weak to express programming languages.

30 / 102

slide-31
SLIDE 31

Regular grammar (2)

Is the following grammar (with start non-terminal S) regular or context-free? S → 0S S → 1A A → ǫ A → 2A

31 / 102

slide-32
SLIDE 32

Regular grammar (2)

Is the following grammar (with start non-terminal S) regular or context-free? S → 0S S → 1A A → ǫ A → 2A If it is a regular grammar then what is the equivalent regular expression? Otherwise, write down the context-free language defined by the above grammar.

32 / 102

slide-33
SLIDE 33

Regular grammar (2)

Is the following grammar (with start non-terminal S) regular or context-free? S → 0S S → 1A A → ǫ A → 2A If it is a regular grammar then what is the equivalent regular expression? Otherwise, write down the context-free language defined by the above grammar. 0∗12∗

33 / 102

slide-34
SLIDE 34

Regular grammar (3)

What language is defined by the following grammar (with start non-terminal S)? S → 0A A → S1 S → ǫ

34 / 102

slide-35
SLIDE 35

Regular grammar (3)

What language is defined by the following grammar (with start non-terminal S)? S → 0A A → S1 S → ǫ L(G) = {0n1n|n ≥ 0}

35 / 102

slide-36
SLIDE 36

Regular grammar (3)

What language is defined by the following grammar (with start non-terminal S)? S → 0A A → S1 S → ǫ L(G) = {0n1n|n ≥ 0} Is the language defined by the above grammar regular or context-free? Prove your answer.

36 / 102

slide-37
SLIDE 37

Parse Trees

37 / 102

slide-38
SLIDE 38

Parse Trees

Every derivation can be represented by a parse tree:

38 / 102

slide-39
SLIDE 39

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

39 / 102

slide-40
SLIDE 40

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P
  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

40 / 102

slide-41
SLIDE 41

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P

S P V P

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

41 / 102

slide-42
SLIDE 42

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P

S P V P

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

42 / 102

slide-43
SLIDE 43

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P

S P V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

43 / 102

slide-44
SLIDE 44

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P

S P V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

44 / 102

slide-45
SLIDE 45

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P

S P V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

45 / 102

slide-46
SLIDE 46

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N

S P N V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

46 / 102

slide-47
SLIDE 47

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N

S P N V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

47 / 102

slide-48
SLIDE 48

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N

S P N V P N

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

48 / 102

slide-49
SLIDE 49

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

S P N V P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

49 / 102

slide-50
SLIDE 50

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

S P N V P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

50 / 102

slide-51
SLIDE 51

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

S P N V P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

51 / 102

slide-52
SLIDE 52

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

⇒ Jim ate N S P N V ate P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

52 / 102

slide-53
SLIDE 53

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

⇒ Jim ate N S P N V ate P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

53 / 102

slide-54
SLIDE 54

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

⇒ Jim ate N S P N V ate P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

54 / 102

slide-55
SLIDE 55

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

⇒ Jim ate N ⇒ Jim ate cheese S P N cheese V ate P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

55 / 102

slide-56
SLIDE 56

Parse Trees

Every derivation can be represented by a parse tree: ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N

⇒ Jim ate N ⇒ Jim ate cheese S P N cheese V ate P N Jim

  • The root is S
  • The children of each node are the symbols (terminals and

non-terminals) it is replaced with.

  • The internal nodes are non-terminals. The leaves–called the

yield of the parse-tree–are terminals.

56 / 102

slide-57
SLIDE 57

Parse Tree (another example)

57 / 102

slide-58
SLIDE 58

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

58 / 102

slide-59
SLIDE 59

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P

59 / 102

slide-60
SLIDE 60

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P

S P V P

60 / 102

slide-61
SLIDE 61

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P

S P V P

61 / 102

slide-62
SLIDE 62

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P

S P V P P A

62 / 102

slide-63
SLIDE 63

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P

S P V P P A

63 / 102

slide-64
SLIDE 64

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P

S P V P P A

64 / 102

slide-65
SLIDE 65

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P

S P V P P A big

65 / 102

slide-66
SLIDE 66

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P

S P V P P A big

66 / 102

slide-67
SLIDE 67

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P

S P V P P A big

67 / 102

slide-68
SLIDE 68

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P S P V P P N A big

68 / 102

slide-69
SLIDE 69

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P S P V P P N A big

69 / 102

slide-70
SLIDE 70

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P S P V P P N A big

70 / 102

slide-71
SLIDE 71

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P S P V P P N Jim A big

71 / 102

slide-72
SLIDE 72

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P S P V P P N Jim A big

72 / 102

slide-73
SLIDE 73

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P S P V P P N Jim A big

73 / 102

slide-74
SLIDE 74

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P S P V ate P P N Jim A big

74 / 102

slide-75
SLIDE 75

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P S P V ate P P N Jim A big

75 / 102

slide-76
SLIDE 76

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P S P V ate P P N Jim A big

76 / 102

slide-77
SLIDE 77

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P S P P A V ate P P N Jim A big

77 / 102

slide-78
SLIDE 78

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P S P P A V ate P P N Jim A big

78 / 102

slide-79
SLIDE 79

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P S P P A V ate P P N Jim A big

79 / 102

slide-80
SLIDE 80

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P S P P A green V ate P P N Jim A big

80 / 102

slide-81
SLIDE 81

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P S P P A green V ate P P N Jim A big

81 / 102

slide-82
SLIDE 82

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P S P P A green V ate P P N Jim A big

82 / 102

slide-83
SLIDE 83

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P ⇒ big Jim ate green N S P P N A green V ate P P N Jim A big

83 / 102

slide-84
SLIDE 84

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P ⇒ big Jim ate green N S P P N A green V ate P P N Jim A big

84 / 102

slide-85
SLIDE 85

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P ⇒ big Jim ate green N S P P N A green V ate P P N Jim A big

85 / 102

slide-86
SLIDE 86

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P ⇒ big Jim ate green N ⇒ big Jim ate green cheese S P P N cheese A green V ate P P N Jim A big

86 / 102

slide-87
SLIDE 87

Parse Tree (another example)

———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ————————

  • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒

big N V P ⇒ big Jim V P ⇒ big Jim ate P ⇒ big Jim ate A P ⇒ big Jim ate green P ⇒ big Jim ate green N ⇒ big Jim ate green cheese S P P N cheese A green V ate P P N Jim A big

87 / 102

slide-88
SLIDE 88

Ambiguity: Example

88 / 102

slide-89
SLIDE 89

Ambiguity: Example

A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E/E E → (E) ————————

89 / 102

slide-90
SLIDE 90

Ambiguity: Example

A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E/E E → (E) ———————— 2 + 3 ∗ 4

90 / 102

slide-91
SLIDE 91

Ambiguity: Example

A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E/E E → (E) ———————— 2 + 3 ∗ 4 E E E 4 * E 3 + E 2

91 / 102

slide-92
SLIDE 92

Ambiguity: Example

A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E/E E → (E) ———————— 2 + 3 ∗ 4 E E E 4 * E 3 + E 2 E E 4 * E E 3 + E 2

92 / 102

slide-93
SLIDE 93

Ambiguity: Example

A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E/E E → (E) ———————— 2 + 3 ∗ 4 E E E 4 * E 3 + E 2 E E 4 * E E 3 + E 2 Violates precedence rules.

93 / 102

slide-94
SLIDE 94

Ambiguity

94 / 102

slide-95
SLIDE 95

Ambiguity

  • There may be more than one parse tree for the same sentence

generated by a grammar G. If this is the case, we call G ambiguous.

95 / 102

slide-96
SLIDE 96

Ambiguity

  • There may be more than one parse tree for the same sentence

generated by a grammar G. If this is the case, we call G ambiguous.

  • Problems of ambiguous grammar: one sentence has different

interpretations.

96 / 102

slide-97
SLIDE 97

Some facts about multiple representations

97 / 102

slide-98
SLIDE 98

Some facts about multiple representations

  • There are infinitely many context-free grammars generating a

given context-free language.

98 / 102

slide-99
SLIDE 99

Some facts about multiple representations

  • There are infinitely many context-free grammars generating a

given context-free language. (How?)

99 / 102

slide-100
SLIDE 100

Some facts about multiple representations

  • There are infinitely many context-free grammars generating a

given context-free language. (How?)

  • Just add arbitrary non-terminals to the right-hand sides of

productions and then add ǫ-productions for these non-terminals.

100 / 102

slide-101
SLIDE 101

Some facts about multiple representations

  • There are infinitely many context-free grammars generating a

given context-free language. (How?)

  • Just add arbitrary non-terminals to the right-hand sides of

productions and then add ǫ-productions for these non-terminals.

  • There may be more than one parse tree for the same sentence

generated by a grammar.

101 / 102

slide-102
SLIDE 102

Some facts about multiple representations

  • There are infinitely many context-free grammars generating a

given context-free language. (How?)

  • Just add arbitrary non-terminals to the right-hand sides of

productions and then add ǫ-productions for these non-terminals.

  • There may be more than one parse tree for the same sentence

generated by a grammar.

  • For programming languages, we require that CFG is

unambiguous.

102 / 102