Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
COMP 3002 Winter 2025 Assignment #5
Creating a Constructor
Note: I converted the Smalltalk code for building first and follow sets into Swift and put it into the Grammar class I provided (it’s a subset so take what’s new). I also implemented the Production code that you will need… It has been compiled and has not been tested but hopefully it will work. Finally, there are a handful of semantic actions for the sponsor (the sponsor of a parser or scanner is the builder that uses it); namely the constructor. They mostly do nothing but they still need to be there… I couldn’t test them at all…
In the Smalltalk version, I added ordered collection methods (which for you are array methods) that can be used to add items and have it be known that additions were made… These are described in Smalltalk on slide 30 of “#10FirstAndFollowSets”. I wrote the Swift equivalent to this code called addIfAbsentAdded and addAllIfAbsentAdded and also made it available. They return true if anything was added and false if nothing was added. You don’t need to use these methods because Swift already has a mechanism for doing this called ifAdded… So you will need to make small changes if you wish to do it the direct Swift way.
Goal of the assignment: You will add code to build a grammar in 2 passes, one pass to pick up the nonterminals and one pass to process the grammar from scratch where you record the macros and productions in full detail. When you are done building a grammar, you can force the building of first and follow sets by saying “finalize ()” to your grammar object. It would be nice for debugging if you could output the grammars processed along with the first and follow sets and whether it is e-generating or not. There is code for doing that.
The new translator to be called Constructor
Create a new class called “Constructor” that contains everything the “FiniteStateMachineBuilder” contained. If your previous assignment was running example1 and example2 to able to open the two input files “parserFSMs.txt” and “scannerFSM.txt”, do these same for the sample grammar files in the SampleGrammars folder (but you will now need a handful of example methods instead of one). If the example methods had the contents of the two files in one long string, do the same here.
The “Constructor” is meant to create a grammar from a series of productions. I called it Constructor rather than GrammarBuilder because you will keep adding to it in subsequent assignments until you output your own tables. So it will be a scanner/parser constructor by then…
Start with toyLispGrammar until it works then go on to other examples. The last grammar to use should be toyParserGrammarToTestFollowSets. It’s the only one important one that fully tests everything about First and Follow set computation.
What I did to help you out
I provided the tables for the scanner and parser grammars (extract just the Swift version) and I also added the grammars themselves in case you wondered what I generated the tables from. Add it to the constructor too so you have a permanent record.
More details about this builder…
It will need to run two passes. Pass1 to pick up all the nonterminals and then pass2 to build the grammar.
For pass1, you can add a new recursive routine, say firstPassWalkTree: which is used to recursively pick up the nonterminals (it’s described in slide 13 of #09GrammarBuilders) and add them to the constructor (basically, looking only for walkLeftPartWithLookahead and walkLeftPart where the nonterminal is the left child; pick up the symbol, not the token). No other processing needs to be done.
For pass2, you will encounter the missing walkGrammar routine which you will add followed by any other missing walkXXX routines as you process the grammar from left to right. Many of those routines have already been written by you. So new routines are mostly for picking up and storing macros and productions in the grammar…
This assignment is mostly about implementing pass1 and pass2 even though I have implemented the routines that compute first and follow sets. I did it to save an assignment… Test this portion by saying “finalize ()” to the grammar.