Jump to content
  • Advertisement
Sign in to follow this  
GameDev Doctor

Grammar!

This topic is 3970 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!