本分析器能识别的单词符号及编码如下表(可改变):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int getSymbolCode(char *symbol) {
if (strcmp(symbol, "begin") == 0) {
return 1;
} else if (strcmp(symbol, "if") == 0) {
return 2;
} else if (strcmp(symbol, "then") == 0) {
return 3;
} else if (strcmp(symbol, "while") == 0) {
return 4;
} else if (strcmp(symbol, "do") == 0) {
return 5;
} else if (strcmp(symbol, "end") == 0) {
return 6;
} else if (isalpha(symbol[0])) {
return 10;
} else if (isdigit(symbol[0])) {
return 11;
} else if (symbol[0] == '*') {
return 13;
} else if (symbol[0] == '+') {
return 15;
} else if (symbol[0] == '-') {
return 16;
} else if (symbol[0] == ':') {
if (symbol[1] == '=') {
return 18;
} else {
return 17;
}
} else if (strcmp(symbol, ">") == 0) {
return 20;
} else if (strcmp(symbol, "<>") == 0) {
return 21;
} else if (strcmp(symbol, "<=") == 0) {
return 22;
} else if (strcmp(symbol, ">=") == 0) {
return 24;
} else if (strcmp(symbol, "=") == 0) {
return 25;
} else if (symbol[0] == ';') {
return 26;
} else if (symbol[0] == '(') {
return 27;
} else if (symbol[0] == ')') {
return 28;
} else {
return 0; // 非法字符
}
}
void lexicalAnalyzer(char *input) {
int start = 0;
int end = 0;
int length = strlen(input);
while (end < length) {
// 找到符号的起始位置
while (input[start] == ' ' && start < length) {
start++;
}
// 找到符号的结束位置
end = start;
while (input[end] != ' ' && end < length) {
end++;
}
// 提取符号
char symbol[100];
strncpy(symbol, input + start, end - start);
symbol[end - start] = '\0';
// 获取符号的种别码并输出结果
int symbolCode = getSymbolCode(symbol);
printf("%s\t种别码:%d\n", symbol, symbolCode);
start = end;
}
}
int main() {
char input[100];
printf("请输入字符串:");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // 去除换行符
lexicalAnalyzer(input);
return 0;
}
代码截图: