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

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. [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

    1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...

  2. 头部固定下面滑动&&获取手机屏高

    height(){ //获取屏高 let phone_height = document.documentElement.clientHeight; let group = this.refs.sea ...

  3. JAVA的String 类

    String类 1.String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; s ...

  4. HTML语言的一些元素(五)

    一.table表格语法与结构 <table> <tr> <td> </td> <td> </td> <td> < ...

  5. c++之路进阶——bzoj3343(教主的魔法)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  6. 关于非阻塞connect的若干细节性问题

    我们用man connection命令查看手册,如下:   EINPROGRESS The socket is nonblocking and the connection cannot be com ...

  7. java数组转化成集合

    package com.shb.web; import java.util.Arrays;import java.util.List; import com.sun.xml.internal.ws.u ...

  8. BaseAction的一般写法

    package com.mi.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import ja ...

  9. ajax讲解:“创建用户”和“用户登录”练习

    ajax可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 传统的网页(不使用 AJAX)如果需要更新内容,必须重载整个网页页面.   接下来,将以例子的形式进行讲解   例一:创建用户   ...

  10. Dreamweaver 时间轴如何打开

    因为下午要辅导几个调皮捣蛋的小孩HTML, 什么能让他们感觉又简单又好玩而起又能提高他们的兴趣呢?DW中设置浮动广告吧!       想好了,动手去做.DwearmWeaver里面居然没了时间轴, 度 ...