Question
In C#, implement the Shift-Reduce Parser for the following grammar, and please provide comments. Use the Shift-Reduce table that is provided below. Shift-Reduce table The
In C#, implement the Shift-Reduce Parser for the following grammar, and please provide comments.
Use the Shift-Reduce table that is provided below.
Shift-Reduce table
The main should display the token sequence, the full sequence of shift reduce steps, and the stack's different states as shown below.
Output:
Enter your expression: num1 + num2 * 3
-----------------------------------
Calling Lexer:
-----------------------------------
num1: id
+: +
num2: id
* : *
3: id
----------------------------------
Parsing Steps
----------------------------------
S5
R6
R4
R2
S6
S5
R6
R4
.
.
.
ACCEPT
--------------------------------
Stack
----------------------------------
0
0id5
0F3
0T2
0E1
0E1+6
0E1+6id5
0E1 + 6F3
0E1 + 6T9
0E1 + 6T9 * 7
0E1 + 6T9 * 7id5
0E1 + 6T9 * 7F10
0E1 + 6T9
0E1
---------------------------------------------------------------------------------------
/*Below is segment of the code I have written in C# that currently works for the Lexer, but still needs to work on both
the parser and the stack. */
using System; using System.Collections.Generic;
public class SRP { public static void Main(string[] args) { Console.Write("Enter your expression: "); string input = Console.ReadLine(); input = input + ";"; Lexer l = new Lexer(input); //Parser par = new Parser(l.tk);
Console.ReadKey(); } }
public class Lexer { char[] ch; char NxtCh; // Use to traverse through the line of code. string lx; // holds lookup from void lookup List
// Remodify this if statement to a while loop // NOTE: This was originally an if statement while (NxtCh != ';') { // ID NOTE: was originally a while loop if (Char.IsLetter(NxtCh)) { while (Char.IsLetterOrDigit(NxtCh)) { lx = lx + NxtCh.ToString(); position++; NxtCh = ch[position]; } // while loop ends
tk.Add("id"); LxList.Add(lx); lx = ""; } // if statement determining if char is letter ends
// else if statement for detecting WHITESPACE else if (Char.IsWhiteSpace(NxtCh)) { position++; NxtCh = ch[position]; } //else if for WHITESPACE ends here
// else if statement for detecting INT_LIT else if (Char.IsDigit(NxtCh)) { while (Char.IsDigit(NxtCh)) { lx = lx + NxtCh.ToString(); position++; NxtCh = ch[position]; } //end of while loop
tk.Add("id"); LxList.Add(lx); lx = ""; } // else if statement for if char is digit ends
// else statement for UNKNOWN else { LookupTable(ch[position].ToString()); position++; NxtCh = ch[position]; } //end of else for unknown
} // end of while NxCh not null
tk.Add("EOF"); // line of code representing the "End of File" not being null }
for (int i = 0; i
Console.WriteLine("---------------------"); }
// Where the Lexer and Tokens show output // that depends on the characters typed by user. void LookupTable(string lex) { LxList.Add(lex); { if (lex == "+") { tk.Add("+"); }
else if (lex == "/") { tk.Add("/"); }
else if (lex == "(") { tk.Add("("); }
else if (lex == "*") { tk.Add("*"); }
else if (lex == ")") { tk.Add(")"); }
else if (lex == "-") { tk.Add("-"); } }
lx = ""; // lx string is cleared } }
ETTF (E id EETTFF Action Goto State id S5 S4 accept S5 8 2 3 R6 R6 9 3 10 S11 1 0 ETTF (E id EETTFF Action Goto State id S5 S4 accept S5 8 2 3 R6 R6 9 3 10 S11 1 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started