FAQ
On Fri, Oct 5, 2012 at 8:40 PM, Greg Ward wrote:

Parser generator: I highly recommend to check http://golang.org/cmd/yacc/
Lexer generator: You might want to check http://github.com/cznic/golex

-j

--

Search Discussions

  • Greg Ward at Oct 5, 2012 at 7:04 pm
    Hi all --

    I'm wondering if anyone else has experience with parsers and lexers in
    Go. I'm writing the parser for a small DSL [1]. I have enough
    background in this stuff to be moderately clueful, but I'm far from an
    expert. (E.g. this is the first time I've ever actually used a
    shift-reduce parser generator, despite having carefully learned the
    theory behind them in grad school 15 yearss ago!)

    After beating my skull against the keyboard for several days now, I've
    got something working with golemon. Lemon is a LALR parser generator
    written in C for C, by the same guy who wrote SQLite. It appears to be
    yacc done right. golemon is a rather literal port to Go:
    https://github.com/nsf/golemon . It's underdocumented and the type
    system is overly complex (IMHO). And it doesn't do anything for
    tokenizing; you have to supply your own lexical scanner. But *I got it
    working* without having to modify golemon or roll my own parser
    generator.

    I just looked briefly at go11ie, an LL(1) parser generator:
    https://github.com/realistschuckle/goll1e. The API appears, ummm,
    non-idiomatic:

    "[...] call the yyparse(int, func(*yystype)int)bool function. It’ll
    return true if the parse succeeded and the result of your
    computations will exist in yyres[0]. Otherwise, you’ll get a false
    and junk will populate yyres."

    That one sentence makes me not want to use goll1e. ;-(

    Before I do anything foolish [2], can anyone recommend a good
    lexer/parser pair that generates clean, idiomatic Go code with a
    minimum of fuss and pain?

    Thanks --

    Greg

    [1] I'm skeptical of Ruby's approach to DSLs, which is basically "Ruby
    has a sloppy syntax, so just use Ruby with appropriate libraries as
    your DSL!". So I'm doing it the old-fashioned way, with a lexer,
    parser, and AST of my own devising.

    [2] In order of increasing foolishness:
    - fork golemon and try to clean up the hairy type system
    - add a Go backend to ANTLR and put up with the dependency issues
    ("you mean I need to install a JDK in order to build your Go
    program???")
    - port ANTLR to a widely-used, widely-available language that does not
    suck (i.e. Python, or maaaaaybe C), *then* add a Go backend and use it
    - learn all about PEGs, the new kid on the block, and write a brand-new
    parser/lexer pair with multiple per-language backends, implemented
    in one of those widely-used/widely-available/does-not-suck languags

    (Why is that "increasing foolishness" also looks like "increasing
    fun"? ;-)

    --
  • Rémy Oudompheng at Oct 5, 2012 at 6:47 pm

    On 2012/10/5 Greg Ward wrote:
    Hi all --

    I'm wondering if anyone else has experience with parsers and lexers in
    Go. I'm writing the parser for a small DSL [1]. I have enough
    background in this stuff to be moderately clueful, but I'm far from an
    expert. (E.g. this is the first time I've ever actually used a
    shift-reduce parser generator, despite having carefully learned the
    theory behind them in grad school 15 yearss ago!)
    Did you try goyacc ? It's bundled with Go and has an example:

    Presentation: http://golang.org/cmd/yacc/
    Example: http://golang.org/src/cmd/yacc/units.y

    Rémy.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 5, '12 at 6:47p
activeOct 5, '12 at 7:04p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase