博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
理解所有解析的代码段:The Piece of Code that Understandeth All Parsing
阅读量:2031 次
发布时间:2019-04-28

本文共 3124 字,大约阅读时间需要 10 分钟。

 

#include 
#include
#include
#include
#define MAXTOKENS 100#define MAXTOKENLEN 64enum type_tag { IDENTIFIER, QUALIFIER, TYPE };struct token { char type; char string[MAXTOKENLEN];};int top=-1;struct token stack[MAXTOKENS];struct token this;#define pop stack[top--]#define push(s) stack[++top]=senum type_tag classify_string(void)/* figure out the identifier type */{ char *s = this.string; if (!strcmp(s,"const")) { strcpy(s,"read-only"); return QUALIFIER; } if (!strcmp(s,"volatile")) return QUALIFIER; if (!strcmp(s,"void")) return TYPE; if (!strcmp(s,"char")) return TYPE; if (!strcmp(s,"signed")) return TYPE; if (!strcmp(s,"unsigned")) return TYPE; if (!strcmp(s,"short")) return TYPE; if (!strcmp(s,"int")) return TYPE; if (!strcmp(s,"long")) return TYPE; if (!strcmp(s,"float")) return TYPE; if (!strcmp(s,"double")) return TYPE; if (!strcmp(s,"struct")) return TYPE; if (!strcmp(s,"union")) return TYPE; if (!strcmp(s,"enum")) return TYPE; return IDENTIFIER;}void gettoken(void) /* read next token into "this" */{ char *p = this.string; /* read past any spaces */ while ((*p = getchar()) == ' ' ) ; if (isalnum(*p)) { /* it starts with A-Z,0-9 read in identifier */ while ( isalnum(*++p=getchar()) ); ungetc(*p,stdin); *p = '\0'; this.type=classify_string(); return; } if (*p=='*') { strcpy(this.string,"pointer to"); this.type = '*'; return; } this.string[1]= '\0'; this.type = *p; return;}/* The piece of code that understandeth all parsing.*/read_to_first_identifier() { gettoken(); while (this.type!=IDENTIFIER) { push(this); gettoken(); } printf("%s is ", this.string); gettoken();}deal_with_arrays() { while (this.type=='[') { printf("array "); gettoken(); /* a number or ']' */ if (isdigit(this.string[0])) { printf("0..%d ",atoi(this.string)-1); gettoken(); /* read the ']' */ } gettoken(); /* read next past the ']' */ printf("of "); }}deal_with_function_args() { while (this.type!=')') { gettoken(); } gettoken(); printf("function returning ");}deal_with_pointers() { while ( stack[top].type== '*' ) { printf("%s ", pop.string ); }}deal_with_declarator() { /* deal with possible array/function following the identifier */ switch (this.type) { case '[' : deal_with_arrays(); break; case '(' : deal_with_function_args(); } deal_with_pointers(); /* process tokens that we stacked while reading to identifier */ while (top>=0) { if (stack[top].type == '(' ) { pop; gettoken(); /* read past ')' */ deal_with_declarator(); } else { printf("%s ",pop.string); } }}main(){ /* put tokens on stack until we reach identifier*/ read_to_first_identifier(); deal_with_declarator(); printf("\n"); return 0;}

 

转载地址:http://rbtaf.baihongyu.com/

你可能感兴趣的文章
ExtJS 实现的Web文件管理系统
查看>>
SOA架构师注意的问题
查看>>
最佳拍档:首席市场官与首席技术官
查看>>
CIO领导力必会的八大诀窍
查看>>
微软技术节(TechFest 2010)最前沿技术汇总
查看>>
SQL 操作结果集 -并集、差集、交集、结果集排序
查看>>
详解索引连接类型
查看>>
托管堆与垃圾收集
查看>>
MySQL初夜(乱码问题,命令行客户端使用)
查看>>
jQuery工具函数
查看>>
cookie
查看>>
javascript之window对象
查看>>
HttpCookie类
查看>>
(转载addone)完全使用Linux作为桌面系统 —— 使用Linux两年记 --软件列表
查看>>
wxzh001,进来看关于APACHE+PHP+MYSQL+SSL的LINUX下安装配置(转自奥索)
查看>>
google app api相关(商用)
查看>>
linux放音乐cd
查看>>
开源im方案之openfire
查看>>
OneNote无法同步问题
查看>>
GridView选择性导出Excel
查看>>