FAQ
Hi folks,

I'm (slowly) working my way through the Coursera compilers course, writing
a COOL compiler in go.

I watched the excellent Lexical Scanning in Go video, then modified
http://golang.org/src/pkg/text/template/parse/lex.go until I can
successfully lex go.

Now, I'd like to do the yacc step.

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

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. 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. 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?

Thanks, and sorry if that was confused/confusing: ask me questions :-)

Zellyn

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nick Craig-Wood at Nov 8, 2014 at 8:17 am

    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 <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Zellyn Hunter at Nov 8, 2014 at 5:36 pm
  • Zellyn Hunter at Nov 8, 2014 at 6:50 pm
  • Gerald Stanje at Nov 8, 2014 at 7:08 pm
  • Nick Craig-Wood at Nov 10, 2014 at 11:02 am
  • Zellyn at Nov 10, 2014 at 7:34 pm
  • Gerald Stanje at Nov 8, 2014 at 1:03 pm
  • Zellyn Hunter at Nov 8, 2014 at 5:41 pm
  • Nick Craig-Wood at Nov 10, 2014 at 11:12 am

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 7, '14 at 9:02p
activeNov 10, '14 at 7:34p
posts10
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase