-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax_v0.c
165 lines (144 loc) · 3.08 KB
/
syntax_v0.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* 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.) */
#include "stdio.h"
#include "string.h"
int c;
int paren, brack, brace, sinquo, douquo, slcmt, mlcmt;
const char ESCAPE[] = "?abfbrtv";
enum legal {LEGAL, ILLEGAL};
void inslcmt(void);
void inmlcmt(void);
void checkout(void);
void search(void);
int chkescape(void);
int insinquo(void);
int indouquo(void);
int main(void) {
while ((c = getchar()) != EOF) {
if (c == '/') {
if ((c = getchar()) == '/') {
++slcmt;
inslcmt();
} else if (c == '*') {
++mlcmt;
inmlcmt();
} else {
printf("Illegal use of '/'.\n");
checkout();
return 0;
}
} else if (c == '\'') {
++sinquo;
if (insinquo() == ILLEGAL) {
checkout();
return 0;
}
} else if (c == '"') {
++douquo;
if (indouquo() == ILLEGAL) {
checkout();
return 0;
}
} else
search();
if (c == EOF)
return 0;
}
checkout();
return 0;
}
/* In single-line comment */
void inslcmt(void) {
while ((c = getchar()) != '\n' && c != EOF);
--slcmt;
}
/* In multi-line comment */
void inmlcmt(void) {
while ((c = getchar()) != EOF) {
if (c == '*' && (c = getchar()) == '/') {
--mlcmt;
return;
}
if (c == EOF)
break;
}
printf("Unterminated multi-line comment.\n");
}
/* Print error message */
void checkout(void) {
if (paren != 0)
printf("Containing unmatched parentheses.\n");
if (brace != 0)
printf("Containing unmatched braces.\n");
if (brack != 0)
printf("Containing unmatched brackets.\n");
if (sinquo != 0)
printf("Containing unmatched single quotes.\n");
if (douquo != 0)
printf("Containing unmatched double quotes.\n");
if (slcmt != 0)
printf("Containing unterminated single-line comment.\n");
if (mlcmt != 0)
printf("Containing unterminated multi-line comment.\n");
}
/* Check escape character */
int chkescape(void) {
if (c == '\\')
if (!strchr(ESCAPE, c=getchar()))
return ILLEGAL;
return LEGAL;
}
/* In single quote */
int insinquo(void) {
while ((c = getchar()) != '\'' && c != EOF) {
if (chkescape() == ILLEGAL) {
printf("Illegal escape character.\n");
return ILLEGAL;
}
}
if (c == '\'') {
--sinquo;
return LEGAL;
}
return ILLEGAL;
}
/* In double quote. */
int indouquo(void) {
while ((c = getchar()) != '"' && c != EOF) {
if (chkescape() == ILLEGAL) {
printf("Illegal escape character.\n");
return ILLEGAL;
}
}
if (c == '"') {
--douquo;
return LEGAL;
}
return ILLEGAL;
}
/* Searching for parentheses, brackets and braces. */
void search(void) {
switch (c) {
case '(':
++paren;
break;
case ')':
--paren;
break;
case '[':
++brack;
break;
case ']':
--brack;
break;
case '{':
++brace;
break;
case '}':
--brace;
break;
default:
;
}
}