0% found this document useful (0 votes)
33 views

Reg Ex Code Gen Tutorial

This document provides a tutorial for a software tool that can: 1. Generate a nondeterministic finite automaton (NFA) graph from a regular expression input. 2. Translate the NFA to an equivalent deterministic finite automaton (DFA). 3. Generate Java code classes to represent the DFA as an object-oriented program, using either an if-else approach or graph approach. The software allows the user to either provide a regular expression and generate the corresponding NFA, DFA and Java code, or provide an existing finite automaton graph file to generate just the Java code. The tutorial explains the software functionality and provides examples of the output generated at each step.

Uploaded by

lmbf757
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Reg Ex Code Gen Tutorial

This document provides a tutorial for a software tool that can: 1. Generate a nondeterministic finite automaton (NFA) graph from a regular expression input. 2. Translate the NFA to an equivalent deterministic finite automaton (DFA). 3. Generate Java code classes to represent the DFA as an object-oriented program, using either an if-else approach or graph approach. The software allows the user to either provide a regular expression and generate the corresponding NFA, DFA and Java code, or provide an existing finite automaton graph file to generate just the Java code. The tutorial explains the software functionality and provides examples of the output generated at each step.

Uploaded by

lmbf757
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

Generating Nondeterministic Finite Automata from the RegularExpression input.

2.

Translating the NFA into an equivalent Deterministic Finite Automata.

3.

Generating Java Code Classes for the DFA automaton.

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

1. Regular Expression to NFA


After RegEx to FA & JavaCode option from File menu of the main window has been selected a
new window will appear (figure 2), that expects as an input a valid regular expression (eg.
(a*[0..5]?)|(b+c) ) as described in section 1.1.

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:

1.1. Regular Expression Convention


The default convention has been kept during this software, that means that you can write regular
expression in the right way without character escape symbols.
Conventionally the Kleene closure is written by adding the star * symbol after the subexpression
you want, eg: a*, (a|b)* [A..z]* ...
The positive closure is written in the same way as Kleene closure but here instead of * you write
+, eg: a+, (a|b)+ [A..z]+ ...
The character tha represents zero or one subexpression is the question mark symbol ?, so you
can create subexpression by adding it at the end, eg: a?, (a|b)? [A..z]? ...
The union closure is expressed by the vertical bar symbol |, eg a|b (ab)*|c ...
The range between two characters is expressed in this way: a..z, 0..9
The symbols like ?,+,*,| are reserved by the grammar, but you can use every other symbol as: '@',
'#', '$', '%', '!', '~', '`', '^', '.', ',', ';', '<', '>', '/', '-'.
And of course you can use any letter or number as alphabet.
An example of how a regular expression that accepts all float values is:
([1..9][0..9]*|0?.[0..9]+)|-(([1..9][0..9]+)|0.0*[1..9][0..9]*)

or the regular expression that validates email addresses:


[A..z]([A..z]|[0..9])+@([A..z]|[0..9])+(.[a..z]+)+

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

3. DFA to Java Code


In order to generate the Java Code Classes for the corresponding regular expression there are
two options to select: if-else approach and graph approach.
A folder JavaCode will be created in the selected directory, containing the generated Java
Classes. In our example while selecting the if-else approach the following code will be generated:
Main.java
import java.util.Scanner;
public class Main {
!

public static void main(String[] args) {

!
!
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

final int state4 = 4;


final int state3 = 3;
final int state2 = 2;
final int state1 = 1;
final int state0 = 0;
final int deadState = -1;
int currentState = 0;

!
!
!
!
!
!
'0'
!
!
!
!
'0'
!
!
!
!
!
!
!
!
!

public void update(String edge) {


!
if(currentState == state3 && edge.equals("b")) {
!
!
currentState = state3;
!
}else if(currentState == state3 && edge.equals("c")) {
!
!
currentState = state4;
!
}else if(currentState == state2 && ( edge.charAt(0) >=
&& edge.charAt(0) <= '5')) {
!
!
currentState = state1;
!
}else if(currentState == state2 && edge.equals("a")) {
!
!
currentState = state2;
!
}else if(currentState == state0 && ( edge.charAt(0) >=
&& edge.charAt(0) <= '5')) {
!
!
currentState = state1;
!
}else if(currentState == state0 && edge.equals("a")) {
!
!
currentState = state2;
!
}else if(currentState == state0 && edge.equals("b")) {
!
!
currentState = state3;
!
}else {
!
!
currentState = deadState;
!
}
}

!
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

The Main.java class is exactly the same as in if-else approach.


When the graph approach has been used you should keep in mind that the grail library should be
built to path.

4. Finite Automata to Java Code


In case you have the NFA or DFA but you are missing the Regular Expression and you need the
JavaCode, the software provides the opportunity to import the drawn graph in form of .gml file.
To draw the graph using yEd Works Software you should follow the rules below:
Initial state should be labeled as Initial, I, Start or S.
Accepted State should be labeled as Accepted, A, Final or F.
The empty edge should be labeled with empty or
Make sure all edges are labeled.
The other states can be labeled anyway.
The interval edges should be labeled as: A..Z or 0..9 (represent the range between two

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

You might also like