(The answer given below is incorrect, since other conditions were checked in it) There are two arrays - termianl and neterminal. when adding rules, it must be determined to which type of Chomsky grammar the rule being added. Type 3 grammar: inputw1 must contain a single character from neterminal, and inputw2 has a chain of characters from termianl and a character from neterminal, which can be either the first, or the last, or be absent (for example, D -> azdA, A -> Ddz). Context-free grammar: Inputw1 should contain only one character from neterminal, and inputw2 can contain any string of characters from terminal and neterminal, including an empty one (for example, A -> zdA, D -> dzb). Context-sensitive grammar: a line from inputw1 must contain a character from neterminal, which is frustrated with context (for example, S -> abc is context-free, and aS -> abc is context-dependent). I do not understand how to do it correctly so that type 3 is determined.
var terminal = ['a', 'b', 'c']; var term=document.getElementById('terminal').innerHTML = terminal; var neterminal = ['D', 'A', 'Z', 'L']; var neterm = document.getElementById('neterminal').innerHTML = neterminal; function pushTerminal(){ var x = document.getElementById('newTerminal'); var str = term.indexOf(document.getElementById("inputAdd").value); var str2 = neterm.indexOf(document.getElementById("inputAdd").value); if(str == -1 && str2== -1) { term.push(document.getElementById("inputAdd").value); x.innerHTML = term.join(); }else{ alert("ΠΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ"); } } //ΡΡΠ½ΠΊΡΠΈΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π½ΠΈΡ ΡΠΈΠΏΠ° ΠΏΡΠ°Π²ΠΈΠ»Π° function determineType(str, dict, caseSensitive) { if(!str.length){ return false; } else if (typeof dict === 'string'){ var dict = dict.split(''); } else if (!(dict instanceof Array || !dict.length)){ return false; } var flags = caseSensitive ? 'g' : 'gi'; var idx = -1; var count, result; result = !dict.some(function(v, i){ count = (str.match(new RegExp(v + '',flags))||[]).length; return ((count === 1) && (idx<0)) ? (idx = i, false) : (!count ? false : true); }); return (result && (idx>-1))? idx : false; } var changeStringFromRules = []; var changeStringToRules = []; function pushRules(thisBtn){ var frm = thisBtn.parentElement; var w1 = frm.inputw1.value; var w2 = frm.inputw2.value; var i1 = determineType(w1, neterminal, true); var i2 = determineType(w2, neterminal, true); var rt = (i2 !==false) ? 'ΠΠΎΠ½ΡΠ΅ΠΊΡΡΠ½ΠΎ-Π·Π°Π²ΠΈΡΠΈΠΌΠ°Ρ Π³ΡΠ°ΠΌΠΌΠ°ΡΠΈΠΊΠ°' : ((i1!==false)? 'ΠΠΎΠ½ΡΠ΅ΠΊΡΡΠ½ΠΎ-ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°Ρ Π³ΡΠ°ΠΌΠΌΠ°ΡΠΈΠΊΠ°' : ''); changeStringFromRules.push(w1); changeStringToRules.push(w2); var li = document.createElement("li"); li.textContent = w1+'-->'+w2 +' '+ rt; document.getElementById('list').appendChild(li); var removeBtn = document.createElement("input"); removeBtn.type = "button"; removeBtn.value = "Π£Π΄Π°Π»ΠΈΡΡ"; removeBtn.onclick = remove; li.appendChild(removeBtn); document.getElementById("list").appendChild(li); } function remove(e) { var el = e.target; el.parentNode.remove(); } <div class="alphabet"> <div class="terminal"> <h2>Π’Π΅ΡΠΌΠΈΠ½Π°Π»ΡΠ½ΡΠΉ Π°Π»ΡΠ°Π²ΠΈΡ: </h2> <div id="terminal"></div> <h2>ΠΠΎΠ²ΡΠΉ Π’Π΅ΡΠΌΠΈΠ½Π°Π»ΡΠ½ΡΠΉ Π°Π»ΡΠ°Π²ΠΈΡ: </h2> <div id="newTerminal"></div> <div class="addTerminal"> <h3>ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠ΅ΡΠΌΠΈΠ½Π°Π»Π°</h3> <form> <input id ="inputAdd" type="text"> <input type="button" value="ΠΠΎΠ±Π°Π²ΠΈΡΡ" onclick="pushTerminal()"> </form> </div> </div> <div class="neterminal"> <h2>ΠΠ΅ΡΠ΅ΡΠΌΠΈΠ½Π°Π»ΡΠ½ΡΠΉ Π°Π»ΡΠ°Π²ΠΈΡ: </h2> <div id="neterminal"></div> </div> </div> <div class="addRules"> <h3>ΠΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΡΠ°Π²ΠΈΠ»ΠΎ</h3> <form> <label>w1:</label><input name="inputw1" type="text"><label> --> w2:</label><input name="inputw2" type="text"> <input type="button" value="Add" onclick="pushRules(this)"> </form> <h3>ΠΡΠ°Π²ΠΈΠ»Π°:</h3> <div class="container_rules"> <ui id="list"></ui> </div> </div>