Getting started with the basics of programming exercises_4
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的更多相关文章
- Getting started with the basics of programming exercises_5
1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...
- Getting started with the basics of programming exercises_3
1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...
- 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) ...
- Getting started with the basics of programming exercises_1
1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...
- Beginning C# Programming with Unity
Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...
- C语言学习书籍推荐《Practical C++ Programming》下载
下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...
- 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? ...
- LINQ Query Expressions
https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...
- 【译】微软的Python入门教程(一)
Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...
随机推荐
- 避免SQL注入三慷慨法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangyy130/article/details/26154837 要说SQL注入还要从 ...
- CHARINDEX函数
CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置.CHARINDEX函数调用方法如下: CHARINDEX ( expression1 , expression2 [ , st ...
- Slackware网卡配置文件和配置工具
Slackware 有关网卡的配置文件是/etc/rc.d/rc.inet1.conf , 这个文件包括乙太网接口的网卡和无线网卡的配置.Slackware 还是比较纯净的,网络配置也较简单:在Sla ...
- MySQL 开启远程访问权限 | 宝塔系统
1.进入 MySQL 管理菜单 2.选择权限为所有人
- 用两个栈实现队列功能【剑指offer】
题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 程序代码: [方法一] class Solution { public: void push(int ...
- 【JZOJ3601】【广州市选2014】Tree(tree)
╰( ̄▽ ̄)╭ 每个非叶子节点,其左右子树叶子节点的权值之和相等.我们称这种二叉树叫平衡二叉树. 我们将一棵平衡二叉树叶子节点的权值从左到右列出来,假如这个权值序列是另一个序列A的子序列,我们称这棵平 ...
- PHP获取用户客户端真实IP的解决方案是怎样呢?
function getIp(){if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIE ...
- SQLServer —— 变量的使用
一.局部变量的定义与赋值 定义语法: -- 声明一个局部变量 DECLARE @变量名 数据类型 -- 声明多个局部变量 DECLARE @变量名1 数据类型1, @变量名2 数据类型2 赋值语法: ...
- 纯CSS3绘制的黑色图标按钮组合
在线演示 本地下载
- react-cnode
感谢无私开源的程序员们~~~代码因为你们更加美腻~ //根index.js import React, { Fragment } from 'react'; import ReactDOM from ...