[plt-scheme] parser-tools basics

From: Dave Herman (dherman at ccs.neu.edu)
Date: Thu Oct 20 21:05:02 EDT 2005

I've started writing a tiny little parser and it's resulting in an 
inexplicable error. Can someone smack me upside the head and tell me 
what I'm doing wrong?

I've included the source below... the lexer seems to correctly lex the 
token "foo", the parser correctly recognizes it as a TOKEN token, and 
then it raises an error. I must be missing something obvious.

Thanks so much,
Dave

;; --

(module parse mzscheme
   (require (lib "lex.ss" "parser-tools")
            (prefix : (lib "lex-sre.ss" "parser-tools"))
            (lib "yacc.ss" "parser-tools"))

   (define-empty-tokens Operators
     (EQUAL SEMI PERIOD COMMA QUOTE BACKSLASH EOF NEWLINE))

   (define-tokens ValueTokens (DIGIT TOKEN STRING))

   (define-lex-abbrevs
     [CR #\return]
     [LF #\newline]
     [control (:or (:/ #\u0001 #\u001F) #\rubout)]
     [token-special (:or "(" ")" "<" ">" "@"
                         "," ";" ":" "\\" "\""
                         "/" "[" "]" "?" "="
                         "{" "}" " " "\t")]
     [ascii (:/ #\u0001 #\u007F)]
     [token (:+ (:& ascii (:~ token-special control)))])

   (print-struct #t)

   (define cookie-lexer
     (lexer-src-pos
      [(eof) 'EOF]
      [(:or CR LF) (token-NEWLINE)]
      [token (token-TOKEN lexeme)]
      ))

   (define cookie-parser
     (parser
      (start Token)
      (end NEWLINE EOF)
      (tokens Operators ValueTokens)
      (error (lambda (token-ok? token-name token-value start-pos end-pos)
               (error (string->immutable-string
                       (format "error: (token-~a ~v) [~a, ~a)"
                               token-name
                               token-value
                               (position-offset start-pos)
                               (position-offset end-pos))))))
      (src-pos)
      (grammar
        (Token
         [(TOKEN) $1]))))

   (cookie-parser (lambda () (cookie-lexer (open-input-string "foo"))))

   (provide cookie-lexer cookie-parser))


Posted on the users mailing list.