From 93a8c2c265cec8db0c599d2d46621c294f571e7d Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Tue, 29 Oct 2024 00:02:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20c=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E8=AF=8D=E6=B3=95=E5=88=86=E6=9E=90=20=E5=88=9D=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make.py | 3 +- soft/clexical.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 soft/clexical.c diff --git a/make.py b/make.py index 73d79ed..66ba86c 100644 --- a/make.py +++ b/make.py @@ -152,7 +152,8 @@ def build_object(src:list[str]): print(f"{i} 没有更新依赖关系") elif(file_type in ['s','S','asm','ASM']): if(check_rebuild(dst,[i])): - cmd=f"{AS} -c {i} -o {dst} {flags}" + # cmd=f"{AS} -c {i} -o {dst} {flags}" + pass else: print(f"{i} 没有更新依赖关系") if(len(cmd)>0): diff --git a/soft/clexical.c b/soft/clexical.c new file mode 100644 index 0000000..a6efc2a --- /dev/null +++ b/soft/clexical.c @@ -0,0 +1,113 @@ +#include "stdio.h" +#include "regex.h" +#include "stdlib.h" +#include "stdint.h" +#include "exception.h" +#include "mythread.h" +#include "unistd.h" +#include "debug.h" + + +// c语言词法分析 + + + + +const char *g_keyword_table[]={ + "if","break","while","switch","case","do", + "char","int","void", +}; + +typedef enum { + TOKEN_IF = 256, + TOKEN_BREAK, + TOKEN_WHILE, + TOKEN_SWITCH, + TOKEN_CASE, + TOKEN_DO, + TOKEN_CHAR, + TOKEN_INT, + TOKEN_VOID, + TOKEN_NAME, +}token_def; + +#define TOKEN_BUFF_MAX_LEN 128 + +typedef struct{ + char buff[TOKEN_BUFF_MAX_LEN]; + int used; + int line; + int pos; + int token; +}token_def; + +typedef struct{ + int current_c; + int current_line; + int current_line_pos; + token_def token_buff; + char *input_text; + int input_len; + int input_pos; +}lex_def; + + +// 获取下一个字符 返回0成功 +int lex_get_next(lex_def *lex) +{ + token_def *t=&lex->token_buff; + if(lex->input_pos>=lex->input_len){ + return -1; + } + lex->current_c=lex->input_text[lex->input_pos]; + lex->input_pos++; + lex->current_line_pos++; + return 0; +} + + +// 保存当前字符 +int lex_save_char(lex_def *lex){ + token_def *t=&lex->token_buff; + if(t->used>=TOKEN_BUFF_MAX_LEN){ + return -1; + } + t->buff[t->used]=lex->current_c; + t->used++; + t->line=lex->current_line; + t->pos=lex->current_line_pos; + return 0; +} + + + + +int lex_analysis(const char *text,int len){ + lex_def lex={0}; + lex.input_text=text; + lex.input_len=len; + lex_get_next(&lex); + while(1){ + switch (lex.current_c) + { + case '\r':{ + lex_get_next(&lex); + if(lex.current_c=='\n'){ + lex_get_next(&lex); + } + lex.current_line++; + lex.current_line_pos=0; + break; + } + case ' ':case '\t':case '\v':case '\f':{ + lex_get_next(&lex); + lex.current_line_pos++; + } + default: + break; + } + } +} + + +