minic编译器 词法分析器识别所有minic文法终结符
时间: 2023-12-23 08:02:00 浏览: 88
下面是 minic 文法的终结符列表:
```
IDENTIFIER
CONSTANT
STRING_LITERAL
SIZEOF
PTR_OP
INC_OP
DEC_OP
LEFT_OP
RIGHT_OP
LE_OP
GE_OP
EQ_OP
NE_OP
AND_OP
OR_OP
MUL_ASSIGN
DIV_ASSIGN
MOD_ASSIGN
ADD_ASSIGN
SUB_ASSIGN
LEFT_ASSIGN
RIGHT_ASSIGN
AND_ASSIGN
XOR_ASSIGN
OR_ASSIGN
TYPE_NAME
ENUMERATION_CONSTANT
EXTERN
STATIC
AUTO
REGISTER
CHAR
SHORT
INT
LONG
SIGNED
UNSIGNED
FLOAT
DOUBLE
CONST
VOLATILE
VOID
STRUCT
UNION
ENUM
ELIPSIS
CASE
DEFAULT
IF
ELSE
SWITCH
WHILE
DO
FOR
GOTO
CONTINUE
BREAK
RETURN
```
相关问题
minic编译器词法分析器
minic编译器的词法分析器主要用来将源代码文本转换为单词序列,每个单词代表程序中的一个符号。下面是一个简单的minic编译器词法分析器的示例:
```c
#include <stdio.h>
#include <ctype.h>
#define MAX_TOKEN_LEN 100
typedef enum {
T_EOF,
T_PLUS,
T_MINUS,
T_MULTIPLY,
T_DIVIDE,
T_ASSIGN,
T_SEMICOLON,
T_LPAREN,
T_RPAREN,
T_IDENT,
T_NUMBER
} TokenType;
typedef struct {
TokenType type;
char value[MAX_TOKEN_LEN + 1];
} Token;
Token get_token() {
Token token = {T_EOF, ""};
int i = 0;
char c = getchar();
while (isspace(c)) {
c = getchar();
}
if (c == '+') {
token.type = T_PLUS;
} else if (c == '-') {
token.type = T_MINUS;
} else if (c == '*') {
token.type = T_MULTIPLY;
} else if (c == '/') {
token.type = T_DIVIDE;
} else if (c == '=') {
token.type = T_ASSIGN;
} else if (c == ';') {
token.type = T_SEMICOLON;
} else if (c == '(') {
token.type = T_LPAREN;
} else if (c == ')') {
token.type = T_RPAREN;
} else if (isalpha(c)) {
token.type = T_IDENT;
token.value[i++] = c;
c = getchar();
while (isalnum(c) && i < MAX_TOKEN_LEN) {
token.value[i++] = c;
c = getchar();
}
ungetc(c, stdin);
} else if (isdigit(c)) {
token.type = T_NUMBER;
token.value[i++] = c;
c = getchar();
while (isdigit(c) && i < MAX_TOKEN_LEN) {
token.value[i++] = c;
c = getchar();
}
ungetc(c, stdin);
}
token.value[i] = '\0';
return token;
}
int main() {
Token token;
do {
token = get_token();
printf("Token: type=%d, value=%s\n", token.type, token.value);
} while (token.type != T_EOF);
return 0;
}
```
该词法分析器根据minic编译器的语法规则,识别出了 +、-、*、/、=、;、(、)、标识符和数字这些单词,并将它们分别以相应的类型和值存储在Token结构体中。这些Token可以进一步被用于minic编译器的语法分析和代码生成等阶段。
阅读全文