lecture 06 syntax analysis 3

81
SYNTAX ANALYSIS OR PARSING Lecture 06

Upload: iffat-anjum

Post on 08-Feb-2017

413 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Lecture 06 syntax analysis 3

SYNTAX ANALYSISORPARSINGLecture 06

Page 2: Lecture 06 syntax analysis 3

FOLLOW EXAMPLE S a S e | B B b B C f | C C c C g | d |

FIRST(C) = FIRST(B) = FIRST(S) =

FOLLOW(C) = FOLLOW(B) = FOLLOW(S) = {$}

Assume the first non-terminal is the start symbol1. If A is start symbol, put $ in FOLLOW(A)2. Productions of the form B A ,

Add FIRST() – {} to FOLLOW(A)3. Productions of the form B A or B A where *

Add FOLLOW(B) to FOLLOW(A)

2

Page 3: Lecture 06 syntax analysis 3

FOLLOW EXAMPLE S a S e | B B b B C f | C C c C g | d |

FIRST(C) = {c,d,} FIRST(B) = {b,c,d,} FIRST(S) = {a,b,c,d,}

FOLLOW(C) =

FOLLOW(B) =

FOLLOW(S) = { }$, e

{c,d} FOLLOW(S)

= {c,d,e,$}

{f,g} FOLLOW(B)

= {c,d,e,f,g,$}

3

Page 4: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

4

Page 5: Lecture 06 syntax analysis 3

PREDICTIVE PARSING LL(1) Grammars

Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol

First L : Left to Right Scanning Second L: Leftmost derivation 1 : one input symbol look-ahead for predictive decision

LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols

Techniques to modify the grammar: Left Factoring Removal of Left Recursion

LL(k) Language Can be described with an LL(k) grammar 5

Page 6: Lecture 06 syntax analysis 3

TABLE DRIVEN PREDICTIVE PARSING

6

Page 7: Lecture 06 syntax analysis 3

PARSE TABLE CONSTRUCTION

7

Page 8: Lecture 06 syntax analysis 3

TABLE DRIVEN PREDICTIVE PARSING

8

Page 9: Lecture 06 syntax analysis 3

TABLE DRIVEN PREDICTIVE PARSING

9

Page 10: Lecture 06 syntax analysis 3

PREDICTIVE PARSING ALGORITHM

10

Page 11: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

11

Page 12: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

12

Page 13: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

13

Page 14: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

14

Page 15: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

15

Page 16: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

16

Page 17: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

17

Page 18: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

18

Page 19: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

19

Page 20: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

20

Page 21: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

21

Page 22: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

22

Page 23: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

23

Page 24: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

24

Page 25: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

25

Page 26: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

26

Page 27: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

27

Page 28: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

28

Page 29: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

29

Page 30: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

30

Page 31: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

31

Page 32: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

32

Page 33: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

33

Page 34: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

34

Page 35: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

35

Page 36: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

36

Page 37: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

37

Page 38: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

38

Page 39: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

39

Page 40: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

40

Page 41: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

41

Page 42: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

42

Page 43: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

43

Page 44: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

44

Page 45: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

45

Page 46: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

46

Page 47: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

47

Page 48: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

48

Page 49: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

49

Page 50: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

50

Page 51: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

51

Page 52: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

52

Page 53: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

53

Page 54: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

54

Page 55: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

55

Page 56: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

56

Page 57: Lecture 06 syntax analysis 3

PREDICTIVE PARSING

57

Page 58: Lecture 06 syntax analysis 3

RECONSTRUCTING THE PARSE TREE

58

Page 59: Lecture 06 syntax analysis 3

RECONSTRUCTING THE PARSE TREE

59

Page 60: Lecture 06 syntax analysis 3

RECONSTRUCTING THE PARSE TREE

60

Page 61: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

61

Page 62: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

62

Page 63: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

63

Page 64: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

64

Page 65: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

65

Page 66: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

66

Page 67: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

67

Page 68: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

68

Page 69: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

69

Page 70: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

70

Page 71: Lecture 06 syntax analysis 3

EXAMPLE: THE “DANGLING ELSE” GRAMMAR

71

Page 72: Lecture 06 syntax analysis 3

LL(1) GRAMMAR LL(1) grammars

Are never ambiguous. Will never have left recursion.

Furthermore... If we are looking for an “A” and the next symbol

is “b”, Then only one production must be possible

Although elimination of left recursion and left factoring is easy. Some grammar will never be a LL(1) grammar.

LL(1) Grammar

72

Page 73: Lecture 06 syntax analysis 3

LL(1) GRAMMAR

73

Page 74: Lecture 06 syntax analysis 3

PROPERTIES OF LL(1) GRAMMAR A grammar G is LL(1) if an only if whenever

A are two distinct productions of G the following conditions hold:1. For no terminal a do both and derive

strings beginning with a.2. At most one of and can derive the empty

string.3. If then * then does not derive any

string beginning with a terminal in FOLLOW(A).

74

Page 75: Lecture 06 syntax analysis 3

ERROR RECOVERY

a + b $

Y

X

$

Z

Input

Predictive Parsing Program

Stack Output

Parsing Table M[A,a]

When Do Errors Occur? Recall Predictive Parser Function:

1. If X is a terminal and it doesn’t match input.

2. If M[X, Input] is empty – No allowable actions 75

Page 76: Lecture 06 syntax analysis 3

ERROR RECOVERY

76

Page 77: Lecture 06 syntax analysis 3

ERROR RECOVERY: SKIP INPUT SYMBOLS

77

Page 78: Lecture 06 syntax analysis 3

ERROR RECOVERY: POP THE STACK

78

Page 79: Lecture 06 syntax analysis 3

ERROR RECOVERY: PANIC MODE

79

Page 80: Lecture 06 syntax analysis 3

ERROR RECOVERY - TABLE ENTRIES

80

Page 81: Lecture 06 syntax analysis 3

QUESTIONS ?

81