-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path1-24.c
140 lines (121 loc) · 3.38 KB
/
1-24.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Exercise 1-24. Write a program to check a C program for rudimentary syntax
* errors like unmatched parentheses, brackets and braces. Don't forget about
* quotes, both single and double, escape sequences, and comments. (This
* program is hard if you do it in full generality.)
*
* By Faisal Saadatmand
*/
/* NOTE: this is not done in full generality */
#include <stdio.h>
#define YES 1
#define NO 0
/* globals */
int leftParens = 0;
int rightParens = 0;
int leftBrackets = 0;
int rightBrackets = 0;
int leftBraces = 0;
int rightBraces = 0;
/* functions */
void printInfo();
int skipChar(int);
void checkSymbolsBallance(void);
void countSymbols(void);
int skipComment(int);
int skipQuote(int);
/* skipChar: skips n characters in the input stream */
int skipChar(int n)
{
int c;
while (n--)
c = getchar();
return c ;
}
/* skipComment: skip characters in the input stream until encountered the
* ending symbol of a c-style comment */
int skipComment(int c)
{
int stop = NO;
while (stop == NO && (c = getchar()) != EOF)
if (c == '*' && (c = getchar()) == '/')
stop = YES;
return c;
}
/* skipComment: skip characters in the input stream until encountered the
* ending character of a c-style quote (single or double) */
int skipQuote(int type)
{
int c, stop = NO, step = 2;
while (stop == NO && (c = getchar()) != EOF) {
if (c == '\\')
c = skipChar(step);
if (c == type)
stop = YES;
}
return c;
}
/* countSymbols: count c-style demarcating symbols for comments and quote */
void countSymbols(void) {
extern int leftParens, rightParens, leftBrackets, rightBrackets,
leftBraces, rightBraces;
int c;
while ((c = getchar()) != EOF) {
if (c == '/' && (c = getchar()) == '*') /* skip comments */
c = skipComment(c);
if (c == '"') /* skip double quotes */
c = skipQuote(c);
if (c == '\'') /* slip single quotes */
c = skipQuote(c);
if (c == '(')
++leftParens;
if (c == ')')
++rightParens;
if (c == '[')
++leftBrackets;
if (c == ']')
++rightBrackets;
if (c == '{')
++leftBraces;
if (c == '}')
++rightBraces;
}
}
/* checkSymbolsBallance: check if number of c-style demarcating symbols for
* comments and quotes are balanced. Print an error message if not. */
void checkSymbolsBallance(void)
{
extern int leftParens, rightParens, leftBrackets, rightBrackets,
leftBraces, rightBraces;
if (leftParens - rightParens < 0)
printf("Error: missing '('\n");
else if (leftParens - rightParens > 0)
printf("Error: missing ')'\n");
if (leftBrackets - rightBrackets < 0)
printf("Error: missing '['\n");
else if (leftBrackets - rightBrackets > 0)
printf("Error: missing ']'\n");
if (leftBraces - rightBraces < 0)
printf("Error missing '{'\n");
else if (leftBraces - rightBraces > 0)
printf("Error missing '}'\n");
}
/* printInfo: print the number of demarcating symbols for comments and quotes */
void printInfo(void)
{
extern int leftParens, rightParens, leftBrackets, rightBrackets,
leftBraces, rightBraces;
printf("'(': %i ')': %i Total: %i\n",
leftParens, rightParens, leftParens + rightParens);
printf("'[': %i ']': %i Total: %i\n",
leftBrackets, rightBrackets, leftBrackets + rightBrackets);
printf("'{': %i '}': %i Total: %i\n",
leftBraces, rightBraces, leftBraces + rightBraces);
}
int main(void)
{
countSymbols();
printInfo();
checkSymbolsBallance();
return 0;
}