c语言写词法分析器的源代码
时间: 2023-10-28 17:06:31 浏览: 80
很高兴回答您的问题。以下是C语言写词法分析器的示例代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_TOKEN_LENGTH 100
typedef enum {
END_OF_FILE,
OPERATOR,
SEPARATOR,
IDENTIFIER,
INT_LITERAL,
FLOAT_LITERAL,
STRING_LITERAL,
CHAR_LITERAL
} TokenType;
typedef struct {
char str[MAX_TOKEN_LENGTH+1];
TokenType type;
} Token;
void print_token(Token token) {
switch (token.type) {
case END_OF_FILE:
printf("End of file\n");
break;
case OPERATOR:
printf("Operator: %s\n", token.str);
break;
case SEPARATOR:
printf("Separator: %s\n", token.str);
break;
case IDENTIFIER:
printf("Identifier: %s\n", token.str);
break;
case INT_LITERAL:
printf("Integer literal: %s\n", token.str);
break;
case FLOAT_LITERAL:
printf("Float literal: %s\n", token.str);
break;
case STRING_LITERAL:
printf("String literal: %s\n", token.str);
break;
case CHAR_LITERAL:
printf("Character literal: %s\n", token.str);
break;
default:
printf("Unknown token\n");
break;
}
}
Token get_token(FILE *fp) {
Token token;
char c = fgetc(fp);
token.str[0] = c;
token.str[1] = '\0';
if (feof(fp)) {
token.type = END_OF_FILE;
return token;
}
if (ispunct(c)) {
token.type = OPERATOR;
return token;
}
if (isspace(c)) {
while (isspace(c)) {
c = fgetc(fp);
}
ungetc(c, fp);
return get_token(fp);
}
if (isdigit(c)) {
token.type = INT_LITERAL;
while (isdigit(c)) {
c = fgetc(fp);
strncat(token.str, &c, 1);
}
ungetc(c, fp);
return token;
}
if (c == '"') {
token.type = STRING_LITERAL;
c = fgetc(fp);
while (c != '"') {
strncat(token.str, &c, 1);
c = fgetc(fp);
}
return token;
}
if (c == '\'') {
token.type = CHAR_LITERAL;
c = fgetc(fp);
strncat(token.str, &c, 1);
c = fgetc(fp);
if (c == '\'') {
return token;
}
}
if (isalpha(c) || c == '_') {
token.type = IDENTIFIER;
while (isalnum(c) || c == '_') {
c = fgetc(fp);
strncat(token.str, &c, 1);
}
ungetc(c, fp);
return token;
}
token.type = SEPARATOR;
return token;
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error: could not open file '%s'\n", argv[1]);
return 1;
}
Token token;
do {
token = get_token(fp);
print_token(token);
} while (token.type != END_OF_FILE);
fclose(fp);
return 0;
}
阅读全文