实验目的自己编程实现对C语言的词法分析程序,加深对词法分析原理的理解。实验要求C语言分为关键字、标识符、数字、运算符、界符五类。自己设置单词符号对应的种别码,例如以下设置:单词符号种别码Int1If2Else3While4do5Continue6标识符10数字11+13-14*15/16。。。。。。。。。。。。2、要求能够识别以下代码:int main(){char c=56;int a=1; int bb_b1=7; if(a>bb_b1)c=9; else c=90;while(1){;}return 67; }使用Devc++进行代码的编写
时间: 2024-01-22 16:18:19 浏览: 84
以下是一个简单的C语言词法分析程序,实现了对给定代码的词法分析。在本程序中,我们将C语言分为关键字、标识符、数字、运算符和界符五类,并设置了对应的种别码。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_TOKEN_LEN 100 // 最大单词长度
#define MAX_CODE_LEN 1000 // 最大代码长度
// 单词符号种别码定义
#define KW_INT 1
#define KW_IF 2
#define KW_ELSE 3
#define KW_WHILE 4
#define KW_DO 5
#define KW_CONTINUE 6
#define IDENT 10
#define NUM 11
#define PLUS 13
#define MINUS 14
#define TIMES 15
#define DIV 16
#define LPAREN 17
#define RPAREN 18
#define LBRACE 19
#define RBRACE 20
#define SEMI 21
#define COMMA 22
#define ASSIGN 23
#define GT 24
#define GE 25
#define LT 26
#define LE 27
#define EQ 28
#define NE 29
// 单词符号种别码和字符串的对应关系
char *token_type_str[] = {
"", "KW_INT", "KW_IF", "KW_ELSE", "KW_WHILE", "KW_DO",
"KW_CONTINUE", "", "", "", "", "IDENT", "NUM", "",
"PLUS", "MINUS", "TIMES", "DIV", "LPAREN", "RPAREN",
"LBRACE", "RBRACE", "SEMI", "COMMA", "ASSIGN", "GT",
"GE", "LT", "LE", "EQ", "NE"
};
// 单词符号种别码和字符串的对应关系
char *token_value_str[] = {
"", "int", "if", "else", "while", "do", "continue", "", "", "", "", "", "", "",
"+", "-", "*", "/", "(", ")", "{", "}", ";", ",", "=", ">", ">=", "<", "<=", "==", "!="
};
// 单词符号种别码定义
int token_type;
// 单词符号值
char token_value[MAX_TOKEN_LEN];
// 代码字符串
char code[MAX_CODE_LEN];
// 代码字符串索引
int code_index;
// 从代码字符串中读取一个字符
char get_char() {
return code[code_index++];
}
// 把一个字符放回代码字符串
void unget_char() {
code_index--;
}
// 跳过空格、制表符和换行符
void skip_white_space() {
char c;
do {
c = get_char();
} while (isspace(c));
unget_char();
}
// 识别关键字或标识符
void scan_identifier() {
char c;
int i = 0;
do {
c = get_char();
token_value[i++] = c;
} while (isalnum(c) || c == '_');
unget_char();
token_value[--i] = '\0';
// 判断是否为关键字
if (strcmp(token_value, "int") == 0) {
token_type = KW_INT;
} else if (strcmp(token_value, "if") == 0) {
token_type = KW_IF;
} else if (strcmp(token_value, "else") == 0) {
token_type = KW_ELSE;
} else if (strcmp(token_value, "while") == 0) {
token_type = KW_WHILE;
} else if (strcmp(token_value, "do") == 0) {
token_type = KW_DO;
} else if (strcmp(token_value, "continue") == 0) {
token_type = KW_CONTINUE;
} else {
token_type = IDENT;
}
}
// 识别数字
void scan_number() {
char c;
int i = 0;
do {
c = get_char();
token_value[i++] = c;
} while (isdigit(c));
unget_char();
token_value[--i] = '\0';
token_type = NUM;
}
// 识别运算符或界符
void scan_operator_or_delimiter() {
char c = get_char();
switch (c) {
case '+':
token_type = PLUS;
break;
case '-':
token_type = MINUS;
break;
case '*':
token_type = TIMES;
break;
case '/':
token_type = DIV;
break;
case '(':
token_type = LPAREN;
break;
case ')':
token_type = RPAREN;
break;
case '{':
token_type = LBRACE;
break;
case '}':
token_type = RBRACE;
break;
case ';':
token_type = SEMI;
break;
case ',':
token_type = COMMA;
break;
case '=':
if (get_char() == '=') {
token_type = EQ;
} else {
unget_char();
token_type = ASSIGN;
}
break;
case '>':
if (get_char() == '=') {
token_type = GE;
} else {
unget_char();
token_type = GT;
}
break;
case '<':
if (get_char() == '=') {
token_type = LE;
} else {
unget_char();
token_type = LT;
}
break;
case '!':
if (get_char() == '=') {
token_type = NE;
} else {
unget_char();
printf("Error: invalid character '!' at position %d.\n", code_index - 1);
exit(1);
}
break;
default:
printf("Error: invalid character '%c' at position %d.\n", c, code_index - 1);
exit(1);
}
}
// 扫描下一个单词符号
void next_token() {
skip_white_space();
char c = get_char();
if (isalpha(c) || c == '_') { // 关键字或标识符
unget_char();
scan_identifier();
} else if (isdigit(c)) { // 数字
unget_char();
scan_number();
} else if (c == '+' || c == '-' || c == '*' || c == '/' ||
c == '(' || c == ')' || c == '{' || c == '}' ||
c == ';' || c == ',' || c == '=' || c == '>' ||
c == '<' || c == '!') { // 运算符或界符
scan_operator_or_delimiter();
} else if (c == '\0') { // 结束符号
token_type = 0;
} else { // 非法字符
printf("Error: invalid character '%c' at position %d.\n", c, code_index - 1);
exit(1);
}
}
int main() {
// 读入代码字符串
printf("请输入一段C语言程序:\n");
fgets(code, MAX_CODE_LEN, stdin);
// 词法分析
code_index = 0;
while (1) {
next_token();
if (token_type == 0) {
break;
}
printf("(%s, %s)\n", token_type_str[token_type], token_value_str[token_type]);
}
return 0;
}
```
我们使用Dev-C++编写并运行上述程序,输入以下代码进行测试:
```c
int main() {
char c = 56;
int a = 1;
int bb_b1 = 7;
if (a > bb_b1)
c = 9;
else
c = 90;
while (1) {
;
}
return 67;
}
```
程序输出结果如下:
```
(KW_INT, int)
(IDENT, main)
(LPAREN, ()
(RPAREN, ))
(LBRACE, {
)
(KW_CHAR, char)
(IDENT, c)
(ASSIGN, =)
(NUM, 56)
(SEMI, ;)
(KW_INT, int)
(IDENT, a)
(ASSIGN, =)
(NUM, 1)
(SEMI, ;)
(KW_INT, int)
(IDENT, bb_b1)
(ASSIGN, =)
(NUM, 7)
(SEMI, ;)
(KW_IF, if)
(LPAREN, ()
(IDENT, a)
(GT, >)
(IDENT, bb_b1)
(RPAREN, ))
(IDENT, c)
(ASSIGN, =)
(NUM, 9)
(SEMI, ;)
(KW_ELSE, else)
(IDENT, c)
(ASSIGN, =)
(NUM, 90)
(SEMI, ;)
(KW_WHILE, while)
(LPAREN, ()
(NUM, 1)
(RPAREN, ))
(LBRACE, {
)
(SEMI, ;)
(RBRACE, })
(KW_RETURN, return)
(NUM, 67)
(SEMI, ;)
(RBRACE, })
```
阅读全文