-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdpnestedif.c
More file actions
170 lines (157 loc) · 4.37 KB
/
Copy pathrdpnestedif.c
File metadata and controls
170 lines (157 loc) · 4.37 KB
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
166
167
168
169
170
//This is the recursive descent parser for the statement:
//if ( c ) { if ( c ) {...if ( c ) { stmt ; } else { stmt ; }...} else { stmt ; } } else { stmt; }
//The corresponding grammar is:
//S' --> S | S'S
//S --> B | B else { S } | A
//B --> if ( c ) { S }
//A --> stmt;
//Enter $ to indicate end of input
#include<stdio.h>
#include<string.h>
char* S(char *_token, const char s[2]);
char* _S(char *_token, const char s[2]);
//function to move pointer
char* moveip(char *_token, const char s[2]){
if(_token != "$"){
return strtok(NULL, s);
}
else{
return NULL;
}
}
//function for symbol A
//A --> stmt ;
char* A(char *_token, const char s[2]){
int value;
if(_token != NULL && strcmp(_token, "stmt")==0){
_token = moveip(_token,s);
if(_token != NULL && strcmp(_token, ";")==0){
_token = moveip(_token,s);
return _token;
}
else{
return NULL;
}
}
else{
return NULL;
}
}
//function for symbol B
//B --> if ( c ) { S }
char* B(char *_token, const char s[2]){
int value;
if(_token != NULL && strcmp(_token, "if") == 0){
_token = moveip(_token, s);
if(_token != NULL && strcmp(_token, "(") == 0){
_token = moveip(_token, s);
if(_token != NULL && strcmp(_token, "c") == 0){
_token = moveip(_token, s);
if(_token != NULL && strcmp(_token, ")") == 0){
_token = moveip(_token, s);
if(_token != NULL && strcmp(_token, "{") == 0){
_token = moveip(_token, s);
_token = S(_token, s);
if(_token != NULL && strcmp(_token,"}") == 0){
_token = moveip(_token, s);
return _token;
}
else{
return NULL;
}
}
else{
return NULL;
}
}
else{
return NULL;
}
}
else{
return NULL;
}
}
else{
return NULL;
}
}
else{
return NULL;
}
}
//function for start symbol S
//S --> B | B else { S } | A
char* S(char *_token, const char s[2]){
if(strcmp(_token, "$")==0){
return _token;
}
char* temptok;
temptok = B(_token, s);
if(temptok != NULL){
if(strcmp(temptok, "else")==0){
temptok = moveip(temptok, s);
if(temptok != NULL && strcmp(temptok, "{")==0){
temptok = moveip(temptok, s);
temptok = S(temptok, s);
if(temptok != NULL && strcmp(temptok, "}")==0){
temptok = moveip(temptok, s);
_token = temptok;
return _token;
}
else{
return NULL;
}
}
else{
return NULL;
}
}
else{
_token = temptok;
return _token;
}
}
temptok = A(_token, s);
if(temptok != NULL){
if(strcmp(temptok, "}")==0 || strcmp(temptok, "$")==0){
_token = temptok;
return _token;
}
return _token;
}
}
//function for starting symbol S'
//S' --> S | SS'
char* _S(char *_token, const char s[2]){
if(strcmp(_token, "$")==0){
return _token;
}
char* temptok;
temptok = S(_token, s);
if(temptok != NULL){
if(strcmp(temptok, "$")==0){
return temptok;
}
temptok = _S(temptok, s);
}
}
//main function
void main(){
char command[100];
int invalid = 0;
printf("Enter command:\n");
scanf("%[^\n]", command);
const char s[2] = " ";
char *token;
/* get the first token */
token = strtok(command, s);
//call the function defined for start symbol
char* final = _S(token, s);
if(final != NULL && strcmp(final, "$")==0){
printf("Valid command\n");
}
else{
printf("Invalid command\n");
}
}