Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>03 Quiz 1</title>
- </head>
- <body>
- <center>
- <h1>03 QUIZ 1</h1>
- <h3>JAVASCRIPT</h3>
- <h3>SANTOS, YGI MARTIN</h3>
- <h3>BSIT3A</h3>
- <br>
- <h6>OUTPUT IN CONSOLE</h6>
- </center>
- <script>
- const output = document.getElementById("output");
- class LexType {}
- class LexContainer extends LexType {
- constructor() {
- super();
- this.items = [];
- }
- get Items() {
- return this.items;
- }
- }
- class LexValue extends LexType {
- constructor(value, isExpression) {
- super();
- this.Value = value;
- this.IsExpression = isExpression;
- }
- }
- const expressions = [
- "A = (A^2 * (B+C)^2)",
- "B = A + C + D",
- "C = A + D^2",
- "D = (A + B)^2 + C",
- "E = (A - B + C) + (C + D^3)"
- ];
- function main() {
- expressions.forEach((exp, i) => {
- console.log(`${i + 1}: `, end="");
- parseExp(exp);
- });
- }
- function parseExp(exp) {
- try {
- if (!exp.trim()) return;
- console.log(`Sentence: ${exp}`);
- console.log("<assign> => ");
- const c = parse([...exp.trim().replace(/\s+/g, '').matchAll(/[A-Za-z0-9()+\-*/^=]/g)].map(match => match[0]));
- console.log(`\t\t| ${formatExpression('<assign>', c, 0)}\n`);
- } catch (ex) {
- console.log(`[!]: Failed to parse (${ex})\n`);
- }
- }
- function formatExpression(expression, c, sub) {
- if (!c.Items || !expression.trim()) return "";
- if (expression.includes("<assign>")) expression = "<id> = <expr>";
- while (c.Items.length > 0) {
- let sb = `\t\t| ${expression}`;
- if (sub > 0) {
- sb += ")".repeat(sub);
- if (sub > 1) sb += " <cont>".repeat(sub - 1);
- }
- console.log(sb);
- let t = c.Items[0];
- if (expression.includes("<id>")) {
- if (!(t instanceof LexValue) || !t.IsExpression)
- throw new Error("Invalid expression found.");
- expression = expression.replace("<id>", t.Value);
- c.Items.shift();
- if (c.Items.length === 0) break;
- t = c.Items[0];
- if (!(t instanceof LexValue) || t.IsExpression || !expression.includes(t.Value))
- throw new Error("Invalid value found.");
- c.Items.shift();
- continue;
- }
- if (expression.includes("<expr>")) {
- if (t instanceof LexValue) {
- if (!t.IsExpression) throw new Error(`'${t.Value}' expected.`);
- if (c.Items.length === 0) throw new Error("Syntax error.");
- if (c.Items.length > 1) {
- const iop = c.Items[1];
- if (iop instanceof LexValue) {
- if (iop.IsExpression)
- throw new Error(`'${iop.Value}' expected.`);
- expression = expression.replace("<expr>", `<id> ${iop.Value} <expr>`);
- }
- continue;
- }
- expression = expression.replace("<expr>", "<id>");
- continue;
- }
- const container = t;
- c.Items.shift();
- const opIdx = container.Items.findIndex(itm => itm instanceof LexValue && !itm.IsExpression);
- if (opIdx === -1) throw new Error("Syntax error.");
- const lv = container.Items[opIdx];
- expression = expression.replace("<expr>", `(<id> ${lv.Value} <expr>`);
- expression = formatExpression(expression, container, sub + 1);
- if (c.Items.length > 0) {
- t = c.Items[0];
- if (!(t instanceof LexValue) || t.IsExpression)
- throw new Error("Syntax error.");
- sb = expression + ` ${t.Value} <expr>`;
- expression = sb;
- c.Items.shift();
- continue;
- }
- }
- }
- if (sub > 0) expression += ")".repeat(sub);
- return expression;
- }
- function parse(exp) {
- const lexContainer = new LexContainer();
- while (exp.length > 0) {
- if (/[A-Za-z0-9]/.test(exp[0])) {
- lexContainer.Items.push(new LexValue(exp[0], true)); // Expression.
- exp.shift();
- continue;
- }
- if (isOperator(exp[0])) {
- if (exp[0] === '(') {
- const closeParenthesisIdx = getCloseParenthesisIndex(exp);
- if (closeParenthesisIdx === -1) throw new Error("Imbalance parentheses.");
- const newArray = exp.slice(1, closeParenthesisIdx);
- lexContainer.Items.push(parse(newArray));
- exp = exp.slice(closeParenthesisIdx + 1);
- continue;
- }
- lexContainer.Items.push(new LexValue(exp[0], false)); // Operator.
- exp.shift();
- }
- }
- return lexContainer;
- }
- function getCloseParenthesisIndex(c) {
- if (c.length === 0) return -1;
- const openParentheses = [];
- for (let i = 0; i < c.length; i++) {
- if (c[i] === '(') {
- openParentheses.push('(');
- continue;
- }
- if (c[i] === ')') {
- if (openParentheses.length === 0) return -1;
- openParentheses.pop();
- if (openParentheses.length === 0) return i;
- }
- }
- return -1;
- }
- function isOperator(c) {
- return ['+', '-', '*', '/', '^', '=', '(', ')'].includes(c);
- }
- main();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement