LL(1)语法分析器C语言代码
时间: 2023-10-11 14:13:44 浏览: 164
ll(1).rar_LL语法分析_语法分析器
以下是一个简单的LL(1)语法分析器的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_TOKEN_LENGTH 100
typedef enum {
TOKEN_INVALID,
TOKEN_PLUS,
TOKEN_MINUS,
TOKEN_MULTIPLY,
TOKEN_DIVIDE,
TOKEN_LEFT_PAREN,
TOKEN_RIGHT_PAREN,
TOKEN_NUMBER,
TOKEN_END_OF_INPUT
} TokenType;
typedef struct {
TokenType type;
char value[MAX_TOKEN_LENGTH];
} Token;
void error(char *message) {
printf("Error: %s\n", message);
exit(EXIT_FAILURE);
}
Token next_token() {
static char input[MAX_TOKEN_LENGTH];
static int input_index = 0;
static char current_char = ' ';
Token token = {TOKEN_INVALID, {0}};
while (isspace(current_char)) {
current_char = getchar();
}
if (isdigit(current_char)) {
token.type = TOKEN_NUMBER;
int i = 0;
while (isdigit(current_char) && i < MAX_TOKEN_LENGTH) {
token.value[i++] = current_char;
current_char = getchar();
}
token.value[i] = '\0';
return token;
}
switch (current_char) {
case '+':
token.type = TOKEN_PLUS;
current_char = getchar();
break;
case '-':
token.type = TOKEN_MINUS;
current_char = getchar();
break;
case '*':
token.type = TOKEN_MULTIPLY;
current_char = getchar();
break;
case '/':
token.type = TOKEN_DIVIDE;
current_char = getchar();
break;
case '(':
token.type = TOKEN_LEFT_PAREN;
current_char = getchar();
break;
case ')':
token.type = TOKEN_RIGHT_PAREN;
current_char = getchar();
break;
case '\n':
case EOF:
token.type = TOKEN_END_OF_INPUT;
break;
default:
error("Invalid character");
}
return token;
}
Token current_token;
void match(TokenType expected_type) {
if (current_token.type == expected_type) {
current_token = next_token();
} else {
error("Unexpected token");
}
}
int factor() {
Token token = current_token;
int value = 0;
if (token.type == TOKEN_NUMBER) {
value = atoi(token.value);
match(TOKEN_NUMBER);
} else if (token.type == TOKEN_LEFT_PAREN) {
match(TOKEN_LEFT_PAREN);
value = expression();
match(TOKEN_RIGHT_PAREN);
} else {
error("Unexpected token");
}
return value;
}
int term() {
int value = factor();
while (current_token.type == TOKEN_MULTIPLY || current_token.type == TOKEN_DIVIDE) {
if (current_token.type == TOKEN_MULTIPLY) {
match(TOKEN_MULTIPLY);
value *= factor();
} else if (current_token.type == TOKEN_DIVIDE) {
match(TOKEN_DIVIDE);
value /= factor();
}
}
return value;
}
int expression() {
int value = term();
while (current_token.type == TOKEN_PLUS || current_token.type == TOKEN_MINUS) {
if (current_token.type == TOKEN_PLUS) {
match(TOKEN_PLUS);
value += term();
} else if (current_token.type == TOKEN_MINUS) {
match(TOKEN_MINUS);
value -= term();
}
}
return value;
}
int main() {
printf("Enter an expression: ");
fflush(stdout);
current_token = next_token();
int result = expression();
if (current_token.type != TOKEN_END_OF_INPUT) {
error("Unexpected token");
}
printf("Result: %d\n", result);
return 0;
}
```
上述代码实现了一个简单的四则运算表达式的LL(1)语法分析器。它通过递归调用来解析表达式,并使用一个静态的`next_token()`函数来获取下一个令牌。`match()`函数用于检查当前令牌是否符合预期,并将当前令牌设置为下一个令牌。在`factor()`、`term()`和`expression()`函数中,它们分别实现了因子、项和表达式的解析过程。最后,在`main()`函数中,它从标准输入中获取表达式,并输出结果。
阅读全文