1、编写一个删除C语言程序中所有的注释语句的程序。要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套。

#include<stdio.h>
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
// remove all comments from a valic C program
int main(void)
{
int c;
while((c = getchar()) != EOF)
rcomment(c); return 0;
}
// rcomment: read each character,remove the comments
void rcomment(int c)
{
int d; if(c == '/')
if((d = getchar()) == '*')
in_comment(); // beginning comment
else if(d == '/')
{
putchar(c); // another slash
rcomment(d);
}
else
{
putchar(c); // not a comment
putchar(d);
}
else if(c == '\'' || c == '"')
echo_quote(c); // quote begins
else
putchar(c); // not a comment
}
// in_comment: inside of a valid comment
void in_comment(void)
{
int c, d; c = getchar(); // prev character
d = getchar(); // curr character
while(c != '*' || d != '/') // here can be more readable
{
c = d;
d = getchar();
}
}
// echo_quote: echo characters within quotes
void echo_quote(int c)
{
int d; putchar(c);
while((d = getchar()) != c) // search for end
{
putchar(d);
if(d == '\\')
putchar(getchar()); // ignore escape seq
}
}

2、小型词法分析器

编写程序,查找C语言程序中的基本语法错误,如圆括号、方括号以及花括号不配对等。要正确处理引号(包括单引号、双引号)、转义字符序列与注释。

#include<stdio.h>
int brace, brack, paren;
void in_quote(int c);
void in_comment(void);
void search(int c);
// rudimentary syntax checker for C programs
int main(void)
{
int c;
extern int brace, brack, paren;
while((c = getchar()) != EOF)
{
if(c == '/')
{
if((c = getchar()) == '*')
in_comment(); // inside comment
else
search(c);
}
else if(c == '\'' || c == '"')
in_quote(c); // inside quote
else
search(c); if(brace < 0) // output errors
{
printf("Unbalanced braces\n");
brace = 0;
}
else if(brack < 0)
{
printf("Unbalanced brackets\n");
brack = 0;
}
else if (paren < 0)
{
printf("Unbalanced parentheses\n");
}
} if(brace > 0) // output errors
printf("Unbalanced braces\n");
if(brack > 0)
printf("Unbalanced brackets\n");
if(paren > 0)
printf("Unbalanced parentheses\n"); return 0;
} // search: search for rudimentary syntax errors
void search(int c)
{
extern int brace, brack, paren; if(c == '{')
++brace;
else if(c == '}')
--brace;
else if(c == '[')
++brack;
else if(c == ']')
--brack;
else if(c == '(')
++paren;
else if(c == ')')
--paren;
} // in_comment: inside of a valid comment
void in_comment(void)
{
int c, d; c = getchar(); // pre character
d = getchar(); // curr character
while(c != '*' || d != '/')
{
c = d;
d = getchar();
}
} // in_quote: inside quote
void in_quote(int c)
{
int d; while((d = getchar()) != c) // search end quote
if(d == '\\')
getchar(); // ignore escape seq
}

Getting started with the basics of programming exercises_4的更多相关文章

  1. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  2. Getting started with the basics of programming exercises_3

    1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...

  3. Getting started with the basics of programming exercises_2

    1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...

  4. Getting started with the basics of programming exercises_1

    1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...

  5. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  6. C语言学习书籍推荐《Practical C++ Programming》下载

    下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...

  7. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  8. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  9. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

随机推荐

  1. 2018-8-10-如何移动-nuget-缓存文件夹

    title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  2. Vbulletin Used to Show Malicious Advertisements

    In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...

  3. SQL Server 存储过程详解

    转自:https://blog.csdn.net/younghaiqing/article/details/62884658 一. 什么是存储过程 系统存储过程是系统创建的存储过程,目的在于能够方便的 ...

  4. oracle误操作commit之后,可以闪回数据

    1. 授予行迁移权限 alter table table_name enable row movement; 2. 到15分钟前: flashback table order   to timesta ...

  5. 二.使用JDBC对数据库CRUD

    一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...

  6. Oracle之PL/SQL编程

    PL/SQL(Procedural Language/SQL,过程语言/SQL) 是结合了Oracel过程语言和结构化查询语言(SQL)的一种扩展语言. 优点: (1)PL/SQL具有编程语言的特点, ...

  7. database homework1

    mysql> select * from teacher_info; +----+------+-----+--------+---------+ | id | name | age | sal ...

  8. 基于OSS+DataLakeAnalytics+QuickBI的Serverless的查询分析和可视化BI

    基于OSS的数据查询分析和可视化BI报表 数据存储在OSS后,有多种查询分析的方法,包括阿里云MaxCompute.DataLakeAnalytics产品等Severless查询分析服务,也可以自建S ...

  9. DCOJ5117 set

    题目描述 给定一个数集 A,要求构造一个数集 B,满足: • 对于 A 集合中任意的数 x,x 属于 B,即 A ⊆ B: • 对于 B 集合中任意的数 a, b,(a + b) mod p 属于 B ...

  10. iOS:学习runtime的理解和心得

    http://www.cocoachina.com/ios/20150901/13173.html 作者:兴宇是谁 授权本站转载. Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门 ...