Question: JAVA PROGRAM OR C++ suggested psudocode // Test Context-Free Grammar Class import java.io.*; public class TestCFG { public static void main(String[] args) { // Language:

JAVA PROGRAM OR C++

JAVA PROGRAM OR C++ suggested psudocode // Test Context-Free Grammar Class import

suggested psudocode

// Test Context-Free Grammar Class

import java.io.*;

public class TestCFG

{

public static void main(String[] args)

{

// Language: strings that contain 0+ b's, followed by 2+ a's,

// followed by 1 b, and ending with 2+ a's.

String[] C = {"S=>bS",

"S=>aaT",

"T=>aT",

"T=>bU",

"U=>Ua",

"U=>aa"};

String inString, startWkString;

boolean accept1;

CFG CFG1 = new CFG(C);

if(args.length >= 1)

{

// Input string is command line parameter

inString = args[0];

char[] startNonTerm = new char[1];

startNonTerm[0] = CFG1.getStartNT();

startWkString = new String(startNonTerm);

accept1 = CFG1.processData(inString, startWkString);

System.out.println(" Accept String? " + accept1);

}

} // end main

} // end class

Part A Write a CFG class that meets the following design specifications: Instance variables: String[] Code char production rules as program code startNT-starting nonterminal CEG (String) C) Methods char getstartNT void setStartNT(char tNT) boolean processData (String inString, String wkString) The CFG class includes an instance variable Code of type String. The Code array contains program statements that define the production rules for the grammar. The instruction format for a CFG is: LHS->RHS where LHS is the left-hand side and RHS is the right-hand side of a production rule. For a CFG, the LHS character is a single nonterminal. The RHS string can contain both terminals and nonterminals. For example, the statement s->aTa, with LHS = S and RHS = ara, states that S can be replaced by aTa. It is assumed that the starting nonterminal is the LHS value for the first production rule. The setStartNT method can be used to change the starting nonterminal Note that you will need a recursive algorithm for the processData method, since two production rules may have the same LHS value. Your CFG class should work for the sample test program you will be given. Turn in the source code for your CFG class. Part A Write a CFG class that meets the following design specifications: Instance variables: String[] Code char production rules as program code startNT-starting nonterminal CEG (String) C) Methods char getstartNT void setStartNT(char tNT) boolean processData (String inString, String wkString) The CFG class includes an instance variable Code of type String. The Code array contains program statements that define the production rules for the grammar. The instruction format for a CFG is: LHS->RHS where LHS is the left-hand side and RHS is the right-hand side of a production rule. For a CFG, the LHS character is a single nonterminal. The RHS string can contain both terminals and nonterminals. For example, the statement s->aTa, with LHS = S and RHS = ara, states that S can be replaced by aTa. It is assumed that the starting nonterminal is the LHS value for the first production rule. The setStartNT method can be used to change the starting nonterminal Note that you will need a recursive algorithm for the processData method, since two production rules may have the same LHS value. Your CFG class should work for the sample test program you will be given. Turn in the source code for your CFG class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!