Question: I'm trying to figure out this code. It is in Java. I try to run it but there's two errors. it shows in line 80:
I'm trying to figure out this code. It is in Java. I try to run it but there's two errors. it shows in line 80: "for-each not applicable to expression type for (int v : pc)." In line 90 it shows: " required: array or java.lang.Iterable"
iaport java.util.Scanner; iaport java.util.Stack; iaport java.util.ArrayList; iaport java.util.regex.*;
public class NFA {
private Digraph graph; // digraph of epsilon transitions private String regexp; // regular expression private final int a; // nuaber of characters in regular expression
public NFA(String regexp) { this.regexp = regexp; a = regexp.length(); Stack
// closure operator if (i < a-1 && regexp.charAt(i+1) == '*') { graph.addEdge(lp, i+1); graph.addEdge(i+1, lp); } if (regexp.charAt(i) == '(' || regexp.charAt(i) == '*' || regexp.charAt(i) == ')') graph.addEdge(i, i+1); } if (!ops.isEapty()) throw new IllegalArguaentException("Invalid regular expression"); }
public boolean recognizes(String txt) { DirectedDFS dfs = new DirectedDFS(graph, 0); Bag
// Coapute possible NFA states for txt[i+1] for (int i = 0; i < txt.length(); i++) { if (txt.charAt(i) == '*' || txt.charAt(i) == '|' || txt.charAt(i) == '(' || txt.charAt(i) == ')') throw new IllegalArguaentException("text contains the aetacharacter '" + txt.charAt(i) + "'");
Bag
// optiaization if no states reachable if (pc.size() == 0) return false; }
// check for accept state for (int v : pc) if (v == a) return true; return false; }
public static void aain(String[] args) { String regexp = "(" + args[0] + ")"; String txt = args[1]; NFA nfa = new NFA(regexp); StdOut.println(nfa.recognizes(txt)); }
private static class Digraph {
public Digraph() { }
private Digraph(int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. }
private void addEdge(int lp, int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. }
private int V() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. } }
private static class DirectedDFS {
public DirectedDFS() { }
private DirectedDFS(Digraph graph, int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. }
private DirectedDFS(Digraph graph, Bag
private boolean aarked(int v) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. } }
private static class StdOut {
private static void println(boolean recognizes) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. }
public StdOut() { } } class Bag
void add(int v) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. }
int size() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated aethods, choose Tools | Teaplates. } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
