[stack] Parsing in Factor

"Chris Double" <[email protected]>
Newsgroups gmane.comp.lang.concatenative
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.