对于声明,应该按下面的步骤来进行解释:

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专家编程读书笔记的更多相关文章

  1. c专家编程读书笔记

    无论在什么时候,如果遇到malloc(strlen(str));,几乎可以直接断定他是错误的,而malloc(strlen(str)+1):才是正确的: 一个L的NUL哟关于结束一个ACSII字符串: ...

  2. 《android开发进阶从小工到专家》读书笔记--HTTP网络请求

    No1: 客户端与服务器的交互流程: 1)客户端执行网络请求,从URL中解析出服务器的主机名 2)将服务器的主机名转换成服务器的IP地址 3)将端口号从URL中解析出来 4)建立一条从客户端与Web服 ...

  3. python高级编程读书笔记(一)

    python高级编程读书笔记(一) python 高级编程读书笔记,记录一下基础和高级用法 python2和python3兼容处理 使用sys模块使程序python2和python3兼容 import ...

  4. C++Windows核心编程读书笔记

    转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%96%87/71405.shtml "C++Windows核心编程读书笔 ...

  5. Node.js高级编程读书笔记Outline

    Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...

  6. CSAPP 并发编程读书笔记

    CSAPP 并发编程笔记 并发和并行 并发:Concurrency,只要时间上重叠就算并发,可以是单处理器交替处理 并行:Parallel,属于并发的一种特殊情况(真子集),多核/多 CPU 同时处理 ...

  7. Java并发编程读书笔记(一)

    ----------------------------------------------<Java并发编程实战>读书笔记-------------------------------- ...

  8. unix环境高级编程-读书笔记与习题解答-第一篇

    从这周开始逐渐的进入学习状态,每天晚上都会坚持写c程序,并且伴随对这本书的深入,希望能写出更高质量的读书笔记和程序. 本书的第一章,介绍了一些关于unix的基础知识,在这里我不想去讨论linux到底是 ...

  9. UNIX网络编程--读书笔记

    会集中这段时间写UNIX网络编程这本书的读书笔记,准备读三本,这一系类的文章会不断更新,一直会持续一个月多,每篇的前半部分是书中讲述的内容,每篇文章的后半部分是自己的心得体会,文章中的红色内容是很重要 ...

随机推荐

  1. svn合并

    1.先去 aone里我的变更 里 重建 新分支 相当于重主干上拉代码下来2.然后再去 原来的分支里 swich切换到新分支3.在原来的分支里 merge 到新分支的url4.选择最早的 version ...

  2. Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)

    控制台程序 定义Point类: public class Point { // Create a point from its coordinates public Point(double xVal ...

  3. linux:主机规划和磁盘分割

    1>.在linux系统中,每个装置都被装成一个档案来对待: 2>.各硬体装置在linux当中的档案名:SATA介面的硬碟的档案名为/dev/sd[a-d];在linux中,几乎所以的硬体装 ...

  4. Swift游戏实战-跑酷熊猫 11 欢迎进入物理世界

    物理模拟是一个奇妙的事情,以此著名的游戏有愤怒的小鸟.我们在这节将会一起来了解如何设置重力,设置物理包围体,碰撞的检测. 要点: 设置物理检测的代理: 让主场景遵循SKPhysicsContactDe ...

  5. PostgreSQL 非持久化设置(Non-Durable Settings)

    Durability is a database feature that guarantees the recording of committed transactions even if the ...

  6. 网易免费邮件开启smtp教程

    网易免费邮件开启smtp教程     作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们在部署zabbix邮件报警中可能会用到163.com.那么我们如何去开启smtp服务呢? 1 ...

  7. How to export a template in Visual Studio?

    Create a customize template file: 1.template arguments introduction like: 上图只是其中一部分,更多请查看文后的参考资源 tem ...

  8. ACM之Java速成(3)

    ACM中Java.大数处理 先上个代码: import java.math.*; import java.util.*; class Main{ public static void main(Str ...

  9. Android设计模式---观察者模式小demo(一)

    1,今天刚好看到了设计模式这一块来,而观察者模式是我一直想总结的,先来看看观察者模式的简单的定义吧 "当一个对象改变时,他的所有依赖者都会受到通知,并自动更新." 一般我们项目中就 ...

  10. android中在代码中设置margin属性

    1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...