Hello, I need help with only part b. I need help with making this in java or c++
ID: 3758215 • Letter: H
Question
Hello, I need help with only part b.
I need help with making this in java or c++.
thanks for the help.
The next field is used to point to the next item in the grammar rule, and the othar field is used to point to atematives given by the I metasymbol. Thus, the data structure for the grammar rale factor exp)aumber would look as follows points to structure for expi TRUX TRUEber where the fields of the record structure are shown as follows other a. Draw the data structures for the rest of the grammar rules in Figure 4.4, page 160 (Hint You will need a special token to represent internally in the data structure.) b. Write a generic parse procedure that uses these data structures to recognize an input stning c. Write a parser generator that reads BNF rules (either from afile or standard input)Explanation / Answer
The following prrgram will solve your question.
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
public final class StringToTypeParser {
private final Map<Type, TypeParser<?>> typeParsers;
final Splitter splitter;
final Splitter keyValuePairSplitter;
final InputPreprocessor inputPreprocessor;;
public static StringToTypeParserBuilder newBuilder() {
return new StringToTypeParserBuilder();
}
StringToTypeParser(StringToTypeParserBuilder builder) {
this.typeParsers = Collections.unmodifiableMap(new HashMap<Type, TypeParser<?>>(builder.typeParsers));
this.splitter = builder.splitter;
this.keyValuePairSplitter = builder.keyValuePairSplitter;
this.inputPreprocessor = builder.inputPreprocessor;
}
public <T> T parse(String input, Class<T> targetType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (targetType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("targetType"));
}
@SuppressWarnings("unchecked")
T temp = (T) parseType2(input, targetType);
return temp;
}
public <T> T parse(String input, GenericType<T> genericType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (genericType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("genericType"));
}
@SuppressWarnings("unchecked")
T temp = (T) parseType2(input, genericType.getType());
return temp;
}
public Object parseType(String input, Type targetType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (targetType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("targetType"));
}
return parseType2(input, targetType);
}
private Object parseType2(final String input, Type targetType) {
String preprocessedInput = preProcessInputString(input, targetType);
if(preprocessedInput == null){
if (isPrimitive(targetType)) {
String message = "'%s' primitive can not be set to null. Input: "%s"; Preprocessed input: '%s'";
throw new IllegalArgumentException(String.format(message, targetType, input, preprocessedInput));
}
return null;
}
if(typeParsers.containsKey(targetType)){
return invokeTypeParser(preprocessedInput, targetType, targetType);
}
if(targetType instanceof ParameterizedType){
ParameterizedType type = (ParameterizedType) targetType;
Class<?> rawType = (Class<?>) type.getRawType();
if(List.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_LIST, targetType);
}
if(Set.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_SET, targetType);
}
if(Map.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_MAP, targetType);
}
}
if(targetType instanceof Class){
Class<?> cls = (Class<?>) targetType;
if(cls.isArray()){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_ARRAY, targetType);
}
if(containsStaticMethodNamedValueOf(cls)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_CLASS_WITH_STATIC_VALUEOF_METHOD, targetType);
}
}
if(targetType instanceof GenericArrayType){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_ARRAY, targetType);
}
String message = "There is either no registered 'TypeParser' for that type, or that "
+ "type does not contain the following static factory method: '%s.%s(String)'.";
message = String.format(message, targetType, STATIC_FACTORY_METHOD_NAME);
message = makeParseErrorMsg(preprocessedInput, message, targetType);
throw new IllegalArgumentException(message);
}
private String preProcessInputString(String input, Type targetType) {
try {
return inputPreprocessor.prepare(input, new InputPreprocessorHelper(targetType));
} catch (Exception e) {
String message = "Exception thrown from InputPreprocessor: %s [%s] with message: "
+ "%s. See underlying exception for more information.";
message = String.format(message,
inputPreprocessor, inputPreprocessor.getClass(), e.getMessage());
message = makeParseErrorMsg(input, message, targetType);
throw new IllegalArgumentException(message, e);
}
}
private Object invokeTypeParser(String input, Type key, Type targetType) {
try {
TypeParser<?> typeParser = typeParsers.get(key);
ParseHelper parseHelper = new ParseHelper(this, targetType);
return typeParser.parse(input, parseHelper);
} catch (NumberFormatException e) {
String message = String.format("Number format exception %s.", e.getMessage());
message = makeParseErrorMsg(input, message, targetType);
throw new IllegalArgumentException(message, e);
} catch (RuntimeException e) {
String message = makeParseErrorMsg(input, e.getMessage(),targetType);
throw new IllegalArgumentException(message, e);
}
}
private boolean isPrimitive(Type targetType) {
if(targetType instanceof Class){
Class<?> c = (Class<?>) targetType;
return c.isPrimitive();
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.