Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C#, implement the Recursive Descent Parser (RDP) for the Grammar Details: 1. Create a class called Lexer that takes an input expression as a

In C#, implement the Recursive Descent Parser (RDP) for the Grammar

image text in transcribed

Details:

1. Create a class called Lexer that takes an input expression as a string and outputs its token stream.

2. The Lexer needs to catch any unidentified characters that don't belong to the grammar.

3. The RCP should then proceed to parse the input expression.

4. Please provide comments to explain what you have done with each line of code, and how the code compiles.

The output should appear as shown below.

Enter your expression:

num1 + num2 / (7 + total)

-----------------------------------

Calling Lexer:

num1: IDENT

+: ADD_OP

num2: IDENT

/: DIV_OP

(: LEFT_PAREN

7: INT_LIT

+: ADD_OP

total: IDENT

): RIGHT_PAREN

----------------------------------

Calling the Recursive Descent Parser:

NextToken: IDENT

Enter

Enter

Enter

NextToken: ADD_OP

Exit

Exit

NextToken: IDENT

Enter

Enter

NextToken: DIV_OP

Exit

NextToken: LEFT_PAREN

Enter

NextToken: INT_LIT

Enter

Enter

Enter

NextToken: ADD_OP

Exit

Exit

NextToken: IDENT

Enter

Enter

NextToken: RIGHT_PAREN

Exit

Exit

Exit

NextToken: EOF

Exit

Exit

Exit

Press any key to continue . . .

--------------------------

Here's my code in progress.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace RecursiveDescentParser {

class MainClass {

public static void Main(string[] args) { // Function expr parses strings in the language // Generated by the rule: -> { (+ | -) } void expr() { Console.WriteLine("Enter your expression: ");

// Parse the first term term();

while (nextToken == ADD_OP || nextToken == SUB_OP) { lex(); term(); }

Console.WriteLine("Exit "); }

void term() { Console.WriteLine("Enter ");

//Parse the first term factor();

// As long as the net token is * or /, call lex to get the // next token and parse the next factor

while (nextToken == MUL_OP || nextToken == DIV_OP) { lex(); factor(); }

Console.WriteLine("Exit "); }

void factor() { Console.WriteLine("Enter ");

//Determine which RHS

if (nextToken == ApplicationIdentity || nextToken == INT_LIT) lex(); //Get the next token

//If the RHS is (), call lex to pass over '(', call expr, and check for ')' else { if (nextToken == LEFT_PAREN){ lex(); expr();

if(nextToken == RIGHT_PAREN) { TypeLoadException(); }

else { error(); } } //End of if (next Token ==

else{ error(); }

}

Console.WriteLine("Exit "); } } }

term> ((* factor-) id l int-constant l ( ((* factor-) id l int-constant l (

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

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions

Question

Consistently develop management talent.

Answered: 1 week ago

Question

Create a refreshed and common vision and values across Europe.

Answered: 1 week ago

Question

Provide the best employee relations environment.

Answered: 1 week ago