Example of Shift-Reduce Parsing - Computer Science
Transcript of Example of Shift-Reduce Parsing - Computer Science
![Page 1: Example of Shift-Reduce Parsing - Computer Science](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/1.jpg)
Example ofShift-Reduce Parsing
![Page 2: Example of Shift-Reduce Parsing - Computer Science](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/2.jpg)
A SUCCESSFUL PARSE
![Page 3: Example of Shift-Reduce Parsing - Computer Science](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/4.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/5.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/6.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/7.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/8.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/9.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/11.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/12.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/13.jpg)
A UNSUCCESSFUL PARSE
![Page 14: Example of Shift-Reduce Parsing - Computer Science](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/14.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/15.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/16.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/18.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/19.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/20.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022020623/61f1b5220799954a46440323/html5/thumbnails/21.jpg)
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: