example of shift-reduce parsing - computer science

21
Example of Shift-Reduce Parsing

Upload: others

Post on 27-Jan-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Example of Shift-Reduce Parsing - Computer Science

Example ofShift-Reduce Parsing

Page 2: Example of Shift-Reduce Parsing - Computer Science

A SUCCESSFUL PARSE

Page 3: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 4: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 5: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 6: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 7: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

NOTE: Reduction by t → e is also possible here

Page 8: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $ Shift

$ t * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 9: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $ Shift

$ t * x $ Reduce by f → x

$ t * f $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 10: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $ Shift

$ t * x $ Reduce by f → x

$ t * f $ Reduce by t → t * f

$ t $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 11: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $ Shift

$ t * x $ Reduce by f → x

$ t * f $ Reduce by t → t * f

$ t $ Reduce by t → e

$ e $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 12: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Shift

$ t * x $ Shift

$ t * x $ Reduce by f → x

$ t * f $ Reduce by t → t * f

$ t $ Reduce by t → e

$ e $ Accept

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 13: Example of Shift-Reduce Parsing - Computer Science

A UNSUCCESSFUL PARSE

Page 14: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

NOTE: This time reduce by t → e

Page 15: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 16: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 17: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $ Shift

$ e * x $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 18: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $ Shift

$ e * x $ Reduce by f → x

$ e * f $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 19: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $ Shift

$ e * x $ Reduce by f → x

$ e * f $ Reduce by t → f

$ e * t $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 20: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $ Shift

$ e * x $ Reduce by f → x

$ e * f $ Reduce by t → f

$ e * t $ Reduce by e → t

$ e * e $

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps:

Page 21: Example of Shift-Reduce Parsing - Computer Science

Grammar:

Stack Input Action

$ x * x $ Shift

$ x * x $ Reduce by f → x

$ f * x $ Reduce by t → f

$ t * x $ Reduce by t → e

$ e * x $ Shift

$ e * x $ Shift

$ e * x $ Reduce by f → x

$ e * f $ Reduce by t → f

$ e * t $ Reduce by e → t

$ e * e $ Fail

e → e + t| t

t → t * f| f

f → ( e )| x

Parsing steps: