This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 47: bc

You should be warmed up and ready to work on this new project. I'm usually assuming you would do these projects over one or two days as 2-3 hour sessions, but you can typically take as long as you need to implement them as much as you can.

This project is all about using what you learned in Part V to create the language for the bc program. We already implemented the simple math for bc in Exercise 36 but now you're going to implement as much of the bc language as you can. There are a large number of operators for bc as well as functions and control structures. Your goal is to implement this in stages using what you learned about Recursive Descent Parsers.

I would focus heavily on building your parser in stages starting with scanning, then parsing, then analysis, and using sample bc code to test it. This project could be massive since you're implementing a language by hand, but complete as much of the syntax as you can.

Exercise Challenge

The bc language has the capability to do more than just process math. I never use more than just the basic math, but the full language is rather powerful. You have the capabilities to define functions, use if-statements, and implement many other common programming constructs. In your implementation you will not be able to implement the entire bc language as it is simply too large. Instead, you should implement just the following:

  1. All the math operators.
  2. Variables.
  3. Functions.
  4. if-statements.

This is actually the order you should probably implement the language. First, get the operators working and parsing just fine. Feel free to steal the minimalist implementation you created in Exercise 35 to get started. Once you have that, implement variables, which will require you to make your analyzer properly handle the storing and retrieving of the variables. Finally, you can implement functions and then if-statements.

You'll want to dig up any documentation on the GNU version of bc as that will have a nice complete description of the language so you can implement it. There's nothing magical about it since they mostly copied everything from C, and many other languages are similar to it.

As you work on this challenge take your time and do it in steps. The beauty of implementing a language is you can actually do it in a logical clean order going from scanning, to parsing, to analysis without much bouncing around between the three phases.

Finally, remember that you are implementing a Recursive Descent Parser, and honestly just a baby version of real big time Computer Science parsing. If you are looking to do serious parsing work, then please use a parser generator rather than writing one by hand. Writing them by hand is merely a fun challenge and a way to learn how a parser logically structures the processing of text.

Study Drills

  1. To study the bc language you should grab the source code to it from gnu.org and look for the files bc.y, sbc.y and scan.l. It may be confusing, so go research a tool called lex and one called yacc.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.


Pre-Order From Zed

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.