[antlr-interest] soliciting language recipes book outline feedback

Terence Parr parrt at cs.usfca.edu
Mon Jan 7 10:36:37 PST 2008


On Dec 31, 2007, at 9:35 PM, Monty Zukowski wrote:

> How about C's typedef?  That's always fun.

Yes, I think some kind of nasty context-sensitive parsing is in order.  
added.

> I'm also really happy with my little expression language which
> generates Java code.  All operators and functions are stored in the
> tree as functions, and the functions themselves are defined in a
> separate file as snippets of ST templates.  I think that would be a
> good little recipe, we can chat further about it if you like.  Sample:
>
> expression:  a+b
> grammar: addition!: x:atom PLUS y:atom {##=#[#(FUNCTION, "add:2"),  
> x, y];}
> tree: (FUNCTION "add:2" a b)
> function definition: add($x, $y) ::= <<$x + $y>>
> alternate function definition for BigIntegers: add($x, $y) ::= << 
> $x.add($y)>>

So the function "name" dictates which template to load?

> Also fun is handling multiple grammars.  For instance our expression
> language is mainly for generating strings, so we have a templating
> kind of grammar:
>
> {$interpreted_variable} some static text followed by
> {$another_variable + $yet_another}
>
> One grammar breaks it into chunks of either static text or code.  If
> it is code it calls the expression grammar on the contents.  Must be
> similar to the string template grammar.

Or any other island grammar.  I have that as one of the recipes.  
probably need to kinds: lexically identified and grammatically  
identified

Thanks!

Ter
> On Dec 31, 2007 1:52 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>> Howdy folks,
>>
>> I've signed up to do another book for pragmatic programmers.  This
>> time I'm going to do a collection of "how to" examples/recipes.  The
>> book is meant to teach you how to fish not give you fish--the  
>> examples
>> will be necessarily smallish. I anticipate the level being
>> introductory through intermediate as real, big examples will simply
>> not fit. I'd like to start out with really brain-dead examples such  
>> as
>> comma separated value parsing so that I can explain the fundamentals.
>> I will always remember the first technical book I read (on 6502
>> assembly code code by Lance Levanthal).  This awesome book started  
>> out
>> showing how to clear a memory location. This gave me the confidence
>> that I might actually understand the rest of the book :)
>>
>> I propose breaking the book up into three main sections: parsing,
>> interpretation, translation. I would love to hear peoples feedback
>> about what examples would be useful and exciting.  Keep in mind that
>> the average example will need to fit in about 5 to 10 pages. Code
>> samples will be made available online and not necessarily completely
>> printed out in the book to save space. Also I would love to hear your
>> feedback about how the recipes should be laid out or described.
>>
>> Please CC Susannah Pfalzer, with whom I'm fortunate to work with
>> again, on your replies as she is not a member of this list. :)
>>
>> Here is my rough draft list of examples, which is a bit too few.
>> Thanks and happy new year!
>>
>> Ter
>> ------------
>> I. Parsing recipes
>>        comma-separated values (show in java then python or ruby  
>> then ANTLR)
>>        parsing binary data (fixed and non-fixed size records)
>>        parsing log files (in awk/sed/grep then ANTLR)
>>        configuration file or JSON or YAML
>>        text-based network protocol (e.g., POP, nntp, http)
>>        graphs and networks via edge list format
>>        read fixed a XML format like:
>>                SVG (vector graphics)
>>                or, RDF ontology graph
>>        handling include files
>>        parsing island grammars like JSP
>>        simplified English imperative statement recognizer
>>                (E.g., games: "pick up gun and shoot spy")
>>
>> II. Interpretation recipes
>>        arithmetic expression trees
>>        polynomial differentiation
>>        query language (parse and pull data from DB)
>>        serial/deserialize objects
>>        partial PS interpreter
>>        simple programming language interpreter
>>
>> III. Translation recipes
>>        wiki to HTML
>>        simplified programming language pretty printer
>>        instrumenting simplified programming language with profiling  
>> info
>>        simple Java byte code generator
>>        simple robot control language for lego MindStorm robots
>>        class hierarchy generator (awk/sed/grep then grep+ANTLR)
>>                generates DOT format for graphviz to visualize
>>



More information about the antlr-interest mailing list