Keywords – Code Parser, Generation, Regular Expression, Finite

advertisement
An Approach for Code Generator- ENUM
Rashmi Sinha1 & Ashish Dewangan 2
1Department of Computer Science & Engineering, 2Department of Electronics & Telecommunication Engineering,
1&2
Chhattisgarh Swami Vivekanand Technical University, Durg, Chhattisgarh, India
E-mail : 1sinha.rashmi715@gmail.com, 2ashishdewangan@csitdugr.in
Abstract – The code generator is fully functional and
feature rich, now fully unit tested. Translation or
code generation of regular expressions and automata
to java code help to remove certain problem and
make it efficient by reducing complexity. This paper
illustrates the method by which this RE and FA’s
will be converted to code using enumeration.
automatic way of implementing finite automata into
Java Code.
A. Finite Automata
The Finite Automata(FA) stores finite amount of
information (finiteness of memory), which is great
because we can do things that in general cannot be done
with programs. For the FA programs you know what
that programdoes, or if there is a shorter program or
another one that does the same thing as the given
automata. Finite automata so called finite automaton is
built of a (finite) collection of states, and each state has
name and represents what is remembered about its
history. The process of changing the current state of an
automaton in response to input characters is called a
transition.
Keywords –
Code Parser, Generation, Regular
Expression, Finite Automata, ENUM approach.
I.
INTRODUCTION
In the case of automatic code generation, semantics
refer to the meaning of modelling elements (symbols)
and how they are interpreted. The semantics of textual
or graphical formalism define the meaning of program
written in it. Several commercial tools are available to
provide support for translation of regular expression and
finite automaton to executable code. Code generator is
“a software tool that accepts as input the requirements or
design for a computer program and produces source
code that implements the requirements or design” [3].
Fig. 1: Automaton that represent the concatenation (ab)
The idea of automatic software generation has
regained strength during the last years, particularly for
enterprise applications. The development of these
applications, which include support for distributed
processing across the Internet and multi-layered
architectures.
B. Regular Expression
Manysystemsthatinsomewaydescribepatternsusereg
ularexpressions.Usuallythey are invisible because are
embedded in the code of the company, but sometimes
they are visible, like the UNIX commands. There are
many UNIX commands that have in some way the
notation of extended regular expression for an input.
Regular Expressions matches the pattern of strings in a
very efficient manner, whether these strings are simple
or complicated. Regular Expressions technology has
been found useful in the description of a defined class of
patterns in text.
Nowadays the old dream of Computer Science for
creating machines that can program themselves has
shown a remarkable progress of becoming
real.Implementingfiniteautomataforregularexpressionisa
processthattakestoomuch time, depending on the regular
expression complexity. This project introduces an
ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013
25
International Journal on Advanced Computer Theory and Engineering (IJACTE)
AnumberoflexicalanalysergeneratorstakesREasinpu
ttodescribethetokensandasa return statement produces a
single finite automaton that recognizes any token. Any
sequence of letters or digits may be expressed as:
B. Conversion to NFA
It turns out that every Regular Expression has an
equivalent NFA advice versa. The RE is divided in
several sub expressions, and is shown by a common
tree, and every sub expression is a sub tree in the main
common tree.
[A….z][A….z]|[0…9]*
The goal of the Subset Construction Algorithm is
converting a NFA graph with zero or more epsilon
transitions and multiple transitions on a single character
into an equivalent DFA graph with no epsilon transitions
and unique transition on a single character that means a
unique path for each accepted sequence of input
characters.
C. Code Parser
Code parsing is the process of where elements of a
text are analysed as tokens to find out the structure text
under some constraints. This parses the source code to
create some form of internal representation. Here the
java source code file is given as input. Code parser
breaks the input code in to the small elements which are
residing in the software. The output of the source code is
in some predetermined format as we have to save this in
to database in our system.
The Algorithm for the Code Parser can be given as
follows:
Step 1: To get a source code input file (like java file)
Step 2: Perform bit level analysis
Step 3: Use Regular expression
Step 4: Separate the tokens
Fig. 3 : Construction of graph forAsterisk, Plus and
QMark nodes
Step 4: Output is the metadata
Step 5: Send and save output in database
Input
Code Parser
The subset algorithm works as follows
Parsed Output
1.
The start state of DFA are all states of the NFA that
can be reached with empty transition
2.
For each new state of DFA do:
For each character of alphabet move to all reached
states in that character perform an empty closure for the
set of states we got (the result from empty closure can
be a new state or an already existing State).
Database
3.
Fig. 2 : Working of Code Parser
II. MODEL ANALYSIS
If at least one of the states from the result is
accepted state in NFA it is accepted state in DFA as
well.
A. Specifying Grammar
C. Java Class from DFA
The input expression is divided into group of
tokens which is done with the help of Thompson
Algorithm. And ANTLR can create AST’s(Abstract
Syntax Tree). The tokens is used to quantify them
earning less stream of characters into discrete groups
that have meaning when processed by the parser. The
parser generates error for the sequences of tokens
that cannot match to the specific syntactical
arrangements allowed, as decreed by the grammar.
Each of these graphs represents a deterministic
finite
automaton
that
can
be
easily
convertedintoaJavaClassthatacceptsallthestringpatternsa
sthegraphitself. Generators or Application generators
area software tool that helps programmers to generate a
completeprogramorpartofitinaveryquickwayaccordingtot
hegiveninputspecification [17]. Using code generators
the programmer can easily edit or modify and execute
the output source (program).The major advantages of
using code generators are:
ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013
26
International Journal on Advanced Computer Theory and Engineering (IJACTE)
•
Saving a lot of development time
•
Useful as a learning tool for writing code
...
•
Programs are easy to modify and maintain
DeadState {
},
There are many ways to interpret an automaton
using Java Code, but we will talk more about most
used ones, and we will describe the ones used in
this project. One way of representing FSA is using
enums.
@Override
public State next(Input input) {
return DeadState;}
};
III. ENUM APPROACH
Enums are essentially list of classes, and each
member of the enum may have a different
implementation. Each enum element may have a
different implementation. For example, let us assume
that we want to implement the automaton, that represent
this regular expression R =^(a+)(b*)(c*)$.
Benefits of Enums:
An enumeration is a special class, which provides
a type-safe implementation of constant data in your
program.The advantage of using enum approach is that
it is clean and simple approach. All the logic of the
automaton is in the same place and it is easy to trace the
automaton. Each enum element describes its
functionality, by this we mean that each transition is
defined.
2) Enum has its own name-space.
1) Enum is type-safe you can not assign anything else
other than predefined Enum constants to an Enum
variable. It is compiler error to assign something
else unlike the public static final variables used in
Enum int pattern and Enum String pattern.
3) Best feature of Enum is you can use Enum in Java
inside Switch statement like int or char primitive
data type. We will also see example of using java
enum in switch statement in this java enum tutorial.
4) Adding new constants on Enum in Java is easy and
you can add new constants without breaking
existing code.
We can write the states as elements of the Enum
State as follows:
IV. CONCLUSIONS
Table 1 : The Enum Approach
This paper provides an idea about the analysis of
regular expression and automata, that how they will be
converted to executable code by using an ENUM
approach of code generation. This shows that Enum
approach is having a better functionality. As its
implementation takes larger memory space because for
each sate enum element is created.
enum States implements State {
State0 {
@Override
public State next(Input input) {
switch(input.read()) {
A better solution is required to simplify and
minimize the source code, but so far ithas not been
proven that they are correct for each case, so that is a
work to be done in future.Providing the enum and the
map like approach while implementing finite automata
usingthe source code generator will help programmers
increase the productivity.
case 'a': return State1;
default: return DeadState;} }
},
State1 {
@Override
V. ACKNOWLEDGMENT
public State next(Input input) {
This paper has been kept on track and been seen
through to completion with the supportand
encouragement of numerous people including my wellwishers and my friends.At this moment of
accomplishment, first of all I pay homage to my guide.
This work would not have been possible without his
guidance, support and encouragement. Under his
switch(input.read()) {
case 'a': return State1;
case 'b': return State2;
case 'c': return State3;
case '': return null;
default: return DeadState;} }
ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013
27
International Journal on Advanced Computer Theory and Engineering (IJACTE)
guidance I successfully overcame many difficulties and
learned a lot. I can’t forget his hard times.
[7]
Javarevisited (2011), ENUM in JAVA, [Online],
Available:http://javarevisited.blogspot.com/2011/
08/enum-in-java-exampletutorial.html#ixzz2Ka2Binop.
VI. REFERENCES
[1]
A.V.Aho and J. D. Ullman “Patterns,Automata
and Regular Expressions” in Foundations of
Computer Science, NewYork:W.H. Freeman &
Company, 2010.
[8]
Arora, Sh. Bansal and A. Arora “ Application
Generators” in Comprehensive Computer and
Languages, New York: Firewall Media, 2005, ch.
2 sec. 4.1, pp. 41
[2]
P. Linz “Introduction toTheory of Computation;
FiniteAutomata; Regular Languages and Regular
Grammars” inAn Introduction to Formal
Languages andAutomata, 3rd ed. Massachusetts:
Jones & Bartlett Learning, 2010.
[9]
R.Sinha at International Journal of Computer
Trends & Technology “Transmutation of Regular
Expression to Source Code using Code
Generators”,Vol 3(6), 2012, pp 787-791.
[10]
[3]
IEEE Standard Glossary of Software Engineering
Terminology/IEEE Std 610.12-1990. Inst of Elect
& Electronic, 2009.
Andrew J. Kornecki,Sona Johri,” Automatic
Code Generation: Model – Code Semantic
Consistency”, 2007.
[11]
[4]
Suejb Memeti, “Automatic Java Code Generator
for Regular Expression and Finite Automata”,
Degree Project, Course code: 5DV00E, 2012.
Christin
Lungu
(2012),
Automaton
Implementation in Java, [Online], Available:
http://java.dzone.com/articles/automatonimplementation-java
[5]
K. Czarnecki and S. Helsen, “Feature-based
survey of model transformation approaches,”
IBM Systems Journal, vol. 45, no. 3, pp. 621–
645, 2006.5DV00E, 2012.
[6]

ANTLR (2005), Abstract Syntax Tree, [Online],
Available: http://www.antlr2.org/doc/trees.html.
ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013
28
Download