function runSimulation() { const grammar = document.getElementById("grammar").value; const inputString = document.getElementById("inputString").value; fetch("/simulate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ grammar: grammar, input: inputString }) }) .then(res => res.json()) .then(data => { if (!data.success) { alert(data.message); return; } document.getElementById("resultBox").style.display = "block"; document.getElementById("startSymbol").innerText = data.start_symbol; document.getElementById("showInput").innerText = data.input; document.getElementById("finalResult").innerText = data.final_result; const grammarList = document.getElementById("grammarList"); grammarList.innerHTML = ""; data.grammar.forEach(rule => { const li = document.createElement("li"); li.innerText = rule; grammarList.appendChild(li); }); const exploredList = document.getElementById("exploredList"); exploredList.innerHTML = ""; data.explored.forEach(step => { const li = document.createElement("li"); li.innerText = step; exploredList.appendChild(li); }); const derivationContainer = document.getElementById("derivationContainer"); derivationContainer.innerHTML = ""; if (data.derivations.length === 0) { derivationContainer.innerHTML = "

No valid derivation found.

"; } else { data.derivations.forEach((derivation, index) => { const box = document.createElement("div"); box.className = "derivation-box"; let html = `

Derivation ${index + 1}

`; derivation.forEach(step => { html += `
${step}
`; }); box.innerHTML = html; derivationContainer.appendChild(box); }); } const treeContainer = document.getElementById("treeContainer"); treeContainer.innerHTML = ""; if (data.trees.length === 0) { treeContainer.innerHTML = "

No parse tree available.

"; } else { data.trees.forEach((tree, index) => { const box = document.createElement("div"); box.className = "tree-box"; let html = `

Parse Tree ${index + 1}

`; tree.steps.forEach((step, i) => { html += `
Level ${i}: ${step}
`; }); box.innerHTML = html; treeContainer.appendChild(box); }); } document.getElementById("resultBox").scrollIntoView({ behavior: "smooth" }); }) .catch(err => { console.error(err); alert("Something went wrong."); }); }