词法分析器
var inputCode = "int main (){console.log()}"
var reservedWords = [
"if",
"int",
"for",
"while",
"do",
"return",
"break",
"continue",
]
var operators = ["+", "-", "*", "/", "=", "<", ">", "!", ">=", "<=", "!=", "."]
var separators = [",", ";", "{", "}", "(", ")"]
let tokens = Lexical_Analysis(inputCode)
for (let index = 0; index < tokens.length; index++) {
console.log(tokens[index])
}
function Lexical_Analysis(str) {
let cur = 0
let tokens = []
while (cur < str.length) {
if (/\s/.test(str[cur])) {
cur++
} else if (/[a-z]/i.test(str[cur])) {
let word = "" + str[cur++]
while (cur < str.length && /[a-z]/i.test(str[cur])) {
word += str[cur++]
}
if (reservedWords.includes(word)) {
tokens.push({
type: 1,
value: word,
})
} else {
tokens.push({
type: 2,
value: word,
})
}
} else if (separators.includes(str[cur])) {
tokens.push({
type: 5,
value: str[cur++],
})
} else if (operators.includes(str[cur])) {
let operator = "" + str[cur++]
if ([">", "<", "!"].includes(operator)) {
if ((str[cur] = "=")) {
operator += str[cur++]
}
}
tokens.push({
type: 4,
value: operator,
})
} else if (/^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/.test(str[cur])) {
let flo = "" + str[cur++]
while (cur < str.length && /[0-9]/.test(str[cur])) {
flo += str[cur++]
}
tokens.push({
type: 6,
value: flo,
})
} else if (/[0-9]/.test(str[cur])) {
let val = "" + str[cur++]
while (cur < str.length && /[0-9]/.test(str[cur])) {
val += str[cur++]
}
tokens.push({
type: 3,
value: val,
})
} else {
return "包含非法字符:" + str[cur]
}
}
return tokens
}