File size: 2,785 Bytes
f6caadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 = "<p>No valid derivation found.</p>";
    } else {
      data.derivations.forEach((derivation, index) => {
        const box = document.createElement("div");
        box.className = "derivation-box";

        let html = `<h4>Derivation ${index + 1}</h4>`;
        derivation.forEach(step => {
          html += `<div class="step">${step}</div>`;
        });

        box.innerHTML = html;
        derivationContainer.appendChild(box);
      });
    }

    const treeContainer = document.getElementById("treeContainer");
    treeContainer.innerHTML = "";

    if (data.trees.length === 0) {
      treeContainer.innerHTML = "<p>No parse tree available.</p>";
    } else {
      data.trees.forEach((tree, index) => {
        const box = document.createElement("div");
        box.className = "tree-box";

        let html = `<h4>Parse Tree ${index + 1}</h4>`;
        tree.steps.forEach((step, i) => {
          html += `<div class="step">Level ${i}: ${step}</div>`;
        });

        box.innerHTML = html;
        treeContainer.appendChild(box);
      });
    }

    document.getElementById("resultBox").scrollIntoView({
      behavior: "smooth"
    });
  })
  .catch(err => {
    console.error(err);
    alert("Something went wrong.");
  });
}