4. A portable and efficient LALR(1) parser generator for Scheme -- Defining a parser

4.1 Loading the generator

The file lalr.scm declares a macro called lalr-parser:

(lalr-parser [options] tokens rules ...)

To use this macro, you must first load lalr.scm in your Scheme system using either load or the include special form in Gambit-C.

This macro, when given appropriate arguments, generates an LALR(1) syntax analyzer. The macro accepts at least two arguments. The first is a list of symbols which represent the terminal symbols of the grammar. The remaining arguments are the grammar production rules. See section The grammar format for further details.


4.2 Running the parser

The parser generated by the lalr-parser macro is a function that takes two parameters. The first parameter is a lexical analyzer while the second is an error procedure.

The lexical analyzer is zero-argument function (a thunk) invoked each time the parser needs to look-ahead in the token stream. A token is usually a pair whose car is the symbol corresponding to the token (the same symbol as used in the grammar definition). The cdr of the pair is the semantic value associated with the token. For example, a string token would have the car set to 'string while the cdr is set to the string value "hello".

Once the end of file is encountered, the lexical analyzer must always return the symbol '*eoi* each time it is invoked.

The error procedure must be a function that accepts at least two parameters.



This Html page has been produced by Skribe.
Last update Sun Dec 3 20:16:43 2006.