类型解释器——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网络编程这本书的读书笔记,准备读三本,这一系类的文章会不断更新,一直会持续一个月多,每篇的前半部分是书中讲述的内容,每篇文章的后半部分是自己的心得体会,文章中的红色内容是很重要 ...
随机推荐
- Orchard学习系列-----如何运行的
感慨:当接触到微软这套程序时,代码实在是太好了,好的几乎都读不懂.很久之前就对这个套开源程序特别感兴趣,但读不明白也让人郁闷. 背景(Orchard官网): 可组装系统的CMS系统,OrChard在运 ...
- nodejs发起HTTPS请求并获取数据
摘要:在网站中有时候需要跨域请求数据,直接用Ajax无法实现跨域,采用其他方式需要根据不同的浏览器做相应的处理.用Nodejs可以很好的解决这些问题,后台引用HTTPS模块,发送和返回的数据均为JSO ...
- Lintcode: Interval Sum
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- java中Date的getTime() 方法奇葩问题
今天遇到了一个奇葩问题: 从数据库中读取了3个Date类型的数据: DATE1:2015-03-12 12:10:42 DATE2:2015-03-12 12:04:40 DATE3:2015-03- ...
- linux-统计一个文件中出现的单词数
#!/bin/bash ] then echo "Usage: $0 filename"; exit - fi filename=$ egrep -o "\b[[:alp ...
- EBS登陆链接网址
SELECT home_url FROM icx_parameters; SELECT profile_option_value FROM fnd_profile_option_values WHER ...
- 夺命雷公狗---node.js---13之Buffer的转换
其实Buffer也是一种数据结构的东西,但实际中用得并不多,我们只需要知道常用的转换方法即可: /** * Created by leigood on 2016/8/30. */ var str = ...
- Testcase篇
1: forever @(); 等待,c触发event. forever @(`SOC_TESTBENCH_NAME.vt_event1);在整个case的执行过程中,只要.c触发event1,就执行 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON HImage与Bitmap格式转换
zw版[转发·台湾nvp系列Delphi例程]HALCON HImage与Bitmap格式转换 (Delphi Prism)namespace HImage_Bitmap_Prism;interfac ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle1
zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle1 procedure TForm1.Button1Click(Sender: TObject);var img : ...