首页
社区
课程
招聘
[原创]编译原理之词法分析::勿忘国耻 9.18事变::
发表于: 2012-9-18 13:31 10920

[原创]编译原理之词法分析::勿忘国耻 9.18事变::

2012-9-18 13:31
10920
RT,今天是特殊纪念日,赶时间,简单啰嗦几句.
什么是词法分析?
词法分析就是把一门语言中的各个部分区分出来.单独用表示出来.不啰嗦了,上代码.
大概就是这样吧,剩下的看工程吧,example是测试的例子.简单一些示例代码.
源码标签
typedef struct __token 
{
	char * name;
	short id;
}var_token;
extern const var_token ext_ecc_token[MAX_TOKEN];
extern const var_token ext_ecc_pp[MAX_PP_TOKEN];

注释和类型.
/*Abstract description of a linked list*/
typedef struct ecc_ast_alloc
{
/*token word list*/
    short type_id;
	char* p_value; 
	short value_len;
	struct ecc_ast_alloc * front;
	struct ecc_ast_alloc * after;
}ecc_ast;
typedef struct __ecc_comment
{
	char * src_comment;
	short  src_len;
	short  type;
	struct __ecc_comment * front;
	struct __ecc_comment * after;
}ecc_comment_list;
导出变量.
extern ecc_comment_list * ext_comment;
extern ecc_ast * ext_ecc_ast;

看下标记的枚举类型.
enum __enum_token_
{
    token_auto = 0x500,
	token_enum,
	token_unsigned, 
    token_break,
	token_extern,
	token_return,
	token_void, 
    token_case,
	token_float,
	token_short,
    token_char,
	token_for,
	token_signed,
	token_while, 
    token_const,
	token_goto,
	token_sizeof,
	token_bool, 
    token_continue,
	token_if,
	token_static,
    token_default,
	token_inline,
	token_struct,
    token_do,
	token_int,
	token_switch, 
    token_double,
	token_long,
	token_typedef, 
    token_else,
	token_union,
};
enum __enum_pp_token
{
	token_pp_define = 0x600,
	token_pp_elif,
	token_pp_else,
	token_pp_endif,
	token_pp_if,
	token_pp_ifdef,
	token_pp_ifndef,
	token_pp_include,
	token_pp_undef,
};
enum __enum_operate
{
	token_space = 0x257, /* ' ' \t */
	token_eol,           /* \n */
	token_cpp_comment,   /* C++ comment */
	token_c_comment,     /* C comment */
	token_word,          /* Word */
	token_number,        /* A number */
	token_complex_divide,/* /= */
	token_divide,        /* / */
	token_left_shift,    /* << */
	token_less_equal,    /* <= */
	token_complex_left_shift,          /* <<= */
	token_less,          /* < */
	token_right_shift,   /* >> */
	token_greater_equal, /* >= */
	token_complex_right_shift,         /* >>= */
	token_greater,       /* > */
	token_front_subtract,/* -- */
	token_ptr_member,    /* -> */
	token_complex_subtract,/* -= */
	token_subtract,      /* - */
	token_complex_add,   /* += */
	token_add,           /* + */
	token_equal,         /* == */
	token_assignment,    /* = */
	token_not_equal,     /* != */
	token_logical_not,   /* ! */
	token_complex_bitwise_not,          /* ~= */
	token_bitwise_not,   /* ~ */
	token_complex_bitwise_inclusive_or, /* |= */
	token_logical_or,    /* || */
	token_bitwise_inclusive_or,         /* | */
	token_logical_and,   /* && */
	token_complex_bitwise_and,          /* &= */
	token_bitwise_and,   /* & */
	token_complex_bitwise_exclusive_or, /* ^= */
	token_bitwise_exclusive_or,         /* ^ */
	token_complex_remainder,            /* %= */
	token_remainder,    /* % */
    token_question,         /* ? */
    token_semicolon,        /* ; */
    token_colon,            /* : */
	token_comma,            /* , */
    token_point,            /* . */
    token_lparen,			/* ( */
    token_rparen,			/* ) */
    token_larray,			/* [ */
    token_rarray,			/* ] */
    token_lbrace,			/* { */
    token_rbrace,			/* } */
	token_double_quote,     /* " */
	token_single_quote,     /* ' */
	token_backslash         /* \ */
};

