【C】CDECL 源代码
来源:互联网 发布:公章制作软件 编辑:程序博客网 时间:2024/06/05 07:56
#include <stdio.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#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] = s/* figure out the identifier type */enum type_tag classify_string(void){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;}/* read next token into "this" */void gettoken(void){char *p = This.string;/* read past any space */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 */void read_to_first_identifier(){gettoken();while (This.type != IDENTIFIER ){push(This);gettoken();}printf("%s is ", This.string);gettoken();}void 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 ");}}void deal_with_function_args(){while (This.type != ')'){gettoken();}gettoken();printf("function returning ");}void deal_with_pointers(){while (stack[top].type == '*'){printf("%s ", pop.string);}}void deal_with_declarator(){/* deal with possible array/function following identifier */switch (This.type){case '[' : deal_with_arrays(); break;case '(' : deal_with_function_args();}deal_with_pointers();/* process tokens that we stacked while reading identifier */while (top >= 0){if (stack[top].type == '('){pop;gettoken(); /* read past ')' */deal_with_declarator();}else{printf("%s ", pop.string);}}}int main(){/* put tokens on stack until we reach identifier */read_to_first_identifier();deal_with_declarator();printf("\n");return 0;}
以上是C语言版本。。
阅读全文
0 0
- 【C】CDECL 源代码
- cdecl源代码
- C声明器cdecl
- cdecl:C语言声明分析器
- cdecl
- cdecl
- cdecl、stdcall、fastcall、declspec 、extern "c"
- C专家编程读书笔记-cdecl程序
- cdecl程序,用于分析C语言的声明
- C语言函数调用约定-stdcall&cdecl&thiscall
- stdcall cdecl
- stdcall cdecl
- stdcall cdecl
- 程序比程序员更理解c语言! cdecl程序(分析c语言的声明
- C专家编程:用python写cdecl程序,以分析C语言程序的声明语句
- linux源代码-fork.c源代码
- windows 加载动态链接库extern "C" 、cdecl、stdcall、fastcall、declspec
- C语言的声明解释的在线工具——cdecl
- dedecms 5.7 list按weight权重排序/支持按权重排序
- Python Selenium 启动新版本Firefox失败问题解决方案
- Ubuntu apache2 + mod_wsgi 部署总结
- 移动端页面自适应代码
- android studio 找不到svn.exe的处理办法
- 【C】CDECL 源代码
- elasticsearch 版本区别
- 使用AsycHttpClient请求Tomcat的新闻客户端的实现
- DWA局部路径规划
- pandas 中 DataFrame
- string path=request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort();
- 无线技术分类与应用场景
- java基础常用类型转换
- Keepalived 原理