-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrgm 12(CD)
More file actions
107 lines (96 loc) · 2.13 KB
/
Prgm 12(CD)
File metadata and controls
107 lines (96 loc) · 2.13 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
%{
#include<ctype.h>
char str[20];
int i;
%}
%token id
%left '+''/''*''-';
%%
E:S {infix_postfix(str);}
S:S'+'T|S'-'T
|T
T:T'*'F|T'/'F
|F
F:id|'('S')'
|;
%%
#include<stdio.h>
main()
{
printf("Enter an identifer\n");
yyparse();
}
yyerror()
{
printf("Invalid\n");
}
yylex()
{
charch='';
while(ch!='\n')
{
ch=getchar();
str[i++]=ch;
if(isalpha(ch))return id;
if((ch=='+'||ch=='-'||ch=='*'||ch=='/')return ch;)
str[--i]='\0';
return 0;
exit(0);
}
}
void push(char stack[],int*pop,char ch)
{
stack[++(*top)]=ch;
}
char pop(char stack[],int* top)
{
return(stack[top--]);
}
int perc(char ch)
{
switch(ch)
{
case'/':;
case'*':return 2;
case'+':;
case'-':return 1;
case'(':return 0;
default:return -1;
}
}
void infix_postfix(char infix[])
{
int top=-1,iptr=-1,stksymb,cursymb;
push(stck,&top,'\0');
while((cursymb=infix[++ptr])!='\0')
{
switch(cursymb)
{
case'(':push(stack,&top,cursymb);
break;
case')':stksymb=pop(stack,&top);
while(stksymb!='(')
{
postfix[++ptr]=stksymb;
stksymb=pop(stack,&top);
}
break;
case'*':
case'/':
case'+':
case'-':while(prec(stack[top])>prec(cursymb))
postfix[++ptr]=pop(stack,&top);
push(stack,&top,cursymb);
break;
default:if(isalnum(cursymb)==0)
{
printf("Error in input\n");
exit(0);
}
postfix[++pptr]=cursymb;
}
}
while(top!=1)
postfix[++ptr]=pop(stack,&top);
printf("%s",postfix);
}