[plt-scheme] Getting good shift/reduce error messages using parser-tools?

From: Casey Klein (clklein at calpoly.edu)
Date: Tue Dec 5 07:12:16 EST 2006

On Mon, Dec 04, 2006 at 04:34:55PM -0800, Danny Yoo wrote:
> Hi everyone,
> I'm playing with the parser-tools for project that I'm doing just for fun, 
> and I ran across a shift/reduce conflict.  Fortunately, I know exactly 
> where my grammar problem is.  Unfortunately, I don't know the parser-tool 
> collection well enough yet to have it give useful hints on where to find 
> the error.
> Here's my silly parser so far:
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module test mzscheme
>   (require (lib "lex.ss" "parser-tools")
>            (lib "yacc.ss" "parser-tools"))
>   (define-tokens tokens (lparen atom rparen comma eof
>                                 plus minus times div))
>   (define parse-expression
>     (parser
>      (tokens tokens)
>      (start expr)
>      (end eof)
>      (grammar
>       [expr ((expr plus expr) 'ok)])
>      (error (lambda (token-ok token-name token-value start-pos end-pos)
>               (raise-syntax-error #f "while consuming" token-value))))))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> When I compiled this, I got an error message '1 shift/reduce conflicts'. I 
> expected this: I understand the issue about forcing left precendence, so I 
> can fix the problem pretty quickly.
> My concern, though, is that the error message I got from the parser-tools 
> is minimal: it doesn't given any location information to pinpoint which 
> rule I should paying attention to.  If my grammar were larger and more 
> complex, I'd be a bit unhappy about the sparse error message.
> Is there something in the tool that I have to turn on to get extended 
> diagnostic information?  I'm thinking I could just dump it to yacc format 
> and ask yacc to give me better information, but that seems slightly silly.
> Thanks!

The `parser' syntax has a debug option which prints to a specified
file the generated LALR table and conflicting transitions:

     (debug "some-file")
     (tokens tokens)
     (start expr)

This output can be kind of hard to read, if you're not familiar with
it, but it might at least get you in the ballpark.


Posted on the users mailing list.