Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

Graph the inequality. 2x - 3y 6

Answered: 1 week ago

Question

=+3. Who can provide information for evaluation?

Answered: 1 week ago

Question

=+ What typical employee will the IA compare him/

Answered: 1 week ago

Question

=+7 What is the overall cost of the international assignment?

Answered: 1 week ago