Question: please write a recursive-decent parser for jack class definations the grammer of the jack language is below please write it in java a parser class



. . . . 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 '' classVarDec* subroutineDec* '); classVarDec ::= (STATICI FIELD ) type varList ';' ; type ::= INT I CHAR | BOOLEAN IDENTIFIER ; varList ::= IDENTIFIER (',' varList ) ? ; subroutineDec ::= routineKind ( VOID I type) IDENTIFIER '('parameterList ? ')' subroutineBody; routinekind ::= CONSTRUCTOR | FUNCTION | METHOD ; parameterList ::- type IDENTIFER (',' parameterList ) ? ; subroutineBody ::= '1' varDec* statement* ''; varDec ::- VAR type varList ';'; block ::= '/' statement. '''; statement ::= dostatement ! ifStatement letStatement ! returnStatement ! whileStatement ; dostatement ::- DO subroutineCall ';'; ifStatement ::- IF ('expression)' block ( ELSE block) ?; Ietstatement ::- LET IDENTIFIER ('L'expression 'l' ) ? ' expression ';'; returnStatement ::- RETURN expression ? ";'; whileStatement ::- WHILE 'C' expression '' block; expression ::- term ( binaryop expression ) ? ; binaryOp ::- term ::- INT_CONST STRING_CONST keywordConstant IDENTIFIER ('l'expression '1') ? subroutineCall I ('expression!! unaryop term keywordConstant ::- TRUE FALSE | NULL | THIS ; subroutineCall :: subroutine Reference ('expressionList ?)'; subroutineReference ::= 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 varDec 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. 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 {
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
