br-parser-tools question

42 views
Skip to first unread message

jon stenerson

unread,
Jun 8, 2020, 8:36:15 PM6/8/20
to racket-users@googlegroups.com List
Hi all,

I don't understand the error message here. The parser seems to be
looking for a position-token but the lexer is sending srcloc-tokens? Is
there a simple fix? Using Racket 7.7 on WIn 10.

I am also confused about whether to use br-parser-tools or parser-tools
package. br-* is the more recent?

Thanks,

Jon


#lang racket

(require br-parser-tools/lex)
(require br-parser-tools/yacc)

(define-empty-tokens etoks (ZERO ONE EOF))

(define simple-lex
  (lexer-srcloc
   ["0" (token-ZERO)]
   ["1" (token-ONE)]
   [(eof) (token-EOF)]
   ))


(define (err-proc tok-ok? tok-name tok-value start-pos end-pos)
  (display "Ugghh!"))

(define simple-parse
  (parser
   (tokens etoks)
   (start S)
   (end EOF)
   (src-pos)
   (error err-proc)
   (grammar
    (S [(ONE ZERO) 'OK])
    )))


(define (make-scanner ip [path #f])
  (λ () (simple-lex ip)))

(simple-parse (make-scanner (open-input-string "01")))

Jon Zeppieri

unread,
Jun 8, 2020, 8:49:50 PM6/8/20
to jon stenerson, racket-users@googlegroups.com List
On Mon, Jun 8, 2020 at 8:36 PM jon stenerson <jonste...@comcast.net> wrote:
>
> Hi all,
>
> I don't understand the error message here. The parser seems to be
> looking for a position-token but the lexer is sending srcloc-tokens? Is
> there a simple fix? Using Racket 7.7 on WIn 10.

Hi Jon,

Yes, you're right, and there is a simple fix. Instead of using
`lexer-srcloc`, you can use `lexer-src-pos`. The former seems to be an
addition to the `br-` variant of parser-tools, but I don't see any
indication that the lexers it generates can be used with the yacc
tools. (I could be wrong about that.)

- Jon

Jon Stenerson

unread,
Jun 9, 2020, 12:29:13 AM6/9/20
to Jon Zeppieri, racket-users@googlegroups.com List
Thanks for confirming. I had actually started with the lexer-src-pos version but had other problems like exn:fail:read wanting a list of srclocs and the lexer only having positions. So I switched to lexer-srcpos which fixed the exception but broke the parse. Maybe I should do a conversion between positions and srclocs at some point? Seems awkward.

Matthew Butterick

unread,
Jun 9, 2020, 1:00:14 AM6/9/20
to Jon Stenerson, Jon Zeppieri, racket-users@googlegroups.com List
`br-parser-tools` has some patches needed to make `brag` work, e.g., using the srcloc structure. I forked rather than patch the underlying `parser-tools` package because I didn't want to destroy the fragile ancient artifact. I fixed a few bugs; the others persist. I notice, for example, that your program fails to work regardless of whether it uses `br-parser-tools` or `parser-tools`. (I'm assuming here you have a good reason to use good old `yacc` and not `brag`, which does mostly the same thing, less painfully)

jon stenerson

unread,
Jun 9, 2020, 9:53:42 AM6/9/20
to racket-users@googlegroups.com List

I don't think you should assume I have a *good* reason for using yacc over brag. I just thought that in my case it would be easier to put some code in the productions rather than build a full parse tree and then process that. I seem to have been wrong about that. I will take another look at brag.

Reply all
Reply to author
Forward
0 new messages