Ruby DSL to describe Automata

24 Oct

Recently, I built an internal DSL using Scheme to describe Deterministic Finite State Automata. It was quite easy to do through Scheme Macros, whose are really powerful (and hard to understand). The article “S. Krishnamurti. Automata via Macros. Journal of Functional Programming, Volume 16 , Issue 3 (May 2006)” is a great reference if you want to go deeply.

A macro to describe automata in Scheme would be as:

(define-syntax automaton
  (syntax-rules (:)
    [(_ init-state
        (state : response ...)
        ...)
     (let-syntax
         ([process-state
           (syntax-rules (accept ->)
             [(_ accept)
              (lambda (stream )
                (cond
                  [(empty? stream ) true]
                  [else false]))]
             [(_ (label -> target ) (... ...))
              (lambda (stream)
                (cond
                  [(empty? stream ) false]
                  [else
                   (case (first stream )
                     [(label ) (target (rest stream ))]
                     (... ...)
                     [else false])]))])])
       (letrec ([state
                 (process-state response ...)]
                ...)
         init-state ))]))

Imagine it’s needed to recognize chars sequences like c[ad]*r. Such macro, would allow you to describe a recognizer automaton for these char sequences:

(define cdar-sequence?
  (automaton init
             [init : (c -> more)]
             [more : (a -> more)
                   (d -> more)
                   (r -> end)]
             [end : accept]))

Now you can test any char sequence:

(cdar-sequence? '(c a d a d r)) ; => #t
(cdar-sequence? '(c a r a d r)) ; => #f

Well, I have to do it now in Ruby. Has anyone built some Ruby internal DSL to describe automata? I really wanted to describe them such as:

automaton "recognizer" do
  initial_state :state1 do
    transition 'c', state2
    transition 'd', state3
  end

  state :state2 do
    transition 'c', state3
  end

  final_state :state3
end

I haven’t found anything and probably will make something. What do you think?

Advertisement

3 Responses to “Ruby DSL to describe Automata”

  1. Tiago Albineli Motta June 7, 2008 at 3:35 pm #

    Vc ta conseguindo me convencer da beleza do Ruby. Fiz hoje umas paradinhas nessa linguagem que ficaram muito interessante. Dá uma olhada lá no meu blog.

    De qualquer forma, mantenho minhas criticas ainda haha. Preciso de mais motivos.

  2. Hugo Baraúna March 20, 2009 at 2:40 am #

    Agora é minha vez de fazer essa DSL em Scheme e surgiu a mesma vontade de ver isso em ruby ou fazê-lo em Ruby. E aí, chegaste a fazer?

  3. Fabio Kung March 20, 2009 at 2:51 pm #

    @Hugo

    I’ve just submitted both internal DSLs (written in scheme and ruby) to github:

    http://github.com/fabiokung/scheme-dsl-for-automata
    and
    http://github.com/fabiokung/kungpiler (simple PL/0 compiler that uses the Ruby Automata DSL)

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.