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
}

最新文章

  1. LeetCode[5] 最长的回文子串
  2. 未能找到Microsoft.Office.Core.MsoTriState的引用
  3. 关于ajax的异步通信之异步
  4. 001Linux命令
  5. XNA Game Studio4.0 Programming 随便读,随便记。
  6. MongoDb Windows linux平台环境及主流编程语言驱动安装同时配置mongoDb的远程连接
  7. Jquery EasyUI tabs处理
  8. How to create a repository in Github with Eclipse?
  9. DM6446的Bootloader
  10. Jquery实现checkbox按shift多选
  11. EF 一个简单的使用
  12. Linux iostat 命令
  13. Android WebRTC开发入门
  14. PAT A1019 General Palindromic Number (20 分)——回文,进制转换
  15. Docker概念学习系列之Docker与传统虚拟机差异(4)
  16. mysql 获取当月日期天数
  17. GAN与NLP的讨论
  18. ConcurrentHashMap vs Collections.synchronizedMap()不同
  19. 关于easyUI的一些js方法
  20. 数据库之python操作mysql

热门文章

  1. HTML5属性
  2. yield函数的执行顺序
  3. 简单几招助您加速 ARM 容器应用开发和测试流程
  4. linux下对拍
  5. CTSC2017密钥、吉夫特
  6. excel一些常用的函数
  7. 关于本地文件请求json文件
  8. JS---案例:旋转木马
  9. 强力Django+杀手级xadmin开发在线教育网站
  10. [运维]ESXI系统的安装 标签: 虚拟机运维vmware服务器虚拟化 2017-05-05 09:24 496人阅读 评论(15)