Reg Ex Code Gen Tutorial
Reg Ex Code Gen Tutorial
WWW.S-SOLUTIONS.INFO
RegExCodeGen
Introduction
The objective of this document is demonstrating the use of this software. The aim of this
software is to generate Finite Automata graphs or Java Code Classes for any Regular
Expression. The following steps describes the functionality of the tool:
1.
2.
3.
The first window that will be shown upon the start of the software (figure 1) will oer two
options:
Regular Expression to Finite Automata (FA) and Java Code
Finite Automata to Java Code
figure 1
PAGE 1 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
figure 2
After the regular expression has been entered, in order to get the NFA, the NFA checkbox
should be selected, and then click on Save button, then the save dialog will appear asking you to
select the name and the directory of the output.
Then, in the selected directory a folder named GMLFiles will be created containing the NFA
graph in form of .gml file, that can be opened using yEd Works 3.9.1 (Section 5).
The corresponding generated NFA graph for the given regular expression example in figure 2
looks like the graph shown in figure 3.
PAGE 2 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
figure 3
where:
PAGE 3 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
2. NFA to DFA
There isnt any additional step to do in order to generate the corresponding DFA graph, except
selecting the DFA checkbox from figure 2 and click Save.
The DFA graph will be generated in the same folder with the DFA sux (eg. tutorialDFA.gml).
The corresponding automaton for the NFA graph from figure 3 is shown in figure 4:
figure 4
In cases where Initial state is also an Accepted state, the node looks like each other accepted
node, but it can be recognized by label number 0(zero).
When the DFA checkbox is selected after clicking the Save button, the Transition Table button
can be clicked that provides the transition table (figure 5) description how the DFA has been
built from the NFA.
PAGE 4 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
!
!
tutorial automaton = new tutorial();
!
!
char nextInt;
!
!
System.out.print("Enter the input string: ");
!
!
Scanner scanner = new Scanner(System.in);
!
!
String input = scanner.nextLine();
!
!
for (int i = 0; i < input.length(); i++) {
!
!
!
nextInt = input.charAt(i);
!
!
!
automaton.update(nextInt + "");
!
!
}
!
!
String message = "";
!
!
if (automaton.matches()) {
!
!
!
message = "The input string: " + input!
+ " is
accepted by this automaton.";
!
!
} else {
!
!
!
message = "The input string: " + input!
+ " is NOT
accepted by this automaton.";
!
!
}
!
!
System.out.println(message);
!
}
}
PAGE 5 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
tutorial.java
/** @author: Suejb Memeti
*The automaton is generated for this regular expression:
(a*[0..5]?)|(b+c)
*/
public class tutorial {
!
!
!
!
!
!
!
protected
protected
protected
protected
protected
protected
protected
!
!
!
!
!
!
'0'
!
!
!
!
'0'
!
!
!
!
!
!
!
!
!
!
public boolean matches() {
!
!
if ((currentState == state4 || currentState == state2
|| currentState == state1 || currentState == state0))
!
!
!
return true;
!
!
else
!
!
!
return false;
!
}
}
PAGE 6 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
If we select the graph approach the following java code will be generated that represents the
same automaton:
tutorial.java
import
import
import
import
import
import
public
grail.interfaces.DirectedEdgeInterface;
grail.interfaces.DirectedNodeInterface;
grail.iterators.EdgeIterator;
grail.iterators.NodeIterator;
grail.properties.GraphProperties;
grail.setbased.SetBasedDirectedGraph;
class tutorial {
!
private SetBasedDirectedGraph graph = new
SetBasedDirectedGraph();
!
private static DirectedNodeInterface state;
!
private static DirectedNodeInterface deadState;
!
private static DirectedNodeInterface currentState;
!
protected DirectedEdgeInterface edge;
!
public tutorial() {
!
!
buildGraph();
!
!
deadState = graph.createNode(-1);
!
!
deadState.setProperty(GraphProperties.DESCRIPTION,
"DeadState");
!
!
graph.addNode(deadState);
!
}
!
protected void buildGraph(){
!
!
//Creating Graph Nodes (Automaton States)
!
!
state = graph.createNode(4);
!
!
state.setProperty(GraphProperties.LABEL,"4");
!
!
state.setProperty(GraphProperties.DESCRIPTION,"Accepted");
!
!
graph.addNode(state);
!
!
state = graph.createNode(3);
!
!
state.setProperty(GraphProperties.LABEL,"3");
!
!
state.setProperty(GraphProperties.DESCRIPTION,"null");
!
!
graph.addNode(state);
!
!
state = graph.createNode(2);
!
!
state.setProperty(GraphProperties.LABEL,"2");
!
!
state.setProperty(GraphProperties.DESCRIPTION,"Accepted");
!
!
graph.addNode(state); ....
PAGE 7 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
!
!
//Creating Graph Edges (Automaton Transitions)
!
!
edge = graph.createEdge(null,(DirectedNodeInterface)
graph.getNode(3),
!
!
!
(DirectedNodeInterface) graph.getNode(3));
!
!
edge.setProperty((GraphProperties.LABEL), "b");
!
!
graph.addEdge(edge);
!
!
edge = graph.createEdge(null,(DirectedNodeInterface)
graph.getNode(3),
!
!
!
(DirectedNodeInterface) graph.getNode(4));
!
!
edge.setProperty((GraphProperties.LABEL), "c");
!
!
graph.addEdge(edge);
!
!
edge = graph.createEdge(null,(DirectedNodeInterface)
graph.getNode(2),
!
!
!
(DirectedNodeInterface) graph.getNode(1));
!
!
edge.setProperty((GraphProperties.LABEL), "0..5");
!
!
graph.addEdge(edge);
!
!
....
!
public boolean update(String edge){
!
!
NodeIterator succ = currentState.getSuccessors();
!
!
!
while(succ.hasNext()){
!
!
!
!
succ.next();
!
!
!
!
EdgeIterator edges =
graph.getAllEdges((DirectedNodeInterface)currentState,
(DirectedNodeInterface)succ.getNode());
!
!
!
!
while(edges.hasNext()){
!
!
!
!
!
edges.next();
!
!
!
!
!
if(edges.getEdge().getProperty(GraphProperties.LABEL).equals(edge))
{
!
!
!
!
!
!
currentState =
(DirectedNodeInterface) succ.getNode();
!
!
!
!
!
!
return true;
!
!
!
!
}
!
!
!
}
!
!
}
!
!
currentState = deadState;
!
!
return false;
!
}
public boolean matches() {
!
!
if
(currentState.getProperty(GraphProperties.DESCRIPTION).equals("Acce
pted"))
!
!
!
return true;
!
!
else
!
!
!
return false;
!
}
}
PAGE 8 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
characters)
In the figure 7 is shown an example of a drawn graph using yEd.
When you are ready with the graph simply choose from file menu FA to JavaCode then the
windows as in figure 6 will appear from where you can choose the graph file and select the
desired output as in examples before.
PAGE 9 OF 10
LINNAEUS UNIVERSITY
TUTORIAL
WWW.S-SOLUTIONS.INFO
figure 6
figure 7
5. Requirements
To open and draw new gml files you will need yEd Works, for better performance use yEd
Works version 3.9.1.
To run graph approach Java Code Classes you need grail library that can be downloaded from
the project website (www.s-solutions.info) and from ocial library web site.
PAGE 10 OF 10
LINNAEUS UNIVERSITY