• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encode FIRST and FOLLOW sets into a recursive descent parser  RSS feed

Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: I have also posted this question here - http://stackoverflow.com/questions/9761267/encode-first-and-follow-sets-into-a-recursive-descent-parser

Hi everyone,

I am implementing the Syntax Analysis phase of my compiler by writing a recursive descent parser. I need to be able to take advantage of the FIRST and FOLLOW sets so I can handle errors in the syntax of the source program more efficiently. I have already calculated the FIRST and FOLLOW for all of my non-terminals, but am have trouble deciding where to logically place them in my program and what the best data-structure would be to do so.

Note: all code will be pseudo code

Option 1) Use a map, and map all non-terminals by their name to two Sets that contains their FIRST and FOLLOW sets:

This seems like a viable option, but inside of my parser I would then need sorta ugly code like this to retrieve the FIRST and FOLLOW and pass to error function:

Option 2) Create a class for each non-terminal and have a FIRST and FOLLOW property:

this leads to code that looks a little nicer:

These are the two options I thought up, I would love to hear any other suggestions for ways to encode these two sets, and also any critiques and additions to the two ways I posted would be helpful.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!