需要初始化的标记.
const var_token ext_ecc_token[MAX_TOKEN]= 
{
    {"auto",token_auto},
    {"enum",token_enum},   
    {"unsigned",token_unsigned},
	{"break",token_break},   
	{"extern",token_extern},
    {"return",token_return},
    {"void",token_void}, 
	{"case",token_case},   
	{"float",token_float},   
	{"short",token_short},   
    {"char",token_char},   
	{"for",token_for},   
	{"signed",token_signed},   
	{"while",token_while}, 
    {"const",token_const},   
	{"goto",token_goto},   
	{"sizeof",token_sizeof},   
	{"bool",token_bool}, 
    {"continue",token_continue},   
	{"if",token_if},   
	{"static",token_static},   
    {"default",token_default},     
	{"struct",token_struct},   
    {"do",token_do},   
	{"int",token_int},   
	{"switch",token_switch}, 
    {"double",token_double},   
	{"long",token_long},   
	{"typedef",token_typedef}, 
    {"else",token_else},   
	{"union",token_union},
};
const var_token ext_ecc_pp[MAX_PP_TOKEN] = 
{
	{"#define",token_pp_define},
	{"#elif",token_pp_elif},
	{"#else",token_pp_else},
	{"#endif",token_pp_endif},
	{"#if",token_pp_if},
	{"#ifdef",token_pp_ifdef},
	{"#ifndef",token_pp_ifndef},
	{"#include",token_pp_include},
	{"#undef",token_pp_undef},
};

void ecc_parse(char * src,size_t len)
{
	if(NULL == (ext_ecc_ast = alloc_chunk(sizeof(ecc_ast))))
		return;
	if(NULL == (ext_comment = alloc_chunk(sizeof(ecc_comment_list))))
		return;
	g_src = src;
	do
	{
		if(ecc_preprocessor())  
			continue;
		if(ecc_keyword())    
			continue;
		if(ecc_identifier()) 
			continue;
		if(ecc_constants())  
			continue;
		if(ecc_comment())    
			continue;
	    if(ecc_operator())   
			continue;
		else
			++g_src;
	}while(*g_src);
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 6
支持
分享
最新回复 (19)
雪    币: 1905
活跃值: (1537)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
好东西。学习之。
2012-9-18 14:00
0
雪    币: 6728
活跃值: (3897)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我是来膜拜的
2012-9-18 14:50
0
雪    币: 35
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
看不懂,求指教
2012-9-18 16:20
0
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
强大   
2012-9-18 17:00
0
雪    币: 183
活跃值: (55)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
楼主最近很活跃啊
2012-9-18 17:17
0
雪    币: 107
活跃值: (326)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
各种ififif么??????????
2012-9-18 18:05
0
雪    币: 287
活跃值: (578)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
8
完全看不懂,小小韬你能不能别这么牛B
2012-9-18 18:50
0
雪    币: 408
活跃值: (156)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
9
去年为了写壳,大半年都在研究这个,现在不搞这行了,感觉生疏许多

词法分析就好像是给你一大串字符,写个程序来查字典,划分词性,标示词意,期待楼主下一篇
2012-9-18 22:10
0
雪    币: 248
活跃值: (25)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
10
思路好清晰啊……
赞一个
2012-9-18 22:44
0
雪    币: 73
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
俺也来学习学习~~
2012-9-19 00:26
0
雪    币: 7115
活跃值: (639)
能力值: (RANK:1290 )
在线值:
发帖
回帖
粉丝
12
表示只用flex的人飘过。。。
2012-9-19 08:54
0
雪    币: 1021
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
zig
13
学习~~~膜拜~~~
2012-9-19 09:28
0
雪    币: 190
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
14
现在看不懂,膜拜下大大,mark,以后再看
2012-9-19 13:26
0
雪    币: 219
活跃值: (738)
能力值: (RANK:290 )
在线值:
发帖
回帖
粉丝
15
还是mark
2012-9-19 13:51
0
雪    币: 1689
活跃值: (379)
能力值: ( LV15,RANK:440 )
在线值:
发帖
回帖
粉丝
16
才刚刚开始。
2012-9-19 18:22
0
雪    币: 324
活跃值: (26)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
17
请问这个代码表编译的时候遇到了库的问题:
fatal error C1083: 无法打开包括文件:“stdbool.h”: No such file or directory       
我使用的VS2008
2012-9-20 23:04
0
雪    币: 28
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
先膜拜下楼主,stdbool貌似是C标准把
2012-9-22 20:21
0
雪    币: 324
活跃值: (26)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
19
谢谢您的回复,因为使用VS2008开发,MSVC不包含这个头文件,同时也不支持相关的布尔操作函数,所以只是添加这个头文件没有办法解决问题,看来只能重新编写WINDOWS下的代码了
2012-9-26 00:20
0
雪    币: 122
活跃值: (72)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
20
收藏。不错。
2012-9-26 16:01
0
游客
登录 | 注册 方可回帖
返回
//