[racket] parser-tools help

From: Marijn (hkBst at gentoo.org)
Date: Wed Nov 30 08:33:10 EST 2011

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm trying to use the parser-tools to parse the startup info for the
Google AI Challenge, but I am not sure what I'm doing wrong. As a
secondary question, given the repetitive nature of the code, how would
one use macros to make it better? Finally, do racket's here-strings
support indented format like bash's here-strings do?

The error produced by the code below is:

get-token: expected a nullary procedure, got 'turn

so lexer seems to expect that the action expression is a nullary
procedure, but if I make such a change I just get the parser to complain.

Marijn


#lang racket

(require parser-tools/lex
         (prefix-in re: parser-tools/lex-sre)
         parser-tools/yacc)

(define-tokens number-group (number))

(define-tokens turn0-data-group
  (turn
   loadtime
   turntime
   rows
   cols
   turns
   viewradius2
   attackradius2
   spawnradius2
   player_seed))

(define-tokens turn0-end-group (ready))

(define turn0-lexer
  (lexer
   ("turn" 'turn)
   ("loadtime" 'loadtime)
   ("turntime" 'turntime)
   ("rows" 'rows)
   ("cols" 'cols)
   ("turns" 'turns)
   ("viewradius2" 'viewradius2)
   ("attackradius2" 'attackradius2)
   ("spawnradius2" 'spawnradius2)
   ("player_seed" 'player_seed)
   ("ready" 'ready)
   ((re:+ (char-set "1234567890")) (token-number lexeme))
   ((re:+ whitespace) (values))
   ) )

(define turn0-parser
  (let ((ret (hash)))
    (parser
     (tokens number-group turn0-data-group turn0-end-group)
     (start data)
     (end ready)
     (grammar
      (data ((turn number) (hash-set ret 'turn $2))
	    ((loadtime number) (hash-set ret 'loadtime $2))
	    ((turntime number) (hash-set ret 'turntime $2))
	    ((rows number) (hash-set ret 'rows $2))
	    ((cols number) (hash-set ret 'cols $2))
	    ((turns number) (hash-set ret 'turns $2))
	    ((viewradius2 number) (hash-set ret 'viewradius2 $2))
	    ((attackradius2 number) (hash-set ret 'attackradius2 $2))
	    ((spawnradius2 number) (hash-set ret 'spawnradius2 $2))
	    ((player_seed number) (hash-set ret 'player_seed $2))
	    ))
     (error (lambda args (map displayln args))) )) )
	
(define (main port)
  (write (turn0-parser (turn0-lexer port))))

(define input
#<<END
turn 0
loadtime 3000
turntime 1000
rows 20
cols 20
turns 500
viewradius2 55
attackradius2 5
spawnradius2 1
player_seed 42
ready
END
)

(main (open-input-string input))
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7WMJYACgkQp/VmCx0OL2xavwCdEbHXOcVlratft6hvJADg0SFs
VWEAnAoJAE+Yp0daAY6ghAeayRdSugIC
=y84Z
-----END PGP SIGNATURE-----


Posted on the users mailing list.