类型解释器——C专家编程读书笔记
对于声明,应该按下面的步骤来进行解释:
1) 声明从它的名字开始读取,然后按照优先级顺序依次读取
2) 优先级顺序
a) 括号括起来的部分
b) 后缀操作符,()表示函数,[]表示数组
c) 前缀操作符,*表示指针
3) 如果const或volatile关键字后面紧跟类型说明符,那么他作用于类型说明符,其他情况下,作用于其左边紧邻的指针星号。
根据这个原则,我们可以得到下面的代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64 enum type_tag { IDENTIFIER, QUALIFIER, TYPE }; struct token {
char type;
char string[MAXTOKENLEN];
}; int top = -;
struct token stack[MAXTOKENS];
struct token that; #define pop stack[top--]
#define push(s) stack[++top] = s enum type_tag classify_string(void) {
char *s = that.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)
{
char *p = that.string; while ((*p = getchar()) == ' '); if (isalnum(*p)){ while (isalnum(*++p = getchar()));
ungetc(*p, stdin);
*p = '\0';
that.type = classify_string();
return;
} if (*p == '*') {
strcpy(that.string, "pointer to");
that.type = *p;
return;
}
that.string[] = '\0';
that.type = *p;
return;
} void read_to_first_identifier (){
gettoken();
while (that.type != IDENTIFIER) {
push(that);
gettoken();
}
printf("%s is ", that.string);
gettoken();
} void deal_with_arrays() {
while (that.type == '[') {
printf("array ");
gettoken();
if (isdigit(that.string[])) {
printf("0..%d ", atoi(that.string)-);
gettoken();
}
gettoken();
printf("of ");
}
} void deal_with_function_args() {
while (that.type != ')') {
gettoken();
}
gettoken();
printf("function returning ");
} void deal_with_pointers () {
while (stack[top].type == '*') {
printf("%s ", pop.string);
}
} void deal_with_declarator() { switch(that.type){
case '[' :deal_with_arrays();break;
case '(' :deal_with_function_args();
} deal_with_pointers(); while (top >= ) {
if (stack[top].type == '(') {
pop;
gettoken();
deal_with_declarator();
}else {
printf("%s ", pop.string);
}
}
}
int (*a)()
结果:
a is pointer to function returning int
过程:
读入int
读入(
读入*
读入a
a是标识符,退出开始的循环
输出a is
读入),由于有)暂不读入后面字符,弹出*,输出pointer to,
一直弹出,直到(则继续读取后面的字符(,
因为读到(,输出function returning.
int *a()
结果:
a is function returning pointer to int
过程:
读入int
读入*
读入a
a是标识符,退出开始的循环
输出a is
读入(判断出a是个函数输出function returning
读取*,输出pointer to
读取int,输出int
类型解释器——C专家编程读书笔记的更多相关文章
- c专家编程读书笔记
无论在什么时候,如果遇到malloc(strlen(str));,几乎可以直接断定他是错误的,而malloc(strlen(str)+1):才是正确的: 一个L的NUL哟关于结束一个ACSII字符串: ...
- 《android开发进阶从小工到专家》读书笔记--HTTP网络请求
No1: 客户端与服务器的交互流程: 1)客户端执行网络请求,从URL中解析出服务器的主机名 2)将服务器的主机名转换成服务器的IP地址 3)将端口号从URL中解析出来 4)建立一条从客户端与Web服 ...
- python高级编程读书笔记(一)
python高级编程读书笔记(一) python 高级编程读书笔记,记录一下基础和高级用法 python2和python3兼容处理 使用sys模块使程序python2和python3兼容 import ...
- C++Windows核心编程读书笔记
转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%96%87/71405.shtml "C++Windows核心编程读书笔 ...
- Node.js高级编程读书笔记Outline
Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...
- CSAPP 并发编程读书笔记
CSAPP 并发编程笔记 并发和并行 并发:Concurrency,只要时间上重叠就算并发,可以是单处理器交替处理 并行:Parallel,属于并发的一种特殊情况(真子集),多核/多 CPU 同时处理 ...
- Java并发编程读书笔记(一)
----------------------------------------------<Java并发编程实战>读书笔记-------------------------------- ...
- unix环境高级编程-读书笔记与习题解答-第一篇
从这周开始逐渐的进入学习状态,每天晚上都会坚持写c程序,并且伴随对这本书的深入,希望能写出更高质量的读书笔记和程序. 本书的第一章,介绍了一些关于unix的基础知识,在这里我不想去讨论linux到底是 ...
- UNIX网络编程--读书笔记
会集中这段时间写UNIX网络编程这本书的读书笔记,准备读三本,这一系类的文章会不断更新,一直会持续一个月多,每篇的前半部分是书中讲述的内容,每篇文章的后半部分是自己的心得体会,文章中的红色内容是很重要 ...
随机推荐
- Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file
springframework.version 3.2.6.RELEASE jdk 1.8
- ads 的一些错误
遇到动不动就有*.o文件找不到的情况,而且通常都是开始的时候可以正常LINK,而经过若干次重新LINK后却找不到(即使只是在代码中加一个无关紧要得空格),新建个工程,将目前的.C和.H文件原封不动加进 ...
- [原创]java WEB学习笔记85:Hibernate学习之路-- -映射 一对一关系 ,基于主键方式实现
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- shell学习笔记(2)替换命令··与()的区别
·CMD·在执行的时候,shell不管··中的内容是什么,先进性解释,再把解释后的最终结果送给shell,如果解释后的结果不是shell可以行的命令,就会报错.但是仅仅把cmd的执行结果作为文本输出, ...
- 封装upload文件上传类
<?php //封装php中的单文件(图片)上传类 /* //参数1:$file 文件数组 5个属性值 name,type,size,tmp,error //参数2:文件保存的路径$pa ...
- springday02-go2
1.复制xml文件到container.auto下2.Waiter类实现构造函数3.Bar类中Waiter作为其成员变量,并实现其get/set方法,有参和无参构造器,toString方法4.分别修改 ...
- 自定义view(使用EditTetx实现记事本特效)
先看一下效果图: 思路: 创建一个类,继承自EditText,在onDraw方法中绘制我们的下划线,通过屏幕的高度和每行控件的高度的比值得出屏幕中应该绘制多少行下划线,再来实现一些自定义的属性,设置控 ...
- 浅谈JavaScript计时器
JavaScript计时器 1.什么是JavaScript计时器? 在JavaScript中,我们可以在设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行. 2.计时器类型 一次性计时器:仅在 ...
- cactive信号
AXI中C-channel的cactive信号并不仅仅应用在CSYSREQ,CSYSACK交互中. CSYSREQ和CSYSACK信号都在低电平表示lower power的请求和应答有效. cacti ...
- [MacOS] xcrun: error: active developer path ("/Volumes/Xcode/Xcode6-Beta.app/Contents/Developer") does not exist, use xcode-select to change
When using MacOS with xcode6-beta, i always meet these error: xcrun: error: active developer path (& ...