-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDescendent-parser.java
201 lines (170 loc) · 3.27 KB
/
Descendent-parser.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.*;
class Production
{
String starting,end,terminal,nonTerminal;
Production(String start,String end)
{
starting=start;
this.end=end;
Lexical();
}
String returnProduction()
{
return(starting+"->"+end);
}
void Lexical()
{
for(int j=0;j<end.length();j++)
{
if(end.charAt(j)>='A'&&end.charAt(j)<='Z')
nonTerminal=nonTerminal+end.charAt(j);
else
terminal=terminal+end.charAt(j);
}
}
boolean appearIn(char c)
{
for(int i=0;i<terminal.length();i++)
{
if(terminal.charAt(i)==c)
return(true);
}
return(false);
}
}
class DescendentParser
{
private
String start,end,terminal,nonTerminal,input;
Production[] P;
int n;
public
DescendentParser()
{
Scanner S=new Scanner(System.in);
System.out.print("Enter no. of Production:");
n=S.nextInt();
P=new Production[n];
nonTerminal=terminal="";
for(int i=0;i<n;i++)
{
System.out.println("Enter Production "+i+":\n");
System.out.print("Enter Starting Symbol:");
start=S.next();
System.out.print("Enter Ending Symbol:");
end=S.next();
P[i]=new Production(start,end);
}
System.out.print("Enter Input String:");
input=S.next();
Lexical();
}
void Lexical()
{
int j;
boolean flag;
String temp=new String(input.replace("->",""));
for(j=0;j<input.length();j++)
{
if(temp.charAt(j)>='A'&&temp.charAt(j)<='Z')
nonTerminal=nonTerminal+temp.charAt(j);
else
terminal=terminal+temp.charAt(j);
}
System.out.print("\nTerminal:"+terminal+" Length:"+terminal.length());
for(j=0;j<terminal.length();j++)
{
flag=false;
for(int i=0;i<n;i++)
{
if(P[i].appearIn(terminal.charAt(j)))
{
flag=true;
break;
}
}
if(!flag)
{
System.out.println("\nSorry During Lexical We cannot find symbol '"+terminal.charAt(j)+"'!");
System.exit(0);
}
}
System.out.println("\nLexical analysis is done!");
}
boolean isTerminal(char a)
{
if(a>='A'&&a<='Z')
{
return(false);
}
return(true);
}
void parse()
{
int i,j,k=0,h;
String temp;
for(i=0;i<n;i++)
{
k=0;
temp=P[i].end;
System.out.println("\nChecking in Production:"+P[i].returnProduction());
for(j=0;j<temp.length();j++)
{
char ch=temp.charAt(j);
if(isTerminal(ch))
{
if(terminal.charAt(k)!=ch)
{
//change the production
System.out.println("Not matched BackTracking!");
break;
}
else
{
System.out.println("'"+terminal.charAt(k)+"' is matched!"+k);
k++;
}
}
else
{
String expanded=expand(temp.charAt(j));
if(expanded.equals("!"))
{ //expand with nothing
temp=temp.replaceFirst(""+temp.charAt(j),"");
}
else
{
temp=temp.replaceFirst(""+temp.charAt(j),expanded);
}
System.out.println("\nAfter expanding:"+temp);
j--;
}
}
if(k>=(terminal.length()-1))
{
System.out.println("Matched"+k);
System.exit(0);
}
}
System.out.println("Not Matched"+k);
}
String expand(char ch)
{
for(int i=0;i<n;i++)
{
if(P[i].starting.charAt(0)==ch)
{
return(P[i].end);
}
}
return(null);
}
}
class Main
{
public static void main(String[] a)
{
DescendentParser P1=new DescendentParser();
P1.parse();
}
}