词法分析器c++代码
时间: 2023-10-03 07:02:14 浏览: 95
以下是一个简单的词法分析器的C++代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
enum TokenType {
ID,
INT,
FLOAT,
OPERATOR,
PUNCTUATION,
KEYWORD
};
struct Token {
TokenType type;
string value;
};
bool isNumber(char c) {
return c >= '0' && c <= '9';
}
bool isAlpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool isOperator(char c) {
string operators = "+-*/=";
return (operators.find(c) != string::npos);
}
bool isPunctuation(char c) {
string punctuations = "(){}[],.;:";
return (punctuations.find(c) != string::npos);
}
bool isKeyword(string s) {
string keywords[] = {"if", "else", "while", "for", "int", "float", "return"};
for (int i = 0; i < 7; i++) {
if (s == keywords[i]) {
return true;
}
}
return false;
}
Token getNextToken(string input, int& currentPos) {
Token token;
token.type = OPERATOR;
while (currentPos < input.size() && input[currentPos] == ' ')
currentPos++;
if (currentPos == input.size()) {
token.type = OPERATOR;
token.value = "";
return token;
}
if (isNumber(input[currentPos])) {
token.type = INT;
while (currentPos < input.size() && isNumber(input[currentPos])) {
token.value += input[currentPos];
currentPos++;
}
if (currentPos < input.size() && input[currentPos] == '.') {
token.type = FLOAT;
token.value += input[currentPos];
currentPos++;
while (currentPos < input.size() && isNumber(input[currentPos])) {
token.value += input[currentPos];
currentPos++;
}
}
}
else if (isAlpha(input[currentPos])) {
token.type = ID;
while (currentPos < input.size() && (isAlpha(input[currentPos]) || isNumber(input[currentPos]))) {
token.value += input[currentPos];
currentPos++;
}
if (isKeyword(token.value)) {
token.type = KEYWORD;
}
}
else if (isOperator(input[currentPos])) {
token.type = OPERATOR;
token.value += input[currentPos];
currentPos++;
}
else if (isPunctuation(input[currentPos])) {
token.type = PUNCTUATION;
token.value += input[currentPos];
currentPos++;
}
return token;
}
int main() {
string input = "int a = 123; float b = 3.14; if (a > b) { return a; } else { return b; }";
int currentPos = 0;
while (currentPos < input.size()) {
Token token = getNextToken(input, currentPos);
cout << token.value << " ";
}
return 0;
}
```
该代码实现了一个简单的词法分析器,它将输入的字符串分解为多个token,每个token具有一种类型(如ID、INT、FLOAT、OPERATOR、PUNCTUATION、KEYWORD)和相应的值。getNextToken()函数根据当前位置的字符选择适当的类型,并返回包含类型和值的Token对象。在main()函数中,我们将字符串分解为多个token,并输出每个token的值。
阅读全文