Sign in to follow this  

Grammar!

This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I this exercise in Compilers: Principles, Techniques, and Tools: Construct an unambiguous context-free grammar for arithmetic expressions in postfix notation. Quick answer: expr := expr expr op | number op := + | - | * | / Afterwards, though, I attempted to get rid of left recursion, and got completely stuck. It seems to make it very difficult that the first number can be used in a subexpression. Is there a remotely simple way, or am I just blanking on this?

Share this post


Link to post
Share on other sites
There are multiple simple ways. The main important bit to realize is that you'll always have number on the left, and it won't be the terminal case, either. Not always, anyways. There will still be right recursion of course.


boost::spirit's FAQ also describes eliminating left recursion for a rather generalized case -- if you want to figure it out without me downright giving you the answer(s). Theirs also eliminates RHS recursion, however, something I doubt you need to do, and may or may not want to do. The multiple ways I'm thinking of didn't involve removing RHS recursion.

Share this post


Link to post
Share on other sites
Quote:
Original post by GameDev Doctor
Thanks, I managed to come up with

expr := part part op
part := term part op | term

Which seems to work for the most part.


We can simplify this a slight bit to one of the following:

expr := term expr op | term
expr := term (expr op)?

Which were the ones I were thinking of :).

Share this post


Link to post
Share on other sites

This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this