On 07/11/14 21:02, Zellyn wrote:
> I was wondering if anyone knew of a go yacc example with the following
> characteristics:
>
> * uses a hand-build lexer in the style from the talk and/or text/template
> * has the lexer in its own file (not in the .y file)
> * is fairly simple
Funnily enough I wrote one last week which satisfies the above three
points (maybe a bit weak on the last!). I could send it to you off list
if you want to see (not ready for release yet).
I've used bison before (a long time ago) but this was my first time
through with "go tool yacc".
I decided to hand build the lexer as it was the go way, and it turned
out to be not that difficult, and a lot easier that fighting with flex
would have been.
> Failing that, a quick sanity check on code organization would help:
>
> I'm thinking that the yacc-output file has to be the one to define
> integer values for tokens.
Correct.
> So I'm thinking I'll put the lexer interface
> definition in one file, so the yacc file can reference it, then generate
> the yacc output, and then have the hand-built lexer reference the term
> values defined there.
That is exactly what I did. I ended up with lexer.go and grammar.y
files. The grammar.y file built a y.go file with the token definitions in.
> It means I can't build the lexer without having
> yacc output, but for just testing lexing, I can create a mostly empty
> grammar that will at least create item numbering. Does that sound sane?
That sounds perfect. I build the grammar first then the lexer when I
did it, but just defining the %token will define the symbols I think.
Documentation on yacc or bison is helpful for working out what go tool
yacc can do. If you look at the source code you can see exactly which
%operations are defined.
The hardest part for me was killing all the shift/reduce and
reduce/reduce errors. That is standard yacc stuff though, not go
specific. At this point it is helpful to have used the -v flag to
generate the parsing tables as you can't really understand the debug
output of y.go withouth them.
--
Nick Craig-Wood <
ni...@craig-wood.com> --
http://www.craig-wood.com/nick