-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpda.java
More file actions
109 lines (87 loc) · 3.46 KB
/
Copy pathpda.java
File metadata and controls
109 lines (87 loc) · 3.46 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
108
109
import java.util.*;
public class tocexp7 {
private Stack<Character> stack;
private String input;
private int index;
private Map<Character, List<String>> cfgProductions;
private List<String> steps;
public tocexp7(Map<Character, List<String>> cfgProductions, String input) {
this.stack = new Stack<>();
this.input = input;
this.index = 0;
this.cfgProductions = cfgProductions;
this.steps = new ArrayList<>();
}
public boolean isAccepted() {
stack.push('S');
steps.add("Start: Stack = [S], Input = " + input);
while (index < input.length()) {
char currentSymbol = input.charAt(index);
List<String> productions = cfgProductions.get(stack.peek());
if (productions != null) {
boolean transition = false;
for (String production : productions) {
char firstProductionSymbol = production.charAt(0);
if (firstProductionSymbol == currentSymbol) {
stack.pop();
for (int i = production.length() - 1; i >= 1; i--) {
stack.push(production.charAt(i));
}
transition = true;
break;
}
}
if (transition) {
steps.add("Matched: Stack = " + stack.toString() + ", Input = " + input.substring(index));
index++;
} else {
steps.add("Rejected: Stack = " + stack.toString() + ", Input = " + input.substring(index));
return false;
}
} else {
steps.add("Rejected: Stack = " + stack.toString() + ", Input = " + input.substring(index));
return false;
}
}
while (!stack.isEmpty()) {
char top = stack.pop();
if (top != 'S') {
steps.add("Accepted: Stack = " + stack.toString() + ", Input = " + input.substring(index));
return true;
}
}
return stack.isEmpty();
}
public void printSteps() {
for (String step : steps) {
System.out.println(step);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the CFG productions :");
String cfgInput = scanner.nextLine();
Map<Character, List<String>> cfgProductions = new HashMap<>();
String[] productions = cfgInput.split(", ");
for (String production : productions) {
String[] parts = production.split("->");
char variable = parts[0].charAt(0);
String[] rightHandSides = parts[1].split("\\|");
List<String> productionList = new ArrayList<>();
for (String rhs : rightHandSides) {
productionList.add(rhs);
}
cfgProductions.put(variable, productionList);
}
System.out.print("Enter the input string: ");
String inputString = scanner.nextLine();
tocexp7 pda = new tocexp7(cfgProductions, inputString);
if (pda.isAccepted()) {
System.out.println("The input string is accepted.");
} else {
System.out.println("The input string is rejected.");
}
System.out.println("Steps:");
pda.printSteps();
}
}