Rewrites the parser stack reduction logic.

Review by @densh.