#include <stdio.h>
#include <ctype.h>
#include <string.h>
// 保留字表
const char* reserved_words[] = { "const", "main", "if", "else", "while", "int", "for", "return", "then" };
int reserved_words_count = 9;
// 运算符表
const char* operators[] = { "+", "-", "*", "/", "==", "<", "<=", ">", ">=", "!=" };
int operators_count = 10;
// 分隔符表
const char* delimiters[] = { "(", ")", "{", "}", ";", ",", "=" };
int delimiters_count = 7;
// 全局变量
char source_code[1024];
int code_length = 0;
int position = 0;
// 函数声明
void skip_whitespace();
int is_reserved_word(char* word);
void recognize_keyword_or_identifier();
void recognize_number();
void recognize_operator();
void recognize_delimiter();
void lexical_analyzer();
int main() {
printf("请输入源代码(输入end结束):\n");
char line[1024];
while (fgets(line, sizeof(line), stdin) != NULL) {
if (strstr(line, "end") != NULL) { // 修正了这里,移除了多余的 '/'
break;
}
strcat(source_code, line);
code_length = strlen(source_code);
}
lexical_analyzer();
return 0;
}
// 跳过空白字符 skip_whitespace()函数用于跳过空格、制表符和换行符
void skip_whitespace() {
while (position < code_length && isspace(source_code[position])) {
position++;
}
}
// 判断是否是保留字
int is_reserved_word(char* word) {
for (int i = 0; i < reserved_words_count; i++) {
if (strcmp(word, reserved_words[i]) == 0) {
return 1;
}
}
return 0;
}
// 识别关键字或标识符
void recognize_keyword_or_identifier() {
char word[64] = "";
while (position < code_length && (isalnum(source_code[position]) || source_code[position] == '_')) {
strncat(word, &source_code[position], 1);
position++;
}
if (is_reserved_word(word)) {
printf("(1, %s)\n", word);
}
else {
printf("(2, %s)\n", word);
}
}
// 识别数字
void recognize_number() {
char number[64] = "";
while (position < code_length && isdigit(source_code[position])) {
strncat(number, &source_code[position], 1);
position++;
}
printf("(3, %s)\n", number);
}
// 识别运算符
void recognize_operator() {
int original_pos = position;
// 尝试双字符运算符
if (original_pos + 1 < code_length) {
char two_chars[3] = { source_code[original_pos], source_code[original_pos + 1], '\0' };
for (int i = 0; i < operators_count; i++) {
if (strcmp(two_chars, operators[i]) == 0) {
printf("(4, %s)\n", two_chars);
position += 2;
return;
}
}
}
// 尝试单字符运算符
char single_char[2] = { source_code[original_pos], '\0' };
for (int i = 0; i < operators_count; i++) {
if (strlen(operators[i]) == 1 && operators[i][0] == single_char[0]) {
printf("(4, %s)\n", single_char);
position += 1;
return;
}
}
printf("非法运算符: %s\n", single_char);
position += 1;
}
// 识别分隔符,将等于号识别为分隔符
void recognize_delimiter() {
char delimiter[2] = "";
strncat(delimiter, &source_code[position], 1);
position++;
printf("(5, %s)\n", delimiter);
}
// 词法分析器主函数 lexical_analyzer()函数用于遍历源代码,识别不同的单词
void lexical_analyzer() {
while (position < code_length) {
skip_whitespace();
if (position >= code_length) {
break;
}
char current_char = source_code[position];
if (isalpha(current_char) || current_char == '_') {
recognize_keyword_or_identifier();
}
else if (isdigit(current_char)) {
recognize_number();
}
else {
// 先判断是否为分隔符
int is_delimiter = 0;
for (int i = 0; i < delimiters_count; i++) {
if (source_code[position] == delimiters[i][0]) {
recognize_delimiter();
is_delimiter = 1;
break;
}
}
if (!is_delimiter) {
recognize_operator();
}
}
}
}
使用上述代码后,出现错误: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. compile E:\computerarchitecture\compile\exp01.cpp 38
该怎么解决
最新发布