Propositional Calculus Parser
by zcomer
HTML
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Logic App</title>
<script type="text/javascript">
var sentence;
var len;
var i;
var sym;
function validate() {
var result;
sentence = document.getElementById('formulaentry').value;
len = sentence.length;
i = -1;
if (sentence == "") {
document.getElementById('formulaentry').value = "There's no formula!";
} else {
nextSym();
result = formula();
if(result == 0) {
document.getElementById('formulaentry').value = "Invalid";
} else {
document.getElementById('formulaentry').value = "Valid";
}
}
}
function nextSym() {
i++;
if (i <= (len-1)) {
sym = sentence.charAt(i);
} else {
sym = "";
}
//console.log("Current Sym:" + sym);
}
function accept(s) {
if (sym == s) {
nextSym();
return 1;
}
return 0;
}
function expect(s) {
if (accept(s)) {
return 1;
}
return 0;
}
function formula() {
if (unaryconn(sym)) {
nextSym();
if (formula() == 0) return 0;
} else if (accept("(")) {
if (formula() == 0) return 0;
if (binaryconn(sym) == 0) return 0;
nextSym();
if (formula() == 0) return 0;
if (!expect(")")) return 0;
} else if (proposition(sym)) {
nextSym();
} else {
return 0;
}
}
function unaryconn(s) {
if (s == "~") {
return 1;
} else {
return 0;
}
}
function binaryconn(s) {
if (s == "&") {
return 1;
} else {
return 0;
}
}
function proposition(s) {
if (s == "p" || s == "q" || s == "r") {
return 1;
} else {
return 0;
}
}
</script>
</head>
<body>
<h1>Logic App</h1>
<h2>Check if formula is...