# Parsing in Factor

## Chris Double — 2007-11-30 00:54:23

    I wrote a Packrat parser in Factor, with an embedded DSL for

    generating grammars;

    http://www.bluishcoder.co.nz/2007/11/embedded-grammars-in-factor.html

    With this you can write Factor code like:

    <EBNF

    expr = number {('+'|'-') number}

    EBNF>

    This creates a word 'expr' that can parse text like "123+456-789". The

    blog post shows the full grammar for the PL/0 programming language and

    it looks very much like the definition of the language from Wikipedia.

    The EBNF grammar parser is written using a series of packrat parser

    combinator words I wrote.  The grammar above using the low level words

    would look like:

    : expr ( -- parser )

      [

        number ,

        [

          [

            "+" token , "-" token ,

          ] { } make choice ,

          number ,

        ] { } make seq repeat0 ,

      ] { } make seq ;

    It's still a work in progress and I'm currently extending it to allow

    left recursive rules, but hopefully it'll make designing domain

    specific languages easier.

    Chris.

    --

    http://www.bluishcoder.co.nz