Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA 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

JAVA 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

CFG CLASS TO TEST WITH BELOW

CFG.java public class CFG { private String[] Code; private char startNT; /* * The constructor takes an array of production rules in string format. * Each element of C is expected to be of the form * "[non-terminal]=>[terminals and non-terminals]". */ public CFG(String[] C){ Code = C; startNT = C[0].charAt(0); } /* * The getStartNT method returns the char currently used as the * starting non-terminal */ public char getStartNT(){ return startNT; }

/* * The setStartNT method allows the starting non-terminal to be * changed */ public void setStartNT(char stNT){ startNT = stNT; }

/* * The processData method determines if inString is in the CFG. * wkString should be "[starting non-terminal]" */ public boolean processData(String inString, String wkString){ // + 1 to account for lambda productions // May not work for some words produced through strings // containing multiple NT's with lambda productions. // This is okay considering we can always remove lambda productions. if (wkString.length() > inString.length() + 1) return false; if (wkString.equals(inString)) return true; // for each production rule in the CFG for (int prodRule = 0; prodRule < Code.length; prodRule++){ String thisProdWkString = wkString; char NT = Code[prodRule].charAt(0); //RHS starts at 3: "[NT]=>[RHS]" String RHS = Code[prodRule].substring(3); int NTLoc = thisProdWkString.indexOf(NT); if (NTLoc == -1) continue; else{ thisProdWkString = thisProdWkString.replaceFirst("[" + NT + "]", RHS); if (processData(inString,thisProdWkString)) return true; else continue; } } return false; } }

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

More Books

Students also viewed these Databases questions

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago

Question

Explain the importance of nonverbal messages.

Answered: 1 week ago

Question

Describe the advantages of effective listening.

Answered: 1 week ago

Question

Prepare an employment application.

Answered: 1 week ago