Advertisement
SenpaiZero

quiz

Mar 17th, 2024
378
0
102 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>03 Quiz 1</title>
  5.        
  6.     </head>
  7.     <body>
  8.         <center>
  9.             <h1>03 QUIZ 1</h1>
  10.             <h3>JAVASCRIPT</h3>
  11.             <h3>SANTOS, YGI MARTIN</h3>
  12.             <h3>BSIT3A</h3>
  13.  
  14.             <br>
  15.             <h6>OUTPUT IN CONSOLE</h6>
  16.  
  17.         </center>
  18.  
  19.         <script>
  20.             const output = document.getElementById("output");
  21.             class LexType {}
  22.  
  23.             class LexContainer extends LexType {
  24.                 constructor() {
  25.                     super();
  26.                     this.items = [];
  27.                 }
  28.  
  29.                 get Items() {
  30.                     return this.items;
  31.                 }
  32.             }
  33.  
  34.             class LexValue extends LexType {
  35.                 constructor(value, isExpression) {
  36.                     super();
  37.                     this.Value = value;
  38.                     this.IsExpression = isExpression;
  39.                 }
  40.             }
  41.  
  42.             const expressions = [
  43.                 "A = (A^2 * (B+C)^2)",
  44.                 "B = A + C + D",
  45.                 "C = A + D^2",
  46.                 "D = (A + B)^2 + C",
  47.                 "E = (A - B + C) + (C + D^3)"
  48.             ];
  49.  
  50.             function main() {
  51.                 expressions.forEach((exp, i) => {
  52.                     console.log(`${i + 1}: `, end="");
  53.                     parseExp(exp);
  54.                 });
  55.             }
  56.  
  57.             function parseExp(exp) {
  58.                 try {
  59.                     if (!exp.trim()) return;
  60.  
  61.                     console.log(`Sentence: ${exp}`);
  62.                     console.log("<assign> => ");
  63.  
  64.                     const c = parse([...exp.trim().replace(/\s+/g, '').matchAll(/[A-Za-z0-9()+\-*/^=]/g)].map(match => match[0]));
  65.  
  66.                     console.log(`\t\t| ${formatExpression('<assign>', c, 0)}\n`);
  67.                 } catch (ex) {
  68.                     console.log(`[!]: Failed to parse (${ex})\n`);
  69.                 }
  70.             }
  71.  
  72.             function formatExpression(expression, c, sub) {
  73.                 if (!c.Items || !expression.trim()) return "";
  74.                 if (expression.includes("<assign>")) expression = "<id> = <expr>";
  75.  
  76.                 while (c.Items.length > 0) {
  77.                     let sb = `\t\t| ${expression}`;
  78.                     if (sub > 0) {
  79.                         sb += ")".repeat(sub);
  80.                         if (sub > 1) sb += " <cont>".repeat(sub - 1);
  81.                     }
  82.  
  83.                     console.log(sb);
  84.  
  85.                     let t = c.Items[0];
  86.  
  87.                     if (expression.includes("<id>")) {
  88.                         if (!(t instanceof LexValue) || !t.IsExpression)
  89.                             throw new Error("Invalid expression found.");
  90.                         expression = expression.replace("<id>", t.Value);
  91.  
  92.                         c.Items.shift();
  93.                         if (c.Items.length === 0) break;
  94.  
  95.                         t = c.Items[0];
  96.  
  97.                         if (!(t instanceof LexValue) || t.IsExpression || !expression.includes(t.Value))
  98.                             throw new Error("Invalid value found.");
  99.  
  100.                         c.Items.shift();
  101.                         continue;
  102.                     }
  103.  
  104.                     if (expression.includes("<expr>")) {
  105.                         if (t instanceof LexValue) {
  106.                             if (!t.IsExpression) throw new Error(`'${t.Value}' expected.`);
  107.                             if (c.Items.length === 0) throw new Error("Syntax error.");
  108.  
  109.                             if (c.Items.length > 1) {
  110.                                 const iop = c.Items[1];
  111.                                 if (iop instanceof LexValue) {
  112.                                     if (iop.IsExpression)
  113.                                         throw new Error(`'${iop.Value}' expected.`);
  114.                                     expression = expression.replace("<expr>", `<id> ${iop.Value} <expr>`);
  115.                                 }
  116.                                 continue;
  117.                             }
  118.  
  119.                             expression = expression.replace("<expr>", "<id>");
  120.                             continue;
  121.                         }
  122.  
  123.                         const container = t;
  124.                         c.Items.shift();
  125.  
  126.                         const opIdx = container.Items.findIndex(itm => itm instanceof LexValue && !itm.IsExpression);
  127.                         if (opIdx === -1) throw new Error("Syntax error.");
  128.                         const lv = container.Items[opIdx];
  129.                         expression = expression.replace("<expr>", `(<id> ${lv.Value} <expr>`);
  130.                         expression = formatExpression(expression, container, sub + 1);
  131.  
  132.                         if (c.Items.length > 0) {
  133.                             t = c.Items[0];
  134.                             if (!(t instanceof LexValue) || t.IsExpression)
  135.                                 throw new Error("Syntax error.");
  136.                             sb = expression + ` ${t.Value} <expr>`;
  137.                             expression = sb;
  138.                             c.Items.shift();
  139.                             continue;
  140.                         }
  141.                     }
  142.                 }
  143.                 if (sub > 0) expression += ")".repeat(sub);
  144.                 return expression;
  145.             }
  146.  
  147.             function parse(exp) {
  148.                 const lexContainer = new LexContainer();
  149.                 while (exp.length > 0) {
  150.                     if (/[A-Za-z0-9]/.test(exp[0])) {
  151.                         lexContainer.Items.push(new LexValue(exp[0], true)); // Expression.
  152.                         exp.shift();
  153.                         continue;
  154.                     }
  155.                     if (isOperator(exp[0])) {
  156.                         if (exp[0] === '(') {
  157.                             const closeParenthesisIdx = getCloseParenthesisIndex(exp);
  158.                             if (closeParenthesisIdx === -1) throw new Error("Imbalance parentheses.");
  159.  
  160.                             const newArray = exp.slice(1, closeParenthesisIdx);
  161.                             lexContainer.Items.push(parse(newArray));
  162.                             exp = exp.slice(closeParenthesisIdx + 1);
  163.                             continue;
  164.                         }
  165.                         lexContainer.Items.push(new LexValue(exp[0], false)); // Operator.
  166.                         exp.shift();
  167.                     }
  168.                 }
  169.                 return lexContainer;
  170.             }
  171.  
  172.             function getCloseParenthesisIndex(c) {
  173.                 if (c.length === 0) return -1;
  174.                 const openParentheses = [];
  175.                 for (let i = 0; i < c.length; i++) {
  176.                     if (c[i] === '(') {
  177.                         openParentheses.push('(');
  178.                         continue;
  179.                     }
  180.                     if (c[i] === ')') {
  181.                         if (openParentheses.length === 0) return -1;
  182.                         openParentheses.pop();
  183.                         if (openParentheses.length === 0) return i;
  184.                     }
  185.                 }
  186.                 return -1;
  187.             }
  188.  
  189.             function isOperator(c) {
  190.                 return ['+', '-', '*', '/', '^', '=', '(', ')'].includes(c);
  191.             }
  192.  
  193.             main();
  194.  
  195.  
  196.         </script>
  197.     </body>
  198. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement