Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_1.24_synerrors.c
1240 views
1
/**
2
*
3
* Exercise 1.24 - Syntax Errors.
4
*
5
* Program to check rudimentary syntax errors like un-match braces,
6
* brackets or parenthesis.
7
*
8
**/
9
10
#include<stdio.h>
11
12
int brace,brack,paren;
13
14
void incomment();
15
void inquote(int c);
16
void search(int c);
17
18
int main(void)
19
{
20
int c;
21
22
extern int brace, brack, paren;
23
24
while((c=getchar())!=EOF)
25
if( c == '/')
26
if((c=getchar())== '*')
27
incomment();
28
else
29
search(c);
30
else if( c == '\'' || c == '"')
31
inquote(c);
32
else
33
search(c);
34
35
if( brace < 0)
36
{
37
printf("Unmatched Braces\n");
38
brace = 0;
39
}
40
else if( brack < 0)
41
{
42
printf("Unmatched brackets\n");
43
brack = 0;
44
}
45
else if( paren < 0)
46
{
47
printf("Unmatched parenthesis\n");
48
paren = 0;
49
}
50
51
if(brace > 0)
52
printf("Unmatched braces\n");
53
else if(brack > 0)
54
printf("Unmatched brackets\n");
55
else if(paren > 0)
56
printf("Unmatched parenthesis\n");
57
58
return 0;
59
}
60
61
void incomment()
62
{
63
int c,d;
64
65
c = getchar();
66
d = getchar();
67
68
while(c != '*' || d != '/')
69
{
70
c = d;
71
d = getchar();
72
}
73
}
74
75
void inquote(int c)
76
{
77
int d;
78
79
putchar(c);
80
81
while((d=getchar())!=c)
82
if( d == '\\')
83
getchar();
84
}
85
86
void search(int c)
87
{
88
extern int brace,brack,paren;
89
90
if ( c == '{')
91
--brace;
92
else if ( c == '}')
93
++brace;
94
else if( c == '[')
95
--brack;
96
else if( c == ']')
97
++brack;
98
else if( c == '(')
99
--paren;
100
else if( c == ')')
101
++paren;
102
}
103
104
105
106
107