LEX是词法分析的主要对象,其结构体在st_lex(sql_lex.h),在THD中,有一个字段thd->lex指向该结构体。
在mysqld启动时初始化LEX(在函数init_common_variables内调用lex_init()),对sql/lex.h中的两个关键字数组symbols和sql_functions进行初始化
这两个关键字数组的结构体为SYMBOL:
1: struct st_sym_group;
2:
3: typedef struct st_symbol {
4: const char *name;
5: uint tok;
6: uint length;
7: struct st_sym_group *group;
8: } SYMBOL;
9:
10: typedef struct st_lex_symbol
11: {
12: SYMBOL *symbol;
13: char *str;
14: uint length;
15: } LEX_SYMBOL;
16:
17: typedef struct st_sym_group {
18: const char *name;
19: const char *needed_define;
20: } SYM_GROUP;
<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->
初始化主要是对SYMBOL的length字段赋值。
当需要解析一个SQL语句时,调用函数:
1: void lex_start(THD *thd)
<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->
该函数将对lex对象进行设置(sql_lex.cc)。
在sql_lex.cc中定义了很多对lex操作的函数,如find_keyword、lex_end、lex_free等