Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Write a recursive descent parser for the Snowflake language. The program should use the lexical scanner you wrote for an earlier assignment. When combined

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
C++
Write a recursive descent parser for the Snowflake language. The program should use the lexical scanner you wrote for an earlier assignment. When combined with the lexical scanner, your parser should be able to read the source code for a Snowflake program and indicate if the syntax is correct If the there is a syntax error, the parser must identify (as best as possible) the row and column where the error occurs and the nature of the syntax error. Your parser need only display an error message for the first error detected The Snowflake language can be defined by the following BNF: SF parmstmt varlist varclist ret pattern parmstmt code ret parm varlist; var var varlist varconst varconst varclist return var; varconst varconst pattern note: vertical bar is part of snowflake line line code assign | loop var = varclist; var pattem= varclist; while varconst pattern (code } code line assign loop You may modify the BNF, as you need, as long as you parse an equivalent language. If your Snowflake lexical scanner does not work perfectly, you may use the Snowflake scanner provided by your instructor. * Lexical scanner suitable for the Snowflake language. public class Scanner { . /* Input symbol group indexes */ private static final int SYMLETTER = // upper and lowercase letters private static final int SYMNUM // number private static final int SYMSPACE // space or tab private static final int SYMHASH // # hash mark private static final int SYMBANG = 4; // ! exclamation point private static final int SYMEOL = 5; // End Of Line private static final int SYMQUOTE = 6; 1 quote character private static final int SYMPUNC = 7; // punctuation private static final int SYMOTHER = 8; // none of the above private static final int(0) STATETABLE = /* state 0 1 2 3 5 6 */ /* SYMLETTER */ { 0, 2, 2, 3, 4, /* SYMNUM */ { 0, -1, 2, 3, 4, 4, /* SYMSPACE */{ 0, 1, 1, 3, 4, 4, /* SYMHASH */ { 0, 3, 3, 3, /* SYMBANG */ { 0, -1, -1, 4, SYMEOL */ { 1, 1, 1, 1, /* SYMQUOTE */ { 0, 6, 6, /* SYMPUNC */ { 0, 1, 1, 3, 4, 4, ** SYMOTHER */ { 0, -1, -1, 3, 4, 4, 4 /* Action values */ private static final int ACTNOTHING = // take no action private static final int ACTCREATE = 1; // create token with input symbol private static final int ACTADD EI // append character to symbol text private static final int ACTQUOTE E 3; // create empty string token 4 private static final int[] [] ACTIONTABLE /* state 0 1 2 3 5 6 */ /* SYMLETTER */{0, 1, 2, 0, 0, /* SYMNUM */ { 0, 0, 2, 0, 0, 0, /* SYMSPACE * { 0, 0, 0, 0, 0, 0, /* SYMHASH */ { 0, 0, 0, 0, 0, 0, * SYMBANG */ { 0, 0, 0, 0, 0, 0, SYMEOL */ { 0, 0, 0, /* SYMQUOTE */ { 0, 3, 3, /* SYMPUNC */ { 0, 1, 1, 0, 0, 0, /* SYMOTHER */ { 0, , 0, 0, 0, 0, * Read the input file and create a list of tokens after removing the comments * and whitespace. * @param inFile Source file of the program. * @return List of token objects in the order in which they appear in the * source code. * @throws java.text.ParseException Thrown when an invalid character is found, * @throws java.io.IOException Thrown by reading input file. public static java.util.ArrayList lexicalScan (java.io.Buffered Reader inFile), throws java.text.ParseException, java.io.IOException int 1; char symbol; int intSymbol; as an int int action; taken int state = // current state // input symbol // input symbol // action to be inx; // input symbol index int lineNumber = // current line number in source code int source int column = source codomien column current column in source code Token tok = null; // newly created token java.util.ArrayList tokenlist = new java.util.ArrayList (); null; jau tok = // new yorkers to intSymbol = inFile.read()); while (intSymbol != -1) { //while not EOF symbol = (char) intSymbol; // convert input to character if (symbol == ' ') // if end of line lineNumber++; column = 0; } else { column++; if (symbol != ' ') // ignore carriage return /* Map input symbol to symbol characters group. */ (Character.isLetter(symbol)) { inx = SYMLETTER; } else if (Character.isDigit(symbol)) { inx = SYMNUM; } else if (symbol == ''Il symbol == '\t') { // space is a space or tab inx = SYMSPACE; } else if (symbol == '#') { inx = SYMHASH; } else if (symbol == '!') { inx = SYMBANG; } else if (symbol == ' ') { inx = SYMEOL; } else if (symbol == "") { inx = SYMQUOTE; } else if ("=1 {};".indexOf(symbol) != -1) { inx = SYMPUNC; } else { inx = SYMOTHER; action = ACTIONTABLE (inx] (state); state = STATETABLE(inx] (state); /* Check for syntax error. */ if (state == -1) // if invalid state throw new java.text.ParseException("Invalid symbol:"+symbol, lineNumber*1000+Column); /* Perform any action for this transition */ switch(action) { case ACTCREATE: // create a token with the current symbol tok = new Token (symbol, lineNumber, column); tokenList.add(tok); break; case ACTADD: // add symbol to token tok.add2Token (symbol); break; case ACTQUOTE: // create empty token for string tok = new Token ( lineNumber, column); tokenList.add(tok); break; inpiter read (17" int Symbol = inFile.read(); I read next token from input file return tokenList; // return list of tokens Write a recursive descent parser for the Snowflake language. The program should use the lexical scanner you wrote for an earlier assignment. When combined with the lexical scanner, your parser should be able to read the source code for a Snowflake program and indicate if the syntax is correct If the there is a syntax error, the parser must identify (as best as possible) the row and column where the error occurs and the nature of the syntax error. Your parser need only display an error message for the first error detected The Snowflake language can be defined by the following BNF: SF parmstmt varlist varclist ret pattern parmstmt code ret parm varlist; var var varlist varconst varconst varclist return var; varconst varconst pattern note: vertical bar is part of snowflake line line code assign | loop var = varclist; var pattem= varclist; while varconst pattern (code } code line assign loop You may modify the BNF, as you need, as long as you parse an equivalent language. If your Snowflake lexical scanner does not work perfectly, you may use the Snowflake scanner provided by your instructor. * Lexical scanner suitable for the Snowflake language. public class Scanner { . /* Input symbol group indexes */ private static final int SYMLETTER = // upper and lowercase letters private static final int SYMNUM // number private static final int SYMSPACE // space or tab private static final int SYMHASH // # hash mark private static final int SYMBANG = 4; // ! exclamation point private static final int SYMEOL = 5; // End Of Line private static final int SYMQUOTE = 6; 1 quote character private static final int SYMPUNC = 7; // punctuation private static final int SYMOTHER = 8; // none of the above private static final int(0) STATETABLE = /* state 0 1 2 3 5 6 */ /* SYMLETTER */ { 0, 2, 2, 3, 4, /* SYMNUM */ { 0, -1, 2, 3, 4, 4, /* SYMSPACE */{ 0, 1, 1, 3, 4, 4, /* SYMHASH */ { 0, 3, 3, 3, /* SYMBANG */ { 0, -1, -1, 4, SYMEOL */ { 1, 1, 1, 1, /* SYMQUOTE */ { 0, 6, 6, /* SYMPUNC */ { 0, 1, 1, 3, 4, 4, ** SYMOTHER */ { 0, -1, -1, 3, 4, 4, 4 /* Action values */ private static final int ACTNOTHING = // take no action private static final int ACTCREATE = 1; // create token with input symbol private static final int ACTADD EI // append character to symbol text private static final int ACTQUOTE E 3; // create empty string token 4 private static final int[] [] ACTIONTABLE /* state 0 1 2 3 5 6 */ /* SYMLETTER */{0, 1, 2, 0, 0, /* SYMNUM */ { 0, 0, 2, 0, 0, 0, /* SYMSPACE * { 0, 0, 0, 0, 0, 0, /* SYMHASH */ { 0, 0, 0, 0, 0, 0, * SYMBANG */ { 0, 0, 0, 0, 0, 0, SYMEOL */ { 0, 0, 0, /* SYMQUOTE */ { 0, 3, 3, /* SYMPUNC */ { 0, 1, 1, 0, 0, 0, /* SYMOTHER */ { 0, , 0, 0, 0, 0, * Read the input file and create a list of tokens after removing the comments * and whitespace. * @param inFile Source file of the program. * @return List of token objects in the order in which they appear in the * source code. * @throws java.text.ParseException Thrown when an invalid character is found, * @throws java.io.IOException Thrown by reading input file. public static java.util.ArrayList lexicalScan (java.io.Buffered Reader inFile), throws java.text.ParseException, java.io.IOException int 1; char symbol; int intSymbol; as an int int action; taken int state = // current state // input symbol // input symbol // action to be inx; // input symbol index int lineNumber = // current line number in source code int source int column = source codomien column current column in source code Token tok = null; // newly created token java.util.ArrayList tokenlist = new java.util.ArrayList (); null; jau tok = // new yorkers to intSymbol = inFile.read()); while (intSymbol != -1) { //while not EOF symbol = (char) intSymbol; // convert input to character if (symbol == ' ') // if end of line lineNumber++; column = 0; } else { column++; if (symbol != ' ') // ignore carriage return /* Map input symbol to symbol characters group. */ (Character.isLetter(symbol)) { inx = SYMLETTER; } else if (Character.isDigit(symbol)) { inx = SYMNUM; } else if (symbol == ''Il symbol == '\t') { // space is a space or tab inx = SYMSPACE; } else if (symbol == '#') { inx = SYMHASH; } else if (symbol == '!') { inx = SYMBANG; } else if (symbol == ' ') { inx = SYMEOL; } else if (symbol == "") { inx = SYMQUOTE; } else if ("=1 {};".indexOf(symbol) != -1) { inx = SYMPUNC; } else { inx = SYMOTHER; action = ACTIONTABLE (inx] (state); state = STATETABLE(inx] (state); /* Check for syntax error. */ if (state == -1) // if invalid state throw new java.text.ParseException("Invalid symbol:"+symbol, lineNumber*1000+Column); /* Perform any action for this transition */ switch(action) { case ACTCREATE: // create a token with the current symbol tok = new Token (symbol, lineNumber, column); tokenList.add(tok); break; case ACTADD: // add symbol to token tok.add2Token (symbol); break; case ACTQUOTE: // create empty token for string tok = new Token ( lineNumber, column); tokenList.add(tok); break; inpiter read (17" int Symbol = inFile.read(); I read next token from input file return tokenList; // return list of tokens

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: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

Sketch the following sets of points. {(r, 0): 1

Answered: 1 week ago

Question

Exude confidence, not arrogance.

Answered: 1 week ago

Question

2. What is the meaning and definition of Banking?

Answered: 1 week ago

Question

3.What are the Importance / Role of Bank in Business?

Answered: 1 week ago