Git Product home page Git Product logo

regextodfa's Introduction

Regex To DFA In Java

This is how to convert regex (REGular EXpression) to DFA by creating and using syntax tree in Java language.

This project is a part of a bigger project we'd done for Compiler Course to create a simple compiler.


Getting Started

๐Ÿ”น Watch this video to comprehend the concepts: https://www.youtube.com/watch?v=uPnpkWwO9hE

Pay attention to some Rules:

rule3 rule2

NetBeans is the IDE I coded in. you can clone this project and import it in NetBeans easily.

The classes used, are as follows:

  • RegexToDfa
  • SyntaxTree
  • BinaryTree
  • Node
  • LeafNode
  • DfaTraversal
  • State

here is an initialize method which is called in the main function:

public static void initialize() {
    DStates = new HashSet<>();
    input = new HashSet<String>();

    String regex = getRegex();
    getSymbols(regex);

    SyntaxTree st = new SyntaxTree(regex);
    root = st.getRoot();
    followPos = st.getFollowPos();

    State q0 = createDFA();
    DfaTraversal dfat = new DfaTraversal(q0, input);    
}
  • DStates is a Set of States which is used for creating the final dfa.

  • input is also a Set which holding the characters of the input regular expression taken from user.

    pay attention to this issue that, some characters like '*' can be used as an operator (closure, union, concatination, ...)

    so if you want to enter these characters just as a normal character, you could bring a backslash '\' following up the intended character

    for example "\*" meaning a normal '*' character. and "*" meaning star opeartor (closure)
    this is why we use a set of String for declaring input variable.

  • String regex = getRegex(); is for getting the intended regular expression from stdin.

  • getSymbols(regex); this line of code sets the input Set.

  • SyntaxTree st = new SyntaxTree(regex); and this line creates the corresponding syntax tree of the inputted regex.

  • root = st.getRoot(); gets the root of the syntax tree.

  • followPos = st.getFollowPos(); make a new refference to the Set of followpos variable.

  • State q0 = createDFA(); creates the DFA through using the syntax tree and assigns q0 the start state of resulted DFA.

  • DfaTraversal dfat = new DfaTraversal(q0, input); makes a new DFA Traversal object for traversing the resulted DFA and recognizing whether the DFA can accept a particular string or not.


Proof Of Concepts

/*
    ***
        (a|b)*a => creating binary syntax tree:
                            .
                           / \
                          *   a
                         /
                        |
                       / \
                      a   b
    ***
*/

if you look at the SyntaxTree class, you will understand that a BinaryTree object is created.

so a syntax tree is a binary tree with some new special attributes (firstpos, lastpos, nullable, followpos).

in the BinaryTree class, the inputted regex is going to be converted to a tree which contains some nodes.

the last most bottom nodes are called, the leaf nodes.

So, now you know what the Node and LeafNode are used for.


Running the tests

click here to see the acceped result.

click here to see the rejected result.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • Fragmenting section of the regex, using the stack, and also doOperation is adopted by a fine code of @felipemoura in github.

regextodfa's People

Contributors

alirezakay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.