This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.Recursive_Descent_Parser.c
137 lines (125 loc) · 1.94 KB
/
5.Recursive_Descent_Parser.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
#include <stdio.h>
#include <ctype.h>
// Gobal declarations
char expr[20];
int i = 0;
// Function Prototyping
int E(void);
int E_Prime(void);
int T(void);
int T_Prime(void);
int F(void);
int id(char c);
int main(void)
{
// Reading input
printf("Enter an arithmetic expression : ");
gets(expr);
// Recursive descent parser logic
if (E())
{
if (expr[i + 1] == '\0')
puts("Accepted..!!!");
else
puts("Rejected..!!!");
}
else
puts("Rejected..!!!");
return 0;
}
// Production : E -> TE'
int E(void)
{
if (T())
{
if (E_Prime())
return 1;
else
return 0;
}
return 0;
}
// Production : E' -> +TE' | ε
int E_Prime(void)
{
if (expr[i] == '+')
{
++i;
if (T())
{
if (E_Prime())
return 1;
else
return 0;
}
else
return 0;
}
return 1;
}
// Production : T -> FT'
int T(void)
{
if (F())
{
if (T_Prime())
{
return 1;
}
else
return 0;
}
return 0;
}
// Production : *FT' | ε
int T_Prime(void)
{
if (expr[i] == '*')
{
++i;
if (F())
{
if (T_Prime())
return 1;
else
return 0;
}
else
return 0;
}
return 1;
}
// Production : (E) | id
int F(void)
{
if (expr[i] == '(')
{
++i;
if (E())
{
if (expr[i] == ')')
return 1;
else
return 0;
}
}
else if (id(expr[i]))
{
return 1;
}
return 0;
}
// id refers to identifier
int id(char c)
{
if (c == '_' || isalpha(c))
{
c = expr[++i];
while (c == '_' || isalnum(c))
{
c = expr[++i];
}
return 1;
}
return 0;
}