添加 c语言词法分析 初步
This commit is contained in:
113
soft/clexical.c
Normal file
113
soft/clexical.c
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user