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
|
#include <stdio.h>
#define OUT 0
#define FALSE -1
#define TRUE 1
#define STAR 1
#define SLASH 2
void count(char input, int* parentheses, int* brackets, int* braces);
/* this is a c programm to detect syntax errors in c programms */
int
main(void)
{
char input;
int temp, state, line, parentheses, brackets, braces, single_quotes, double_quotes, double_quotes_line;
state = OUT;
parentheses = brackets = braces = single_quotes = double_quotes = double_quotes_line = 0;
line = 1;
while ((input = getchar()) != EOF) {
if (input == '\n') {
line++;
continue;
}
/* filiter out comments */
else if (input == '/') {
temp = input;
if ((input = getchar()) == '*') {
state = STAR;
temp = FALSE;
}
else if (input == '/') {
state = SLASH;
temp = FALSE;
}
}
else if (input == '*' && state == STAR) {
if ((input = getchar()) == '/') {
state = OUT;
continue;
}
}
else if (input == '\n' && state == SLASH) {
state = OUT;
continue;
}
if (temp != FALSE)
count(temp, &parentheses, &brackets, &braces);
if (state == OUT) {
if (input == '"' && double_quotes == TRUE) {
double_quotes = FALSE;
continue;
}
/* process escape sequences */
else if (input == '\\') {
input = getchar();
if (input == '\n') {
line++;
continue;
}
if (input != 'a' && input != 'b' && input != 'f' && input != 'n' && input != 'r' &&
input != 't' && input != 'v' && input != '\\' && input != '\'' && input != '"' && input != '?' && input != '0')
printf("unproper escape sequence detected at line %d\n", line);
else if (input == 'o') {
if((input = getchar()) != 'o' || (input = getchar()) != 'o')
printf("unproper escape sequence detected at line %d\n", line);
}
else if (input == 'x') {
if((input = getchar()) != 'h' || (input = getchar()) != 'h')
printf("unproper escape sequence detected at line %d\n", line);
}
else
continue;
}
/* process double quotes */
else if (input == '"') {
double_quotes = TRUE;
double_quotes_line = line;
}
/* process single quotes */
else if (input == '\'' && double_quotes != TRUE) {
;
if ((input = getchar()) == '\\') {
input = getchar();
}
if ((input = getchar()) != '\'')
printf("unproper single quotes detected at line %d\n", line);
continue;
}
/* process all others */
count(input, &parentheses, &brackets, &braces);
}
}
if (parentheses%=2)
printf("unproper parentheses detected\n");
if (brackets%=2)
printf("unproper brackets detected\n");
if (braces%=2)
printf("unproper braces detected\n");
if (double_quotes == TRUE)
printf("unproper double quotes detected at line %d\n", double_quotes_line);
return 0;
}
void
count(char input, int* parentheses, int* brackets, int* braces)
{
switch (input) {
case '(' :
(*parentheses)++;
break;
case ')' :
(*parentheses)--;
break;
case '[' :
(*brackets)++;
break;
case ']' :
(*brackets)--;
break;
case '{' :
(*braces)++;
break;
case '}' :
(*braces)--;
break;
}
}
|