This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Encode FIRST and FOLLOW sets into a recursive descent parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encode FIRST and FOLLOW sets into a recursive descent parser" Watch "Encode FIRST and FOLLOW sets into a recursive descent parser" New topic
Author

Encode FIRST and FOLLOW sets into a recursive descent parser

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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.
Thanks


"If the facts don't fit the theory, get new facts" --Albert Einstein
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encode FIRST and FOLLOW sets into a recursive descent parser
 
Similar Threads
2 Dimmensional Array and asList
Recursive Descent Parser Question
Rails uptake time?
Executing calculation of an expression within a string
Java is lexical analysis