Question: Java or C++ program Define a context-free grammar for each of the languages described below. Then write a test program to implement the grammar as

Java or C++ program

Define a context-free grammar for each of the languages described below. Then write a test program to implement the grammar as an instance of your CFG class. Each context-free grammar should be defined in a separate test program.

A CFG for alphabet {x,y,z} that recognizes the language consisting of all strings that start with z, followed by N x's (N >= 0), followed by twice as many y's, and ending with z. Test your program with the following input strings:

zz, zxxyyz, zxxyyyy, zxyyz, zxxyyyyz

Sample test program for CFG class

// 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

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!