Studio 2
Getting Started
Update your repository and locate the studios/2 directory. Import this directory into Eclipse.Open report.txt in eclipse, fill in your team members' names, and Browse this studio document to get an idea of the work you will do in this session.
Warm Up:
Run the Main class as a Java Application This should produce some output in the console window. Look at the output, and look at the Example class given in Main.java Look at the method getTestFiles() and see how it supplies the testfiles used for this grammar's parse. Look at the methods in this class (S() and B()) and see how they accurately reflect the grammar. Look at how the terminal symbol a is referenced as sym.a in the code. Open sym class to see where those symbols appear. That file is automatically generated by the compiler tools, so you will find it in the autogen package. Look at the use of int peek(), void expect(char), and void oops(String) are used in the code.If you have time, glance at the build.xml file. Discuss how it controls the build process you witnessed.
Problems
You will consider a sequence of grammars and use them to create corresponding recursive-descent parsers. All of your work should be done in Main.java, but you are welcome to create more test files if you wish.For each rule of each grammar, you are asked which symbols predict that particular rule. Recall that the predict sets are computed primarily from First sets, but if a rule's right-hand side can derive the empty string, then the Follow set of the rule's left-hand side must also be included.
All of your work is given and can be done in the Main.java file. The information here is supplemental.
Before implementing any of these parsers, check your Predict sets with a neighboring group or the professor or TA.
Part 1
Complete the recursive-descent parsers for the grammars below. You will need to determine the First, Follow, and Predict sets for each grammar. Include these sets in the comments so that you can refer to them as you go. Be sure to use rule-specific Predict sets based on the First and Follow sets.1. MatchingParens A grammar that generates matching parentheses with an x in the middle. You should be able to write a recursive-descent parser for this grammar without changing the grammar at all.
2. Problem1a
3. Problem1d
Part 2
Continue with the following grammars:1. Declarations This is a grammar that generates declaration statements for a list of variables. Take a look at the test files to get an idea of the language. To find out the names of the terminal symbols, look in the sym.java file. For example, the keyword "int" is represented by sym.INT.
This grammar has left recursiion, which you must eliminate using the principled techniques for doing so.
2. IfThenElseFi This grammar generates if-then-else-fi statements. The "fi" terminal always ends and brackets the "if". The grammar has problems you must address before you can write a recursive-descent parser.
Part 3
1. Expressions There is just one grammar in this part -- an expression grammar with left recursion.* I already devoped a grammar suitable for Recursive Descent parsing.
* I implemented the methods for the parser.
* Your job: use parameter values and return values so that the numerical computation is carried out by the parse. Do not use any global varibles to do this, or the techinque will fail when you do the lab!