Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi please write a recursive-descent parser for Jack class definitions using java language. The grammar to which valid programs must conform is given below. you

Hi please write a recursive-descent parser for Jack class definitions using java language. The grammar to which valid programs must conform is given below. you are required to complete this with the parsing process
Below is the grammer: image text in transcribed image text in transcribed
the order is implementation of the parser.java class is shown below
image text in transcribed
the parser class has a starting code shown below please use this code
image text in transcribed
image text in transcribed
Grammar Here is the grammar of the Jack language. Note that this has been expressed in a slightly different form from that in the book/slides but it describes the same language. You must structure your parser according to this grammar. . The notation : := means 'is defined as'. A semicolon marks the end of each non-terminal 'rule'. Parentheses group terminal and non-terminal symbols as a single item. A ? means the preceding item is optional. A* means the preceding item occurs zero or more times. A separates alternatives. Items all in upper-case are lexical tokens/keywords. Items enclosed in single quotes are SYMBOL tokens. . . class ::= CLASS IDENTIFIER 'f' classVarDec* subroutineDec* ''; classVarDec ::= ( STATIC FIELD ) type varList ';'; type ::- INT | CHAR | BOOLEAN IDENTIFIER ; varList ::= IDENTIFIER ( ',' varList ) ? ; subroutineDec ::= routinekind ( VOID | type ) IDENTIFIER '('parameterList ?')' subroutineBody ; routineKind ::= CONSTRUCTOR FUNCTION | METHOD; parameterList ::- type IDENTIFER (,' parameterlist ) ? ; subroutineBody :: '' varDec statement. '', var Dec ::- VAR type varList';'; block ::- 'I' statement. '); statement ::- dostatement ! if Statement ! letStatement ! returnStatement ! whileStatement ; dostatement ::- DO subroutineCall ';'; ifStatement ::- IF '('expression ')' block ( ELSE block) ? ; letStatement ::- LET IDENTIFIER ( ['expression '1') ? ' expression ';'; returnStatement ::- RETURN expression ? ';'; whileStatement ::- WHILE 'C' expression ')' block; expression ::- term ( binaryOp expression ) ? ; binaryOp ::- + "G" 1 term ::- INT_CONSTI STRING_CONSTI keywordConstant IDENTIFIER (l'expression '1') ? | subroutineCall | '('expression) unaryOp term ; keywordConstant != TRUE FALSE | NULL | THIS ; subroutineCall :: subroutineReference l'expressionist?)' subroutine Reference - IDENTIFIER ('.' IDENTIFIER) ? ; expressionList ::expression (','expressionList ) ? ; unaryop :: Suggested order of implementation 1. Parse a class with no variables or subroutines. In other words, just the header and curly brackets. 2. Parse a'classVarDec but you might like to just handle a single IDENTIFIER in parsing the varlist part. 3. Handle the full version of varList. Note that this is a recursive non-terminal and there are several non-terminal rules with a similar form. When you have successfully written the parsing method for this non-terminal, you should find writing those for other recursive ones straightforward. 4. Make a start on subroutineDec but you could skip parsing parameterList and for statementBody you could just check for the pair of curly brackets; i.e., ignore var Dec and statement. 5. Add var Dec. This should be straightforward as you already have the varlist part. 6. Make a start on statement. Keep this simple at first and just parse a returnStatement without an expression. 7. Add dostatement. This requires the parsing of a subroutineCall. Once again, keep this simple and ignore parsing expressionList. 8. Gradually build up further elements. At some point you will need to parse the term part of expression. As noted in the book/slides and lectures, some extra 'lookahead' is required when an IDENTIFIER is found so that will require some extra thought but you should still be able to parse simpler versions of term until you are ready to handle the full version 9. For the challenge part, you will need to create a simple 'symbol table' that holds information on variable identifiers. import tokenizer.Tokenizer; * Parse a Jack source file. * * Cauthor * @version */ public class Parser { // The tokenizer. private final Tokenizer lex; * Parse a Jack source file. * @param lex The tokenizeri */ public Parser(Tokenizer lex) { this. lex = lex; } * Parse a Jack class file. * @throws ParsingFailure on failure. */ public void parseClass() { throw new ParsingFailure(); } ** * A ParsingFailure exception is thrown on any form of * error detected during the parse. */ public static class Parsintallurg extends RuntimeException { 1.Parse a class with no variables or subroutines(only header and curly brackets.) 2.Parse a classVarDec by handling just a single IDENTIFIER in parsing the varList part 3.Handle the full version of varList. Note that this is a recursive non-terminal and there are several non-terminal rules with a similar form. When you have successfully written the parsing method for this non-terminal, you should find writing those for other recursive ones straightforward. 4. Make a start on subroutineDec (skip parsing parameterList and for statementBody. only check for the pair of curly brackets; i.e., ignore varDec and statement. 4.Add varDec. 5. Make a start on statement. Keep this simple at first and just parse a returnStatement without an expression. 6.Add doStatement. This requires the parsing of a subroutineCall

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

EXTRA CREDIT DISCUSSION: Sentencing & Corrections...

Answered: 1 week ago