Algorithm for computing FIRST and FOLLOW sets for context-free grammars

2 8 picture 2 8 · Nov 8, 2013 · Viewed 16.9k times · Source

I need an algorithm to computing FIRST and FOLLOW sets for a grammar.

Is there a simple algorithm or simple code for computing these?

Answer

templatetypedef picture templatetypedef · Nov 8, 2013

The standard algorithm for computing FIRST and FOLLOW sets is discussed in most compiler textbooks and books on parsing algorithms. I would be surprised if you were taking a course where this was covered and had no assigned reading or materials provided on this topic.

I taught a class on compiler construction and have some lecture slides that might be useful here. You might want to check out slides 303-304 of these slides on LL parsing for an algorithm that computes FIRST sets and slide 32 of these slides on LL parsing for an algorithm that computes FOLLOW sets.

Hope this helps!