我正在创建一种新的编程语言,只是为了好玩和了解更基本的语言。我开始用 moo js 编写一个词法分析器,除了 NEWLINE 之外一切都运行良好。我尝试了很多事情,但它不会解决。我什至尝试从 moo js 的文档中复制一段确切的代码,但仍然没有帮助。
词法分析器代码:
const moo = require("moo");
const lexer = moo.compile({
whitespace: /[ \t]+/,
// comment: /\/\/.*?$/,
number: /0|[1-9][0-9]*/,
string: /"(?:\\["\\]|[^\n"\\])*"/,
leftParen: '(',
rightParen: ')',
// keyword: ['while', 'if', 'else', 'moo', 'cows'],
assignmentOp: "=",
identifier: /[a-zA-Z_][a-zA-Z0-9_]*/,
newline: { match: /\n/, lineBreaks: true },
});
module.exports = lexer;
文本词法分析器代码:
const fs = require("fs").promises;
const lexer = require("./lexer");
async function main() {
const code = (await fs.readFile("example1.hin")).toString();
lexer.reset(code);
let token;
while (true) {
token = lexer.next();
if (token) {
console.log("Got token", token);
} else {
break;
}
}
}
main().catch(err => console.log(err.stack));
测试示例:
n = 4
m = 6