返回列表 回复 发帖

编译原理之词法分析

什么是词法分析?
词法分析就是把一门语言中的各个部分区分出来.单独用表示出来.不啰嗦了,上代码.
大概就是这样吧,剩下的看工程吧,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);}

lexis.zip (28.41 KB)

返回列表