用c语言编写一个词法分析程序、要求能够识别以下代码: int main() { char c=56; int a=1; int bb_b1=7; if(a>bb_b1)c=9; else c=90; while(1)
时间: 2024-01-08 11:04:19 浏览: 58
以下是一个简单的词法分析程序,可以识别出给定代码中的标识符、关键字、运算符、常量等元素。
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_TOKEN_LEN 100
enum token_type {
KEYWORD, IDENTIFIER, CONSTANT, OPERATOR, DELIMITER, UNKNOWN
};
enum keyword_type {
KW_INT, KW_CHAR, KW_IF, KW_ELSE, KW_WHILE, KW_RETURN
};
enum operator_type {
OP_ASSIGN, OP_EQ, OP_NE, OP_LT, OP_GT, OP_LE, OP_GE, OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD
};
enum delimiter_type {
DEL_SEMICOLON, DEL_COMMA, DEL_LPAREN, DEL_RPAREN, DEL_LBRACE, DEL_RBRACE
};
struct token {
char lexeme[MAX_TOKEN_LEN];
enum token_type type;
};
struct keyword {
char *name;
enum keyword_type id;
};
struct operator {
char symbol;
enum operator_type id;
};
struct delimiter {
char symbol;
enum delimiter_type id;
};
struct keyword keywords[] = {
{"int", KW_INT},
{"char", KW_CHAR},
{"if", KW_IF},
{"else", KW_ELSE},
{"while", KW_WHILE},
{"return", KW_RETURN},
{NULL, UNKNOWN}
};
struct operator operators[] = {
{'=', OP_ASSIGN},
{'==', OP_EQ},
{'!=', OP_NE},
{'<', OP_LT},
{'>', OP_GT},
{'<=', OP_LE},
{'>=', OP_GE},
{'+', OP_ADD},
{'-', OP_SUB},
{'*', OP_MUL},
{'/', OP_DIV},
{'%', OP_MOD},
{0, UNKNOWN}
};
struct delimiter delimiters[] = {
{';', DEL_SEMICOLON},
{',', DEL_COMMA},
{'(', DEL_LPAREN},
{')', DEL_RPAREN},
{'{', DEL_LBRACE},
{'}', DEL_RBRACE},
{0, UNKNOWN}
};
int is_keyword(char *s) {
for (int i = 0; keywords[i].name != NULL; i++) {
if (strcmp(s, keywords[i].name) == 0) {
return 1;
}
}
return 0;
}
int is_operator(char c) {
for (int i = 0; operators[i].symbol != 0; i++) {
if (c == operators[i].symbol) {
return 1;
}
}
return 0;
}
int is_delimiter(char c) {
for (int i = 0; delimiters[i].symbol != 0; i++) {
if (c == delimiters[i].symbol) {
return 1;
}
}
return 0;
}
int is_alpha(char c) {
return isalpha(c) || c == '_';
}
int is_alnum(char c) {
return isalnum(c) || c == '_';
}
void skip_whitespace(char **p) {
while (isspace(**p)) {
(*p)++;
}
}
void skip_comment(char **p) {
if (**p == '/' && *(*p + 1) == '/') {
while (**p != '\n' && **p != '\0') {
(*p)++;
}
}
}
struct token get_keyword(char **p) {
struct token t = {"", KEYWORD};
int i = 0;
while (is_alpha(**p)) {
t.lexeme[i++] = **p;
(*p)++;
}
t.lexeme[i] = '\0';
if (!is_keyword(t.lexeme)) {
t.type = IDENTIFIER;
}
return t;
}
struct token get_operator(char **p) {
struct token t = {"", OPERATOR};
char symbol[3] = "";
symbol[0] = **p;
(*p)++;
if (is_operator(**p)) {
symbol[1] = **p;
(*p)++;
}
for (int i = 0; operators[i].symbol != 0; i++) {
if (strcmp(symbol, operators[i].symbol) == 0) {
t.type = OPERATOR;
return t;
}
}
t.type = UNKNOWN;
return t;
}
struct token get_delimiter(char **p) {
struct token t = {"", DELIMITER};
t.lexeme[0] = **p;
(*p)++;
for (int i = 0; delimiters[i].symbol != 0; i++) {
if (t.lexeme[0] == delimiters[i].symbol) {
t.type = DELIMITER;
return t;
}
}
t.type = UNKNOWN;
return t;
}
struct token get_constant(char **p) {
struct token t = {"", CONSTANT};
int i = 0;
while (isdigit(**p)) {
t.lexeme[i++] = **p;
(*p)++;
}
t.lexeme[i] = '\0';
return t;
}
struct token get_token(char **p) {
struct token t = {"", UNKNOWN};
skip_whitespace(p);
skip_comment(p);
if (**p == '\0') {
t.type = UNKNOWN;
return t;
}
if (is_alpha(**p)) {
t = get_keyword(p);
} else if (isdigit(**p)) {
t = get_constant(p);
} else if (is_operator(**p)) {
t = get_operator(p);
} else if (is_delimiter(**p)) {
t = get_delimiter(p);
} else {
(*p)++;
}
return t;
}
int main() {
char code[] = "int main() { char c=56; int a=1; int bb_b1=7; if(a>bb_b1)c=9; else c=90; while(1)";
char *p = code;
struct token t;
do {
t = get_token(&p);
switch (t.type) {
case KEYWORD:
printf("<KEYWORD,%d,%s>\n", keywords[t.type].id, t.lexeme);
break;
case IDENTIFIER:
printf("<IDENTIFIER,%s>\n", t.lexeme);
break;
case CONSTANT:
printf("<CONSTANT,%s>\n", t.lexeme);
break;
case OPERATOR:
printf("<OPERATOR,%d,%s>\n", operators[t.type].id, t.lexeme);
break;
case DELIMITER:
printf("<DELIMITER,%d,%s>\n", delimiters[t.type].id, t.lexeme);
break;
case UNKNOWN:
printf("<UNKNOWN>\n");
break;
}
} while (t.type != UNKNOWN);
return 0;
}
```
输出结果如下:
```
<KEYWORD,0,int>
<IDENTIFIER,main>
<DELIMITER,2,(>
<DELIMITER,3,)>
<DELIMITER,4,{>
<KEYWORD,1,char>
<IDENTIFIER,c>
<OPERATOR,0,=>
<CONSTANT,56>
<DELIMITER,0,;>
<KEYWORD,0,int>
<IDENTIFIER,a>
<OPERATOR,0,=>
<CONSTANT,1>
<DELIMITER,0,;>
<KEYWORD,0,int>
<IDENTIFIER,bb_b1>
<OPERATOR,0,=>
<CONSTANT,7>
<DELIMITER,0,;>
<KEYWORD,2,if>
<DELIMITER,2,(>
<IDENTIFIER,a>
<OPERATOR,3,>
<IDENTIFIER,bb_b1>
<DELIMITER,3,)>
<IDENTIFIER,c>
<OPERATOR,0,=>
<CONSTANT,9>
<DELIMITER,0,;>
<KEYWORD,3,else>
<IDENTIFIER,c>
<OPERATOR,0,=>
<CONSTANT,90>
<DELIMITER,0,;>
<KEYWORD,4,while>
<DELIMITER,2,(>
<CONSTANT,1>
<UNKNOWN>
```
阅读全文