$25
For this assignment, we will be wri7ng a parser that is able to parse an input file wri:en in our “Tiny” grammar. You should finish wri7ng the “Parser” class that I’ve provided. Your Parser class should print:
• Each 7me it enters or leaves a rule and what the rule is:
Entering [RULE_NAME] Rule
Exi-ng [RULE_NAME] Rule
There are two excep7ons: the ID and the INT rules. In this grammar, ID and INT are also Token names, so we will not print when we are “entering” or “exi7ng” those rules since we can just check the token type.
• Each 7me it recognizes a token and what the token was:
Found [TOKEN_TYPE] Token: [LEXEME]
For rules with Epsilon defini7ons, you should also indicate if that defini7on was chosen: Did not find [TOKEN_TYPE] or [TOKEN_TYPE] Token, choosing EPSILON produc-on
• Something, every7me it catches an error: Expected [TOKEN_TYPE] found [LEXEME]
When the error could have been mul7ple things, separate the token types with the word or Expected [TOKEN_TYPE] or [TOKEN_TYPE] or [TOKEN_TYPE] found [LEXEME]
• How many parse errors were found There were [X] parse errors found.
Parser.rb extends Scanner.rb (The lexer that you wrote for your last assignment) and provides a framework for a topdown, recursive-descent parser of the TINY language. The parser stays one token ahead in the Token stream
(@lookahead) and uses the Token to predict how to con7nue parsing the current instruc7on and which method to call next.
The consume() method calls nextToken() in the scanner. The current @lookahead Token is discarded, and the next Token in the stream is retrieved. Whitespace Tokens are discarded.
The match(dtype) method tries to match the @lookahead Token with the provided type (dtype). If a match is found, consume() is called to retrieve the next Token. Otherwise an error message is displayed and then consume() is called to retrieve the next token.
The program() method is first called to parse a TINY program. Since a TINY program consists of a sequence of statements, program() calls statement() repeatedly un7l it encounters the EOF token.
Complete the parser by providing methods for the appropriate BNF rules in TINY.
I have given you my lexer and token ruby classes. I have also par7ally wri:en the parser for you and have wri:en a main.rb file that can run your ruby parser. Your assignment is to FINISH WRITING THE PARSER (parser.rb). I have also included 5 sample input files that you can use to test your program once you’ve finished wri7ng it. input[1-3].txt should complete with no parse errors. input[4-5].txt should have parse errors.
Below are screenshots of what your output should look like, based on the input files I’ve provided.