• Tidak ada hasil yang ditemukan

An Approach for Code Generator- ENUM

N/A
N/A
Protected

Academic year: 2024

Membagikan "An Approach for Code Generator- ENUM"

Copied!
4
0
0

Teks penuh

(1)

ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013

25

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 : 1[email protected], 2[email protected]

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.

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].

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.

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

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.

Fig. 1: Automaton that represent the concatenation (ab) 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.

(2)

International Journal on Advanced Computer Theory and Engineering (IJACTE)

ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013

26 AnumberoflexicalanalysergeneratorstakesREasinpu ttodescribethetokensandasa return statement produces a single finite automaton that recognizes any token. Any sequence of letters or digits may be expressed as:

[A….z][A….z]|[0…9]*

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 Step 4: Output is the metadata

Step 5: Send and save output in database

Fig. 2 : Working of Code Parser II. MODELANALYSIS A. Specifying Grammar

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.

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.

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.

Fig. 3 : Construction of graph forAsterisk, Plus and QMark nodes

The subset algorithm works as follows

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).

3. If at least one of the states from the result is accepted state in NFA it is accepted state in DFA as

well.

C. Java Class from DFA

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:

Input Code Parser Parsed Output

Database

(3)

International Journal on Advanced Computer Theory and Engineering (IJACTE)

ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013

27

• Saving a lot of development time

• Useful as a learning tool for writing code

• Programs are easy to modify and maintain 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.

III. ENUMAPPROACH

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*)$.

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.

We can write the states as elements of the Enum State as follows:

Table 1 : The Enum Approach enum States implements State {

State0 { @Override

public State next(Input input) { switch(input.read()) { case 'a': return State1;

default: return DeadState;} } },

State1 { @Override

public State next(Input input) { switch(input.read()) { case 'a': return State1;

case 'b': return State2;

case 'c': return State3;

case '': return null;

default: return DeadState;} }

}, ...

DeadState { @Override

public State next(Input input) { return DeadState;}

};

Benefits of Enums:

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.

2) Enum has its own name-space.

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.

IV. CONCLUSIONS

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.

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.

V. ACKNOWLEDGMENT

This paper has been kept on track and been seen through to completion with the supportand encouragement of numerous people including my well- wishers 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

(4)

International Journal on Advanced Computer Theory and Engineering (IJACTE)

ISSN (Print) : 2319 – 2526, Volume-2, Issue-5, 2013

28 guidance I successfully overcame many difficulties and learned a lot. I can’t forget his hard times.

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.

[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.

[3] IEEE Standard Glossary of Software Engineering Terminology/IEEE Std 610.12-1990. Inst of Elect

& Electronic, 2009.

[4] Suejb Memeti, “Automatic Java Code Generator for Regular Expression and Finite Automata”, Degree Project, Course code: 5DV00E, 2012.

[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.

[7] Javarevisited (2011), ENUM in JAVA, [Online], Available:http://javarevisited.blogspot.com/2011/

08/enum-in-java-example- tutorial.html#ixzz2Ka2Binop.

[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

[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] Andrew J. Kornecki,Sona Johri,” Automatic Code Generation: Model – Code Semantic Consistency”, 2007.

[11] Christin Lungu (2012), Automaton Implementation in Java, [Online], Available:

http://java.dzone.com/articles/automaton- implementation-java



Referensi

Dokumen terkait