lr parsing - the algorithm -...

55
LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T. Koether Hampden-Sydney College Wed, Feb 18, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 1 / 19

Upload: others

Post on 24-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

LR Parsing - The AlgorithmLecture 13

Sections 4.5, 4.7

Robb T. Koether

Hampden-Sydney College

Wed, Feb 18, 2015

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 1 / 19

Page 2: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

1 The Algorithm

2 Shift/Reduce Conflicts

3 Precedence and Associativity

4 Assignment

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 2 / 19

Page 3: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Outline

1 The Algorithm

2 Shift/Reduce Conflicts

3 Precedence and Associativity

4 Assignment

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 3 / 19

Page 4: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 5: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 6: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 7: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 8: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 9: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 10: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 11: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 12: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 ( 4 id + id ) * id $

0 ( 4 id 5 + id ) * id $

0 ( 4 F 3 + id ) * id $

0 ( 4 T 2 + id ) * id $

0 ( 4 E 8 + id ) * id $

0 ( 4 E 8 + 6 id ) * id $

0 ( 4 E 8 + 6 id 5 ) * id $

0 ( 4 E 8 + 6 F 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 4 / 19

Page 13: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 14: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 15: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 16: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 17: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 18: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 19: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 20: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 21: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 22: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 23: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 ( 4 E 8 + 6 T 9 ) * id $

0 ( 4 E 8 ) * id $

0 ( 4 E 8 ) 11 * id $

0 F 3 * id $

0 T 2 * id $

0 T 2 * 7 id $

0 T 2 * 7 id 5 $

0 T 2 * 7 F 10 $

0 T 2 $

0 E 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 5 / 19

Page 24: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

In practice, we need only push the state numbers onto the stack.There is no need to push the grammar symbols.Thus, the previous example can be streamlined.

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 6 / 19

Page 25: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 26: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 27: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 28: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 29: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 30: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 31: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 32: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 33: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Parse ( id + id )∗ id)

Stack Input0 ( id + id ) * id $

0 4 id + id ) * id $

0 4 5 + id ) * id $

0 4 3 + id ) * id $

0 4 2 + id ) * id $

0 4 8 + id ) * id $

0 4 8 6 id ) * id $

0 4 8 6 5 ) * id $

0 4 8 6 3 ) * id $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 7 / 19

Page 34: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 35: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 36: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 37: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 38: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 39: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 40: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 41: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 42: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 43: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 44: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (LR Parsing)

Stack Input0 4 8 6 9 ) * id $

0 4 8 ) * id $

0 4 8 11 * id $

0 3 * id $

0 2 * id $

0 2 7 id $

0 2 7 5 $

0 2 7 10 $

0 2 $

0 1 $

Accept $

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 8 / 19

Page 45: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (A Simplified Grammar)We may simplify our grammar to

E → E + EE → E * EE → ( E )

E → id

In this form, the precedence rules for + and * are not implicit.They must be incorporated into the tables.

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 9 / 19

Page 46: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Outline

1 The Algorithm

2 Shift/Reduce Conflicts

3 Precedence and Associativity

4 Assignment

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 10 / 19

Page 47: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Shift/Reduce Conflicts

It is possible that a cell will contain both a shift operation and areduce operation.This is called a shift/reduce conflict.To choose between “shift” and “reduce,” each case must beconsidered on its own merit.Consider the case of E → E + E | E ∗ E and the inputsa + b * c and a * b + c.

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 11 / 19

Page 48: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Reduce/Reduce Conflicts

It is possible that a cell will contain two different reduce operations.This is called a reduce/reduce conflict.This occurs when a sequence of tokens matches the right-handsides of two different productions at the same time.For each such conflict in the table, we must choose whichreduction to apply.

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 12 / 19

Page 49: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (The Action and Goto Tables)State Action Goto

+ * ( ) id $ E0 s2 s3 11 s5 s6 acc2 s2 s3 73 r4 r4 r4 r44 r5 r5 r5 r55 s2 s3 86 s2 s3 97 s5 s6 s108 s5/r1 s6/r1 r1 r19 s5/r2 s6/r2 r2 r2

10 r3 r3 r3 r3

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 13 / 19

Page 50: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Outline

1 The Algorithm

2 Shift/Reduce Conflicts

3 Precedence and Associativity

4 Assignment

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 14 / 19

Page 51: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Shift/Reduce Conflicts and Associativity)The shift/reduce conflict in cell (8,+) is between shifting a + andreducing by

E → E + E .

If we choose “shift,” then we will make addition right associative.If we choose “reduce,” then we will make addition left associative.The case is similar in cell (9,*) regarding multiplication.

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 15 / 19

Page 52: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (Shift/Reduce Conflicts and Precedence)The shift/reduce conflict in cell (9,+) is between shifting a + andreducing by

E → E * E .

If we choose “shift,” then we will give multiplication a higherprecedence than addition.If we choose “reduce,” then we will give addition a higherprecedence than multiplication.The case is similar in cell (8,*).

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 16 / 19

Page 53: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Example

Example (The Action and Goto Tables)State Action Goto

+ * ( ) id $ E0 s2 s3 11 s5 s6 acc2 s2 s3 73 r4 r4 r4 r44 r5 r5 r5 r55 s2 s3 86 s2 s3 97 s5 s6 s108 r1 s6 r1 r19 r2 r2 r2 r210 r3 r3 r3 r3

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 17 / 19

Page 54: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Outline

1 The Algorithm

2 Shift/Reduce Conflicts

3 Precedence and Associativity

4 Assignment

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 18 / 19

Page 55: LR Parsing - The Algorithm - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/L… · LR Parsing - The Algorithm Lecture 13 Sections 4.5, 4.7 Robb T

Assignment

HomeworkLet the grammar be

S → ( L ) | idL→ L , S | S

Parse the expression (id,id).

Robb T. Koether (Hampden-Sydney College) LR Parsing - The Algorithm Wed, Feb 18, 2015 19 / 19