PEG compiler

303 views
Skip to first unread message

Manlio Perillo

unread,
Nov 28, 2015, 10:38:09 AM11/28/15
to golang-nuts
Hi.

I need a PEG runtime compiler (with an API similar to regexp).
Currently there are a few implementations of PEG in Go, but all generates Go code.
One example of what I want is

Is someone working or implemented something like this?


Thanks  Manlio

Manlio Perillo

unread,
Nov 29, 2015, 1:48:04 PM11/29/15
to golang-nuts
I found this paper:

and this implementation:

Unfortunately the grammar must be specified using a DSL, and Go is not a good language for DSL.


Thanks  Manlio

Caleb Spare

unread,
Nov 29, 2015, 2:28:44 PM11/29/15
to Manlio Perillo, golang-nuts
IMO Go is a fine language for a DSL. For instance, the Go standard
library packages fmt, regexp, and text/template all implement DSLs
that make certain tasks very convenient.

In recent years, it has become fashionable to build libraries for
domain-specific tasks which use syntax of the host language to look
"DSL-ish". This works better in some languages than others. For
example, Ruby is well-suited for those kinds of libraries because of
its flexible syntax, dynamic typing, and block construct. This
approach doesn't work very well in languages like Go or Java.

Even though it is now common to use "DSL" to mean those kinds of
libraries, keep in mind that they are only a subset of DSLs. (Some
would argue those libraries are not DSLs at all.)

While Go's syntax doesn't make it easy to write libraries that look
like a different language, it is a nice language for writing small
lexers and parsers.

-Caleb
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Manlio Perillo

unread,
Nov 30, 2015, 9:24:21 AM11/30/15
to golang-nuts, manlio....@gmail.com
Il giorno domenica 29 novembre 2015 20:28:44 UTC+1, Caleb Spare ha scritto:
IMO Go is a fine language for a DSL. For instance, the Go standard
library packages fmt, regexp, and text/template all implement DSLs
that make certain tasks very convenient.  
In recent years, it has become fashionable to build libraries for
domain-specific tasks which use syntax of the host language to look
"DSL-ish". This works better in some languages than others.

Note that I was referring precisely to this kind of DSL.
The  https://github.com/losinggeneration/pego/ implementation does not offer an API that accept the grammar as a string, compile it to an AST and execute it.
Instead you need to build the AST using Go syntax, and this is very verbose.

> [...]


Regards  Manlio 

Sean Wolcott

unread,
Nov 30, 2015, 10:10:10 AM11/30/15
to golang-nuts
Are you looking for something that reads PEG text representation of a grammar at runtime and generates the parser? Or are you looking for a package that supplies those elements as functions or a builder pattern which produces a parser as output? My possibly flawed intuition tells me that you would be more likely to find the latter, but I tend to see them implemented more often in interpreted languages. Parser combinators are an easy-to-construct method for producing a top-down, PEG-like parser at runtime if you give up looking and just want to write it yourself. I wrote one for Javascript not too long ago, and it was a joy.

Manlio Perillo

unread,
Nov 30, 2015, 11:17:07 AM11/30/15
to golang-nuts
Il giorno lunedì 30 novembre 2015 16:10:10 UTC+1, Sean Wolcott ha scritto:
Are you looking for something that reads PEG text representation of a grammar at runtime and generates the parser? Or are you looking for a package that supplies those elements as functions or a builder pattern which produces a parser as output? My possibly flawed intuition tells me that you would be more likely to find the latter, but I tend to see them implemented more often in interpreted languages. Parser combinators are an easy-to-construct method for producing a top-down, PEG-like parser at runtime if you give up looking and just want to write it yourself. I wrote one for Javascript not too long ago, and it was a joy.

I would like to have a port of LPeg to Go.
As I wrote, https://github.com/losinggeneration/pego/, *is* a port of LPeg to Go, but it's missing the final part, where a string with the grammar definition is compiled to the AST.
Since this is the easy part, I don't if its is a design choice or the cause is the author's lost of interest.

I agree with you that, after having read several papers, writing a PEG implementation is very easy.
However the problem with parser combinators is that they are convenient to use only with functional languages, not with Go.


Thanks  Manlio
Reply all
Reply to author
Forward
0 new messages