[racket] Using lex and yacc
On Tue, Oct 12, 2010 at 11:12 AM, Mark Carter <mcturra2000 at yahoo.co.uk> wrote:
> Are there any examples on using lex and yacc, as I'm having trouble getting
> started. I am trying to write a simple calculator. Here's what I've got so far:
>
> #lang racket/base
>
> (require parser-tools/lex)
>
> (define lex (lexer
> ((:+ "[0-9]") (values 'int (string->number lexeme)))
> ((:+ whitespace) null)
> ((:: "+") (values 'plus 0))
> ((:: "-") (values 'minus 0))
> ((:: "*") (values 'mult 0))
> ((:: "/") (values 'div 0))))
>
> When I try to compile it, I get
> regular-expression: undefined operator in: (:+ "[0-9]")
> How do I correct this?
I think you missed the part of the documentation that says:
The regular expression language is not designed to be used directly,
but rather as a basis for a user-friendly notation written with
regular expression macros. For example, parser-tools/lex-sre
supplies operators from Olin Shivers’s SREs, and
parser-tools/lex-plt-v200 supplies (deprecated) operators from the
previous version of this library. Since those libraries provide
operators whose names match other Scheme bindings, such as * and +,
they normally must be imported using a prefix:
(require (prefix-in : parser-tools/lex-sre))
The suggested prefix is :, so that :* and :+ are imported. Of
course, a prefix other than : (such as re-) will work too.
Cheers,
Sam