Question
The goal of this assignment set is for you to apply your understanding of argument form validity checks to automate them by implementing code to
The goal of this assignment set is for you to apply your understanding of argument form validity checks to automate them by implementing code to accomplish this.
Write a Java program to automate checking validity of argument forms. To avoid the complexity of parsing arbitrary argument forms, we have included a parser and evaluator for you to use. Your code should be able to handle a varied selection of negations, conjunctions, disjunctions, and conditionals. Do not hardcode the truth tables, as your code should be able to handle any number of variables. We are giving you an additional file (Evaluator.java or Evaluator.py) to help you evaluate the argument forms. More about how to use this file is included in the template files. It is not necessary to upload this file to Gradescope.
package com.gradescope.validator;
import java.util.*;
final public class Evaluator { final private static char[] operators = {'^', 'V', '~', '>'}; // Evaluates an infix logical expression public static boolean evaluate(String arg, Hashtable
private static LinkedList
LinkedList
while (tokens.size() > 0) { // Get next token char token = tokens.remove(); if (isOperator(token)) { int precedence = getPrecedence(token); // Pop operators off the stack and push them into the output queue until the next operator // in the stack has a higher precedence (i.e. the operators popped off the stack will be // executed before the current operator) while (operator_stack.size() > 0 && precedence <= getPrecedence(operator_stack.peek())) { output.add(operator_stack.pop()); }
operator_stack.push(token); } else if (token == '(') { operator_stack.push(token); } else if (token == ')') { // Remove operators until the left parenthesis is found while (operator_stack.peek() != '(') { output.add(operator_stack.pop()); }
operator_stack.pop(); // Pop the left paren } else if (isVariable(token)) { output.add(token); } else { throw new Exception(String.format("Token \'%c\' is not a valid token.", token)); } } // Add the rest of the operators into the output in precedence order while (operator_stack.size() > 0) output.add(operator_stack.pop()); return output; } // Evaluates the given parsed text according to reverse polish notation private static boolean eval_rpn(LinkedList
private static boolean isOperator(char token) { for (int i = 0; i < 4; i++) { if (token == operators[i]) { return true; } }
return false; }
// Returns a number denoting when in the order of operations the operator // will be calculated (i.e. * before +). Higher number -> higher precedence private static int getPrecedence(char operator) { switch (operator) { case '>': return 0; case '^': case 'V': return 1; case '~': return 2; case '(': case ')': return -1; default: return -5; } }
}
// DO NOT DELETE THIS LINE!!! package com.gradescope.validator;
// Notes on how to use evaluator(): // Call the evaluator with Evaluator.evaluate(
// The only valid operators for premise strings are '^' (and), 'V' (or--CAPITAL v), '~' (not), // and '>' (implies), and you can use parentheses to override the order of operations as usual. // All variables should be lowercase letters and each should only be one character long. Finally, // do not include spaces in the string.
// For example, if you want to test the premise 'p implies q or not r', you should use 'p>qV~r' as // your premise string.
public class Validator { // All of the logic to complete this assignment should be written in this function. // This method accepts two things: An array of strings called premiseList and a // single string called conclusion. These strings should be formatted according to // the structure definded above. Also, this needs to return a boolean variable: true if // the argument form is valid, and false if it is not valid. public boolean validate(String[] premiseList, String conclusion) { return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started