-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictive parser.c
More file actions
74 lines (74 loc) · 869 Bytes
/
predictive parser.c
File metadata and controls
74 lines (74 loc) · 869 Bytes
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
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
void err(const char* s)
{
perror(s);
exit(0);
}
int factor()
{
int val,i;
char ch[0];
scanf("%s",ch);
switch(ch[0])
{
case '(':
val=expr();
scanf("%s",ch);
if(ch[0]!=')')
err("Missing closing paranthesis in factor.");
break;
default :{
for(i=0;i<strlen(ch);i++)
{
if((ch[i]>'0')&&(ch[i]<='9'))
continue;
else
err("Illegal character sequence in place of factor.");
}
val=atoi(ch);
}
}
return val;
}
int term()
{
int val;
char ch[10];
val=factor();
while(1)
{
scanf("%s",ch);
if(ch[0]=='*')
{
val=val*factor();
}
else
break;
}
ungetc(ch[0],stdin);
return val;
}
int expr()
{
int val;
char ch[10];
val=term();
while(1)
{
scanf("%s",ch);
if(ch[0]=='+')
val=val+term();
else
break;
}
ungetc(ch[0],stdin);
return val;
}
main()
{
printf("\nEnter the expression: ");
printf("\n Result: %d\n",expr());
}