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
Goal of the assignment: I implemented but did not test the building of first and follow sets. You will augment that by building a grammar where you record the macros and productions.
The new translator is called Constructor
I have created a translator called “Constructor.st” which 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…
If installing it replaced a version of some method by an older one, Smallalk does give you the ability to grab a previous version. For example, if you enhanced the Grammar method isPrintable: to take an arbitrary object rather than an integer (so it can say it’s not “printable as a character”), you will find it reverted back to the original code… Just select the method, get the pop-up menu, click on “Browse version”, find your enhanced version and click LOAD. You will now be back to where you were originally.
You will see that it contains a class method “promptForGrammar” to run it (where you will select one of the files from SampleGrammars. Start with toyLispGrammar until it works then go on to other examples. The last grammar to use should be toyScannerGrammar.
But before you go too far, incorporate some of the code from the last assignment into this new translator. In particular, copy all your walk routines that build fsms. Get Smalltalk to do the work… (avoid doing it by hand) by multi-selecting the walk routines and right button clicking to get the pop-up menu… Choose menu item “Copy”.
If you do attempt to run the constructor, the first UN-IMPLEMENTED walk routine will be walkGrammar:. Don’t implement it yet because this translator needs to run 2 passes…
What I did to help you out
I have already added the tables for the scanner and parser grammars and I also added the grammars themselves in case you wonder what I generated the tables from (they use EndOfFile instead of '-|').
I added method finalize that computes e-generating nonterminals, first sets, and follows sets.
You will note that I also added additional methods to your class Grammar and a class Production to speed up the implementation. Feel free to change it. To test it, process the grammar called “toyParserGrammarToTestFollowSets.txt” and just finalize the grammar before you return it. You may have to debug it a little bit (hopefully not).
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 #08Grammars) and add them to the grammar (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 missing walkGrammar: which you will add followed by any other missing walkXXX: routines as you process the grammar. 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…