Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Example ofShift-Reduce Parsing
A SUCCESSFUL PARSE
Grammar:
Stack Input Action
$ x * x $e → e + t| t
t → t * f| f
f → ( e )| x
Parsing steps:
Grammar:
Stack Input Action
$ x * x $ Shift
$ x * x $
e → e + t| t
t → t * f| f
f → ( e )| x
Parsing steps:
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:
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:
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
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:
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:
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:
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:
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:
A UNSUCCESSFUL PARSE
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
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:
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:
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:
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:
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:
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:
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: