未捕获的语法错误但无法找到指定的令牌

时间:2018-04-25 02:10:51

标签: javascript

我一直在努力调试这个问题几个小时,不幸的是无处可去。我使用JS mint来看看我是否可以通过那里找到错误但无处可去。

当eventlistener发生时,会发生语法错误。

Chrome说:

VM33:1 Uncaught SyntaxError: Unexpected token )
    at processInput (VM22 lexer_parser.js:42)
    at HTMLFormElement.form.addEventListener (VM22 lexer_parser.js:52)

Firefox说:

SyntaxError: expected expression, got ')'[Learn More]
lexer_parser.js:1:1
processInput
file://lexer_parser.js:42:15
<anonymous>
file://lexer_parser.js:52:21

lexer_parser.js

var lexer_parser = lexer_parser || {};

//Turn input string into a list of tokens
lexer_parser.lex = (str) => str.split(" ").map(s => s.trim()).filter(s => s.length);

lexer_parser.Num = Symbol("num");
lexer_parser.Op = Symbol("op");

//Begin parsing
lexer_parser.parse = tokens => {
  let i = 0;
  const peek = () => tokens[i];
  const next = () => tokens[++i];

  const parse_num = () => ({ val: parseInt(next()), type: lexer_parser.Num});

  const parse_op = () => {
      const node = {val: next(), type: lexer_parser.Op, expr: [] };
      while(peek()) node.expr.push(parse_result());
    return node;
  };

  const parse_result = () => /\d/.test(peek()) ? parse_num() : parse_op();

  return parse_result();
};

lexer_parser.transpile = ast => {
  const op_values = {sum: "+", mul: "*", sub: "-", div: "/"};
  const transpile_node = ast =>
    ast.type === lexer_parser.Num ? transpile_num(ast) : transpile_op(ast);
  const transpile_num = ast => ast.val;
  const transpile_op = ast =>
        `(${ast.expr.map(transpile_node).join(" " + op_values[ast.val] + " ")})`;
  return transpile_node(ast);
};

window.addEventListener("load", () => {
  const processInput = str =>
  {
    let syn = lexer_parser.transpile(lexer_parser.parse(lexer_parser.lex(str)));
    let val = eval(syn);
    return {
      syntax: syn,
      value: val
    };
  };

  var form = document.getElementById("inputForm");
  var text = document.getElementById("lexer_output");
  form.addEventListener("submit", (e) => {
    const content = processInput(document.getElementById("lexer_input").value);
    text.innerHTML = content.value + "<br/>" + content.syntax;
    console.log(content.value);
    console.log(content.syntax);
    e.preventDefault();
  });
});

lab.html

<html>

<head>
  <script src="lexer_parser.js"></script>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="took_out" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="tookout" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="tookout" crossorigin="anonymous"></script>
</head>
<body>
<div class="row">
  <div class="col-md-12" id="task_02">
    <hr />
    <div class="container">
      <form class="form-horizontal" id="inputForm">
        <div class="form-group">
          <label for="lexer_input">Task 02</label>
          <input type="text" value="" class="form-control" id="lexer_input"> <br />
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-default">Compute</button>
        </div>
      </form>
      <br />
      <div id="lexer_output">
      </div>
    </div>
  </div>
</div>
</body>

</html>

我故意取出了诚信价值观。

0 个答案:

没有答案