解决词法分析token错误的问题

This commit is contained in:
ranchuan
2024-11-28 19:11:39 +08:00
parent c9874d1eaa
commit 4b2011f336
4 changed files with 89 additions and 14 deletions

11
main.c
View File

@@ -12,11 +12,12 @@
const char g_str[ ] = "int main(){\n"
"int index=1000;\n"
"while(index--){\n"
"printf(index)\n"
"}\n"
const char g_str[] =
"int main() {\n"
" int index=-1000;\n"
" while(index--){\n"
" printf(index>=0);\n"
" }\n"
"}";

View File

@@ -252,8 +252,9 @@ int lex_analysis(const char *text,int len){
lex.input_len = len;
lex_get_next(&lex);
lex.current_line_pos = 0;
while(in_loop){
switch (lex.current_c)
while (in_loop) {
int _char = lex.current_c;
switch (_char)
{
case 0: {
in_loop = 0;
@@ -289,7 +290,7 @@ int lex_analysis(const char *text,int len){
case ':':
{
lex_save_char(&lex);
lex.token_buff.token = lex.current_c;
lex.token_buff.token = _char;
lex_save_token(&lex);
lex_get_next(&lex);
break;
@@ -322,7 +323,7 @@ int lex_analysis(const char *text,int len){
lex_get_next(&lex);
lex.token_buff.token = TOKEN_INC;
}else{
lex.token_buff.token = lex.current_c;
lex.token_buff.token = _char;
}
lex_save_token(&lex);
break;
@@ -335,7 +336,7 @@ int lex_analysis(const char *text,int len){
lex_get_next(&lex);
lex.token_buff.token = TOKEN_DEC;
}else{
lex.token_buff.token = lex.current_c;
lex.token_buff.token = _char;
}
lex_save_token(&lex);
break;
@@ -343,15 +344,24 @@ int lex_analysis(const char *text,int len){
case '!':
case '>':
case '<':
case '=':{
case '=': {
lex_save_char(&lex);
lex_get_next(&lex);
if(lex.current_c=='='){
lex_save_char(&lex);
lex_get_next(&lex);
lex.token_buff.token = TOKEN_EQ;
switch (_char)
{
case '!':lex.token_buff.token = TOKEN_NEQ;break;
case '>':lex.token_buff.token = TOKEN_GEQ;break;
case '<':lex.token_buff.token = TOKEN_LEQ;break;
case '=':lex.token_buff.token = TOKEN_EQ;break;
default:
DBG_ERR("未知的情况('%c'): 在 %d 行" , lex.current_c , lex.current_line);
break;
}
}else{
lex.token_buff.token = lex.current_c;
lex.token_buff.token = _char;
}
lex_save_token(&lex);
break;
@@ -380,7 +390,7 @@ int lex_analysis(const char *text,int len){
int token_len=0;
token_def *tlist=lex_cpy_token_list(&lex , &token_len);
lex_del_token_list(&lex);
par_parser(tlist , token_len);
// par_parser(tlist , token_len);
mem_free(tlist);
DBG_LOG("解析结束.");
return 0;

9
soft/parser.c Normal file
View File

@@ -0,0 +1,9 @@
// #include "parser.h"

55
soft/parser.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef parser_h__
#define parser_h__
// c语言语义分析
// 变量类型
typedef enum {
VTYPE_VOID = 0 ,
VTYPE_CHAR = 1 ,
VTYPE_SHORT = 2 ,
VTYPE_INT = 3 ,
VTYPE_PTR = 4 ,
VTYPE_FUNC = 5 ,
}variable_type_def;
// 语句类型
typedef enum {
STYPE_NONE = 0,// 代码文件
STYPE_TYPEDEF = 1 ,// 类型定义 typedef
STYPE_VDECLARE = 2 ,// 变量声明
STYPE_FDECLARE = 3 ,// 函数声明
STYPE_STRUCT = 4 ,// 结构体语句
STYPE_ENUM = 5 ,// 枚举语句
STYPE_UNION = 6 ,// 联合体语句
STYPE_FDEF = 7 ,// 函数定义
STYPE_PARLIST = 8 ,// 参数列表
STYPE_BLOCK = 9,// 代码块
}sentence_type_def;
// 属性类型 可以组合
typedef enum {
ATYPE_VOID = 0 ,// 无属性
ATYPE_STATIC = 1 ,// 静态
ATYPE_CONST = 2 ,//只读
ATYPE_EXTERN = 4 ,// 外部
ATYPE_UNSIGNED = 8,// 无符号
}attr_def;
struct token_def;
typedef struct _par_node {
int sentence_type;
int attr_type;
token_def* token_list;
int token_len;
struct _par_node* next;
struct _par_node* chid;
char name[1024];
}par_node_def;
#endif