-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpgm12.y
79 lines (78 loc) · 1.37 KB
/
pgm12.y
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
%{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int getREindex(const char*);
signed char productions[MAX][MAX];
int count =0,i,j;
char temp[MAX+MAX],temp2[MAX+MAX];
%}
%token ALPHABET
%left '|'
%left '.'
%nonassoc '*''+'
%%
S: re '\n' {
printf("This is RMD\n");
for(i=count-1;i>=0;--i)
{
if(i==count-1)
{
printf("\nre=>");
strcpy(temp,productions[i]);
printf("%s",productions[i]);
}
else
{
printf("\n=>");
j=getREindex(temp);
temp[j]='\0';
sprintf(temp2,"%s%s%s",temp,productions[i],(temp+j+2));
printf("%s",temp2);
strcpy(temp,temp2);
}
}
printf("\n");
exit(0);
}
re: ALPHABET {
temp[0]=yylval;
temp[1]='\0';
strcpy(productions[count++],temp);
}
| '('re')' { strcpy(productions[count++],"(re)");}
| re'*' { strcpy(productions[count++],"re*");}
| re'+' { strcpy(productions[count++],"re+");}
| re'|'re { strcpy(productions[count++],"re|re");}
| re'.'re { strcpy(productions[count++],"re.re");}
;
%%
int main( int argc, char** argv)
{
yyparse();
return 0;
}
yylex()
{
signed char ch=getchar();
yylval=ch;
if(isalpha(ch))
return ALPHA;
return ch;
}
yyerror()
{
fprintf(stderr,"INVALID REGEXP\n");
exit(0);
}
int getREindex(const char *str)
{
int i=strlen(str)-1;
for(;i>=0;--i)
{
if(str[i]=='e' && str[i-1]=='r')
return i-1;
}
}