Posts Tagged ‘Scheme’

Parsers and Combinators

Posted in Scheme on June 26th, 2008 by Lorenz Pretterhofer – 1 Comment

Last time I showed some brief examples of what a parser combinator is, how they work and how I intended to use them to solve the parsing problem using the Scheme programming language. This time we get to start the interesting stuff…

To get things going, last time we looked at a simple character facility which returns a parser that will only accept that particular character. We can also use this technique to select from a range of characters.

(define (char-range start end)
  (λ (state)
    (let ([rc (read-char state)])
      (cond ((eof-object? rc) fail)
            ((and (char>=? rc start)
                  (char<=? rc end))
             rc)
            (else fail)))))

We might also wish to repeat the actions of a parser, which involves writing a proper combinator. Actually, this is the first real utility parser we'll see and it simply allows us to infinitely repeat the parser provided.

(define (many p)
  (letrec ([mp (λ (state acc)
                 (aif result fail? ((with-mark p) state)
                      acc
                      (mp state (cons result acc))))])
    (λ (state)
      (reverse (mp state '())))))

This defines a small function that accumulates a list (backwards as usual) by repeatedly applying the parser (p). The with-mark stuff is the second combinator we'll look at, which is used here to prevent the stream position from moving beyond the last match.

The description of many would be a combinator that continues to apply a parser until it fails, reverting the parser's state to the state prior to running the final failing application of the parser.

The definition of with-mark is even simpler (or should this be with-state?).

(define (with-mark parser)
  (λ (state)
    (let ([mark (file-position state)])
      (aif v fail? (parser state)
           (begin (file-position state mark)
                  v)
           v))))

Our parser state is of course still non-functional, based simply on the port capabilities built into Scheme. We check to make sure the parser doesn't fail, and if it does we reset the parsers state by recording it before hand. Non-functional user state can be stored in a larger closure/environment, and this could be changed to support functional state as a second variable (in a data structure or list), but I'll leave to a later post, or the reader of course.

Running our parser is actually this simple (from the repl).

(define digit
  (char-range # #9))

(define state (open-input-string "12345"))

((many digit) state)

There is one slight problem with this... It accepts any number of digits... including zero digits! I've provided a second version called many1, but you could actually define it like the following snipped. (Note: See the bottom of this post to find the source code as it currently appears).

Ok, we still need a way to parse whitespace, and to abstract the tokens of whatever language we're implementing. To help us here we can write a whitespace parser and a token combinator which will help us.

(define ws-char
  (λ (state)
    (let ([rc (read-char state)])
      (cond ((eof-object? rc) fail)
            ((char-whitespace? rc) rc)
            (else fail)))))

(define whitespace
  (many1 ws-char))

(define (wstok tparser)
  (mand (opt whitespace)
        tparser))

I'd just like to point out before we more on... the ws-char parser here actually recognizes any Scheme whitespace character. I could have written this parser to recognize some other whitespace character set but I felt that this would serve us better for now, since we might eventually want to test our method against the existing Scheme language, or at least a sizable subset.

Again, using this is quite simple... to recognize a number token we can simple define the number token parser as (wstok (many1 digit)).

Now we still have one minor issue here... what if we don't want to simply recognize the language, but also produce some direct result. For example, our parser so far can recognize number tokens, but it doesn't return those numbers... just the lists of characters that make them up! (Without the whitespace of course.)

(define t-number
  (mlet nstr (wstok (many1 digit))
    (! (string->number (list->string nstr)))))

This is where the magic begins... mlet and ! are just macros which expand to scheme let and raw expressions wrapped in lambdas which require the current parser state. mlet then also takes a symbol to bind, and two parsers, which is why we need the ! macro magic. Remember that parsers are just lambdas that take a single parameter... it doesn't matter if they don't use that parameter, or just pass it on (like mlet).

It might also be interesting to note that we could write a combinator that took a function that accepted the results of the parser, and produced a replacement result. Technically this parser is actually very much like the one we just wrote, except that it can only filter the results of a parser, and not the results of several parsers which can only be achieved though nested mlet expressions. Usage might look similar to the following.

(define t-number
  (return nstr (wstok (many1 digit))
    (string->number (list->string nstr))))

Pretty close right.

By this point I thing most of you are starting to see what we're getting to... the canonical example of an expression parser, but this time I'm actually going to add a slight twist to the story instead. We're actually going to generate Scheme code instead, and then use the standard eval function instead. I know this might seem like a strange thing to do, especially since we don't normally use eval in most Scheme code, but think forward a little. Normally you wouldn't directly evaluate the code you just parsed either... think of eval as our interpreter, operating on an abstract syntax tree... conveniently Scheme code is already very much like an AST so we just use that.

(define t-add
  (return _ (wstok-char #+)
    '+))

This gives us our addition token returning, obviously, the Scheme + operator. And finally two more pieces to put it all together.

(define (left-assoc term op)
  (letrec ([f (λ (state acc)
                (aif lst fail? ((with-mark (all op term)) state)
                     acc
                     (f state
                        (list (car lst) acc (cadr lst)))))]) ;; (op lhs rhs)
    (mlet result term
      (λ (state)
        (f state result)))))

(define expr
  (left-assoc t-number t-add))

Finally, to end this... rather lengthy... example. Here is a finished REPL using our parser...

(define (expr-repl)
  (display ">> ")
  (let ([line (read-line)])
    (if (string-ci=? "exit" line)
        '()
        (begin
          (display " = ")
          (display (eval (expr (open-input-string line))))
          (newline)
          (expr-repl)))))

Calling it is pretty simple, and we can get results like so...

> (expr-repl)
>> 1
 = 1
>> 1+2+3+4
 = 10
>> 1 + 2 + 3 + 4
 = 10
>> exit
()

The purpose of this simple example is to show you how our combinators and point out what, in my opinion, is possibly the most important observation to make about parser combinators... all our combinators return parsers and all our parsers are not combinators. Yup, you always have to realize when you're looking at a combinator, and when your looking at a parser. Hell, you could even be looking at a combinator that returns a combinator! (currying et cetera)

Also, in the code I've used above, I made a point of following a coding convention that separates top-level parser definitions by using lambdas, and combinators or simple functions which use the define syntax instead. I found that this makes it easier to visually distinguish between the two, without having to resort to an explicit naming convention.

Next time we'll move beyond this simple stuff and I'll try to investigate more complicated combinators that don't take parsers but data structures to construct their resulting parser.

-- Lorenz

Recognize This!

Posted in Scheme on June 15th, 2008 by Lorenz Pretterhofer – Comments Off

Ok… I may not have entirely explained the machinery behind the basic backtracking parser combinators yet, but before I get to far into the heavy stuff I thought it might be useful to discuss some basic context issues involved in writing parsers.

There are two main elements involved in writing parsers… The Why… and the How (to some extent what and when also fit here… these are technical aspect that is).

Why do we write parsers?

There are actually a few reasons, and this isn’t really a particularly interesting question for us (after all, we’re already writing a parsers aren’t we?), but I would like to briefly recap what I believe are important points to keep in mind when designing a parser combinator framework.

There are two main varieties of parsers, which come in the form of prototypes and production parsers. Regardless of what the parser is being used for, the purpose of the parser is either to support another process, or it is simply to test some linguistic nature of a computer language (concrete syntax typically).

Of these two categories, we also stumble over a hybrid variation, where a parser written for production use may end up serving a prototype role and, of course, prototype parsers may end up evolving into production parsers.

The importance of this categorization is the implications of generally knee-jerk decisions associated with each approach. The main decisions we need to worry about ourselves when designing a parser framework are of course the error reporting and recovery mechanisms, basic performance concerns, and to some extent the language and tool support.

This isn’t just limited to our intended audience either, but also to ourselves as parser implementors… its all to common to consider why we are writing a parser and select a methodology based purely on practical concerns, and yet, quite often the very notation we use to write the parser is more important. The notation can have very severe effects on the cognitive processes involved in writing a parser or computer language, and so too can the tools that support that notation.

How do we implement parsers?

This part is a little simpler to describe at a high level… that and we’ll be delving into the issues more closely later on anyway.

I personally like to break the implementation concerns into three major categories… recognition, state, and output.

Each of these can become extraordinarily complex and intertwined, but they always tend to follow the same basic trade-offs which we’ll discuss briefly here.

Recognizing input is possibly the most obvious element of parser construction, since it tends to follow the design of the language anyway. More specifically it tends to follow the formal definition of a language, and most conventional notations use the same form as the BNF or PEG formal notation.

While it might seem useful to apply the same notation as the formal definition of a language, imo its not particularly friendly in practice. The main issue with this trade off is the difficulty in implementing non-trivial concerns like left-factoring, lookahead, error reporting/recovery or simply the fact that they don’t contain any proper form of abstraction (no state information or context sensitivity).

This the actually the reason we’re using a combinator approach… it allows us to capture the conceptual elements involved rather than directly writing a whole lot of repetitive rules, simply because the parser generator tool we selected, didn’t realize that scanners aren’t the only type of repetitive task in language writing (expression/operator grammars for example)… or even the lowly delimited list based on non-conventional parameter parsers.

This might actually be a good time to point out the ANTLR parser generator as an example of the kind of trade of we’re trying to avoid. I can’t stress enough how often I’ve wished I could refactor part of a parser involving delimited lists, enclosed parsers, associativity and precedence rules, and it just gets worse from there. The problem? No higher-level abstraction of rules (its like programming a user interface with 1st order logic… well… maybe not quite that bad but you get the idea).

Collecting state is the second important consideration when parsing a computer language. It might be interesting to consider what kind of state you’re accumulation however. In a calculator for example, the state is actually the current number and possibly a bindings table for variables if you allow them, but for a C parser you also need the symbol table and possibly even some other annotations for the preprocessor phase.

The point I’m trying to make here is not that parsers must return something… we’ll get to that in a second… the point is that many parser have other state that is needed by the other software components that receive the output form. This state obviously cannot be collected directly as a result, and yet it also has to be collected directly as a result of parsing.

Why do I care so much about these side effects? Its a perfect opportunity to reflect on Parsec’s usage of monads… and why we don’t need them!

Yup… that’s right, monads primarily capture a few things, but the main elements are timely evaluation (not necessarily eager), imperative ordering of evaluation, and propagation of out-of-band state information. To paraphrase… side effects and imperative code blocks. (Monads are actually capable of quite a bit more than this, but in our case, none of that behaviour is relevant.)

Actually we do still need to pass state, but whether we do it globally (uh…), or through an accumulator doesn’t matter… we have a different way of writing side effects, and we’ll use that for them. Scheme also provides the begin notation for us, so we have a perfectly viable means of writing imperative code if we need it, but even in Parsec it’s rarely a necessity.

Finally we come to the Output form, which is almost exclusively broken down into Abstract Syntax Trees (ASTs), Compilers (intermediate or machine code output), Simple Recognizers, Translators (Scheme to C for example), and Interpreters. Of these only ASTs are used when we have reasonable levels of computation power available, while the other forms are typically relegated to AST parsers or in our case, the native pattern matching and program code of the host language.

The big challenge we’re facing here however is actually related somewhat to the difference between prototype and production parsers. If we wish to provide good support for prototyping, we have to realize that its not entirely uncommon for interpreters to be written without an accompanying AST representation. Unless we can make the AST representation so inexpensive to generate and extraordinarily useful, we may wish to provide support for both ASTs and interpreters.

Whatever trade-off we make however, as long as we realize that we need to effectively support all of the possible applications unless we expect programmers to also use another parser framework or generator. Not necessarily perfectly, but at least adequately (we need to at least make generating ASTs cheap so that programmers can work with them even if the other forms are not simple to directly implement).

Before I finish I’d also like to add a small note about how parsers are actually used… or more specifically that the tools we provide for testing, debugging and maintaining our parsers are possibly more important than the ability to write the parsers in the first place.

For those who have used ANTLR before, you probably realize what I’m talking about here… yes the ANTLR grammars are limited, and painful to effectively refactor, when half the time you’re only even generating ASTs anyway. But the grammar debuggers and generally the ANTLR Works IDE are exceptional tools for constructing parsers. Also… I don’t mean to pay out the ANTLR parser generator… it really is the best tool I’ve used with Java or C++… its just not as powerful as I believe we can get in Scheme

While we can simplify the construction of most parsers greatly, it would be even better if we could also provide dedicated tooling for profiling, debugging, inspecting or even just testing our parsers. To this end, I’ll be adding various tooling issues interspersed with the construction of various parser facilities as we construct hopefully the canonical Scheme parser framework… or at least make the framework as useful as possible.

I don’t intend on covering any of this again in future posts in the series. Instead we’ll only be investigating the trade offs relevant for a Scheme parser combinator framework

– Lorenz

Scheme Parser Combinators

Posted in Scheme on June 10th, 2008 by Lorenz Pretterhofer – 2 Comments

I’ve had my MMeta parsing library on hold for a while now, but I think its finally time to reinvestigate the idea.

There are actually two reasons for doing this… The first is rather obvious… there is still a vacuum of good parser libraries for Scheme atm. But the other might be less so (uh… unless you read the title)… Scheme Parser Combinators.

After investigating parser combinators for a little while now, a short while ago I had the good fortune to stumble on a post by Gilad Bracha about parser combinators (this time its in Smalltalk).

While not everyone reading this will be able to easily follow the Smalltalk code involved in the post, its possible one of the best descriptions of what a combinator parser is, and how they tend to get written.

My goal here is to replace the forreign syntax of OMeta with a Scheme native syntax using plain old functions and namespace bindings. And of course there’s the odd macro to help with readability (combinators can occasionally get a little out of hand without proper currying, or an unusually light weight syntax for writing lambdas/closures).

What this means is that my parsing solution will now look a bit more like a set of constructors nested between each other being assigned to a variable. Its not quite as pretty as the Haskell variation which uses the parsers directly (thanks to lazy evaluation and currying), but it does work in a reasonably straight forward way.

(define basic-char
  (λ (state)
    (aif rc eof-object? (read-char state)
         fail
         rc)))

(define (char c)
  (λ (state)
    (let ([rc (read-char state)])
      (cond ((eof-object? rc) fail)
            ((char=? rc c) rc)
            (else fail)))))

In the above snipped we define the two fundamental character parsers… They essentially define the naive variation of a character parser and a selective character parser.

What I want you to do is note the second parser’s definition. This parser is not technically a parser at all, and is what we generally refer to as a combinator. In reality its not really a combinator since it doesn’t accept a function as an argument… but it is higher order since it does return one, and that’s they key to making these things work.

All you need to drive a parser combinator is to be capable of returning functions until you have a function with all but one argument… plain and simple currying. Of course because we’re using Scheme and not Haskell, currying for us is not implicit, so rather than doing all of it by hand we’d rather just have combinators which have not been parameterized yet, and actual parsers which only accept a single state argument (a Scheme input port currently).

This does make some of the parser we might implement a little more complicated, since some of our functions will return a parser, and some of our functions will be the parser… but overall it tends to work quite effectively once you understand the difference between the two. It might also help to note that currying is used to prepare functions early, but rather it tends to be on the fly. Occasionally you may even return a parser immediately before running the very same parser (within the same top level expression).

Since I’m looking predominantly at backtracking parsing still lets now investigate a useful practical example to introduce some more combinators and the basic style that you use when writing parser with this stuff…

(define a-or-b
  (many1 (mor (char #a)
              (char #b))))

Ok then… what do we have here. (char #a) and the equivalent for #b are simple parsers which only accept the plain and simple input of their respective character, or they fail.

This might be a good point to explain what the fail value actually is… its not a value for a start. Actually its a clever little MzScheme macro (not R5RS unfortunately), which allows you to either write just fail and it will expand to (make-fail-type ()), or you could write (fail "Did not find character #c!")… which is what we’ll be using when we cover error handling in a later post. Of course there’s also a function for testing it called fail?.

The mor here is of course the or branch (I may come up with a better name for this at some point), which in our case performs any stream rewinding necessary and checks each path until one of them succeeds or the entire construct fails. The parser returned by mor here will actually parse a single #a or a single #b or it will fail.

And finally the many1 accepts a parser which it will run over and over again collecting the results in to a list based accumulator. If it doesn’t see at least one result it will return a fail result, otherwise it will return the list of results. Interestingly this parser that many1 accepts must fail at least once before many1 itself will stop, which means that many1 must always rewind the stream at least a little before returning a successful result.

And of course we use the standard define to set a-or-b…

To finish up this post, our completed parser can be evaluated just like a hand-written parser in Scheme can…

(a-or-b (open-input-string "aabb"))

This will of course return (#a #a #b #b).

Of course this is really just a brief introduction on Parser Combinators and they’re basic operation… in the next post for MMeta I’ll be discussing some actual usage of the parser and of course, attached will be the actual source files for the parser ready to go.

– Lorenz