C语言代码的语法分析实现代码
时间: 2023-10-26 11:28:41 浏览: 79
C语言的语法分析可以使用工具如Flex和Bison来实现,也可以手写代码实现。以下是手写代码实现的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKEN_LEN 100
// Token类型
typedef enum {
TOKEN_INT,
TOKEN_FLOAT,
TOKEN_PLUS,
TOKEN_MINUS,
TOKEN_MULTIPLY,
TOKEN_DIVIDE,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_END
} TokenType;
// Token结构体
typedef struct {
TokenType type;
char value[MAX_TOKEN_LEN];
} Token;
// TokenStream结构体
typedef struct {
Token tokens[MAX_TOKEN_LEN];
int pos;
} TokenStream;
// 词法分析函数
void lex(char* input, TokenStream* ts)
{
int i = 0, j = 0;
while (input[i] != '\0') {
if (input[i] == '+') {
ts->tokens[j].type = TOKEN_PLUS;
strcpy(ts->tokens[j].value, "+");
j++;
} else if (input[i] == '-') {
ts->tokens[j].type = TOKEN_MINUS;
strcpy(ts->tokens[j].value, "-");
j++;
} else if (input[i] == '*') {
ts->tokens[j].type = TOKEN_MULTIPLY;
strcpy(ts->tokens[j].value, "*");
j++;
} else if (input[i] == '/') {
ts->tokens[j].type = TOKEN_DIVIDE;
strcpy(ts->tokens[j].value, "/");
j++;
} else if (input[i] == '(') {
ts->tokens[j].type = TOKEN_LPAREN;
strcpy(ts->tokens[j].value, "(");
j++;
} else if (input[i] == ')') {
ts->tokens[j].type = TOKEN_RPAREN;
strcpy(ts->tokens[j].value, ")");
j++;
} else if (isdigit(input[i])) {
int k = i;
while (isdigit(input[k])) {
k++;
}
if (input[k] == '.') {
k++;
while (isdigit(input[k])) {
k++;
}
ts->tokens[j].type = TOKEN_FLOAT;
} else {
ts->tokens[j].type = TOKEN_INT;
}
strncpy(ts->tokens[j].value, &input[i], k-i);
ts->tokens[j].value[k-i] = '\0';
j++;
i = k-1;
}
i++;
}
ts->tokens[j].type = TOKEN_END;
}
// 语法分析函数
double parse_expression(TokenStream* ts);
double parse_primary(TokenStream* ts)
{
double result = 0.0;
if (ts->tokens[ts->pos].type == TOKEN_INT) {
result = atoi(ts->tokens[ts->pos].value);
ts->pos++;
} else if (ts->tokens[ts->pos].type == TOKEN_FLOAT) {
result = atof(ts->tokens[ts->pos].value);
ts->pos++;
} else if (ts->tokens[ts->pos].type == TOKEN_LPAREN) {
ts->pos++;
result = parse_expression(ts);
if (ts->tokens[ts->pos].type != TOKEN_RPAREN) {
printf("Error: expected )\n");
exit(1);
}
ts->pos++;
} else {
printf("Error: expected number or (\n");
exit(1);
}
return result;
}
double parse_term(TokenStream* ts)
{
double left = parse_primary(ts);
while (ts->tokens[ts->pos].type == TOKEN_MULTIPLY || ts->tokens[ts->pos].type == TOKEN_DIVIDE) {
if (ts->tokens[ts->pos].type == TOKEN_MULTIPLY) {
ts->pos++;
left *= parse_primary(ts);
} else if (ts->tokens[ts->pos].type == TOKEN_DIVIDE) {
ts->pos++;
left /= parse_primary(ts);
}
}
return left;
}
double parse_expression(TokenStream* ts)
{
double left = parse_term(ts);
while (ts->tokens[ts->pos].type == TOKEN_PLUS || ts->tokens[ts->pos].type == TOKEN_MINUS) {
if (ts->tokens[ts->pos].type == TOKEN_PLUS) {
ts->pos++;
left += parse_term(ts);
} else if (ts->tokens[ts->pos].type == TOKEN_MINUS) {
ts->pos++;
left -= parse_term(ts);
}
}
return left;
}
int main()
{
char input[100];
TokenStream ts;
printf("Enter an expression: ");
fgets(input, sizeof(input), stdin);
input[strlen(input)-1] = '\0';
lex(input, &ts);
double result = parse_expression(&ts);
printf("Result: %f\n", result);
return 0;
}
```
该示例实现了一个简单的四则运算表达式解析器,包括词法分析和语法分析两个部分。程序首先读入一个表达式,然后使用lex函数将其转换为TokenStream。接着,程序调用parse_expression函数对表达式进行语法分析并计算结果。parse_expression函数内部调用parse_term和parse_primary函数来解析表达式的项和因子。如果在解析过程中发现错误,程序将输出错误信息并退出。
阅读全文