Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My Java code: /** Program: GrammarProcessor Programmer: Allen Putich Purpose: Ge

ID: 662748 • Letter: M

Question

My Java code:

/**

Program: GrammarProcessor

Programmer: Allen Putich

Purpose: Generates string(s) of sentences that follow predetermined grammatical rules

*/

import java.io.*;

import java.util.*;

public class GrammarProcessor {

//Variables

private Map<String, String[]> rules = new TreeMap<String, String[]>(); //Use a ruleName to find the grammatical rule

public GrammarProcessor (List<String> grammar){

List<String> rulesList = new ArrayList<String>();

rulesList = (List<String>)grammar; //Type cast to insure proper type

try {

while(int i = 0; i < rulesList.size(); i++){

String[] token = rulesList.get(i).split("::="); //Break up of strings "side[0]" ::= "side[1]"

String left = token[0].trim(); //Grammar rule name

String[] right = token[1].split("[|]").trim();//Array of possible rule structures

rules.put(left, right); //Add key and array of possible structures to rules

}

}catch(Exception e){

throw new IllegalArgumentException ("List<String> is null or wrong type");

}

}//End of GrammarProcessor

public boolean grammarContains(String symbol){

return rules.containsKey(symbol);

}

//Shows all keys that exist in the map

public String getSymbols(){

return rules.keyset().toString();

}

//Public helper method to generate sentences

public String generate(String symbol){

generate(symbol, null)

}

private String generate(String symbol, String symbolKey){

//symbol is found as key

if(grammarContains(symbol){

String[] values = rules.get(symbol);

while(int i = 0; i < values.size(); i++){

generate(values[i], symbol)

}

//symbol is not a key

}else{

if(rules.containsValue(symbol){//Terminal value

values = map.get(symbolKey);//Contains terminal values

int wordSelect = (int)Math.random()*values.size();

return values[wordSelect];

}else{

//Entered symbol doesn't exist as a key or value

throws new IllegalArgumentException("The grammar does not conbtain the given nonterminal symbol");

}

//Value is terminal

}

}

}

I recieve this error:

GrammarProcessor.java:17: error: '.class' expected
while(int i = 0; i < rulesList.size(); i++){
^
Can someone help me find the error I'm making?

Explanation / Answer

Yeah There are lot of errors.....

1) while loop syntax is not correct .....instead use for which is fine better

2)You missed ; at some function calls....generate(symbol, null)???---------->generate(symbol, null);

3)keyset error.....change to keySet

4) return statement misses for generate methods;

5) values = map.get(symbolKey);...you missed declaring string before......String[]  values = map.get(symbolKey);

6)You are throws exception..where you didnt declared anywhere..declare first or change to throw

7)define ruleslist as global instead in function(constructor)

8)no main method..i hope you are operating this by another class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote