Giter VIP home page Giter VIP logo

re_to_dfa's Introduction

Regex to DFA by direct method

A simple regex compiler in python which converts the regular expression into a dfa, which can then be simulated to check if a given string matches the pattern...

See main.py for usage examples

Features

  • Convert regex to dfa
  • Match regex with a string
  • Supports additional operators like + (kleene), ? (optional) in regex
  • Supports additional symbols like \w, \s, \d, .

How to Use

  • The program recognizes the following symbols and operators in the regex

    Symbol Meaning
    $ epsilon (empty string)
    \s Matches any whitespace characters
    \w Alphanumeric or underscore
    \d Digits (0-9)
    . All except newline
    Any ascii/unicode character
    Operator Meaning
    * Kleene Star (zero or many)
    + Kleene Plus (One or many)
    ? Optional (zero or one)
    | OR
    . Concat (for internal use)
    ( ) brackets
  • Note: To use any character which is an operator/symbol like '+' or '?', append '\' before it.

    • Like: \. for '.', \+ for '+', \? etc.

How it works

  1. Preprocess the regex
    1. Replace all the characters/token in the regex with a integer code (This makes it easier to support additional operators like +,?,\s,\d etc.)
    2. Support + and ? operators (Replace all instances of x+ with xx* and x? with (x | epsilon) [epsilon=empty string])
    3. Add concatenate operator in the regex and augment # in the end to indicate end: (a|b)abb -> (a|b).a.b.#
  2. Convert the regex to postfix
  3. Generate Syntax tree from postfix regex
    • Assign every leaf node an unique id
  4. Calculate nullable, firstpost, lastpos, followpos
  5. Generate DFA from Syntax tree using the following algorithm:
    • algo

re_to_dfa's People

Contributors

fnvir avatar

Stargazers

 avatar  avatar

Watchers

 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.