[squeak-dev] Xtreams up to date

Hannes Hirzel hannes.hirzel at gmail.com
Sat Jan 22 10:00:31 UTC 2011


Thank you Martin,

your explanations got me some steps further.

I found the page http://pdos.csail.mit.edu/~baford/packrat/ by Bryan
Ford which seems to be a good entry point for reading about PEG
parsing. That page refers to
http://en.wikipedia.org/wiki/Parsing_expression_grammar as a good
technical overview.

I took the wiki example from
http://code.google.com/p/xtreams/wiki/Parsing and adapted the names so
that it fits the Squeak implementation


A Parser is created from a PEG grammar by parsing the grammar rules
using the ParserParser.

        wikiGrammar := PEGParser grammarWiki reading.
        wikiParser := PEGParser parserPEG parse: 'Grammar' stream:
wikiGrammar actor: PEGParserParser new.


        input := 'Single paragraph with *bold* and _italic_ text and a
[link]' reading.

"As you indicated I used the PEGTreeBuilder"
	  wikiParser parse: 'Page' stream: input actor: PEGTreeBuilder new

If I explore the resulting object I got what is shown in the screen
shot. I am not sure how to interpret the result.



"Regarding the wiki example: the class PEGWikiGenerator is not included"

        wikiParser parse: 'Page' stream: input actor: PEGWikiGenerator new.

Where can I get this class?


The comment of the method  #parse:stream:actor: of class PEGParser is helpful

"Parse @definition from aStream using @anActor to act on the matching rules.
	""	definition	<String>			identifies the rule of the grammar to apply
to the input
		aStream		<ReadStream>	the input to parse
		anActor		<Actor>			receives callbacks from the parser for the
successfully rules with the matching the input
		^			<Object>		result of the actor action for the @definition
	"


As class methods of PEGParser there are 8 grammars included - CSS3,
HttpUrl, JSON, JavaScript, PEG, Smalltalk, Wiki, XML.

I think a simple complete example which actually does something would
be helpful for me.
TreeBuilder  seems to do nothing and I do not see the tree.

For example besides the classes PEGParserPEGTest and
PEGParserSmalltalkTest (6 test cases) a class PEGParserHttpUrlTest or
PEGParserJavaScriptTest would be helpful.


Actually I would like to parse some simple Javascript like

                                mySent = new aswExample('')
                                mySent.addWord('Ninatoka')
                                mySent.currentWord().firstLetterUppercase=true
                                mySent.currentWord().addMorpheme('ni-','S1s-')
                                mySent.currentWord().addMorpheme('na-','Pres.-')

mySent.currentWord().addMorpheme('tok','come_from')
                                mySent.currentWord().addMorpheme('-a','-VF')
                                mySent.addWord('Uswisi.')

mySent.currentWord().addMorpheme('Uswisi','Switzerland.')
                                mySent.writeHTML()

It is used for web pages on which Swahili text is displayed. On
request a morphological analysis is shown.

A note: this example could be parsed with some manually constructed
code without PEG I assume, just string matching and some branching. So
there is no pressure on this. But I'd like to use it to learn PEG
parsing. The goal is to translate the JavaScript code to Smalltalk
code (and later one to an XML representation for printing).


--Hannes



On 1/21/11, mkobetic at gmail.com <mkobetic at gmail.com> wrote:
> "Hannes Hirzel"<hannes.hirzel at gmail.com> wrote:
>> Besides http://code.google.com/p/xtreams/wiki/Parsing are there more
>> examples how to use Peg parsing?
>
> Unfortunately that one is still waiting for documentation. Even the little
> bit that is on the wiki page uses an example that wasn't ported because it
> uses VW's XML Elements.
>
> It's actually not that complicated to use. Basically you take a PEG grammar,
> you parse it as the wiki example shows (the first 2 lines). Then you need an
> 'actor' that will get callbacks from the parser when it parses some input.
> The Actor class provides simple pragma based dispatch mechanism for
> convenience, but ultimately the actor simply receives
> #process:object:start:stop: callbacks from successfully applied grammar
> rules. The TreeBuilder in the Tests package shows the simplest Actor that
> just collects all the callback arguments. The return value of the callback
> is important, the object: argument of the callback is a collection of what
> the lower level rules returned from their callbacks. Anyway that's real
> quick description of how the parsing mechanism works from user point of
> view. Real documentation needs a lot of simple examples to make that
> clearer, but hopefully this could get you going if you experiment with
> simpler grammars and the TreeBuilder a bit. I'll try to answer any more
> specific questions as I can.
>
> HTH,
>
> Martin
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ExploreResultOfPEGwikiParser.PNG
Type: image/png
Size: 41434 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20110122/fcc9f12b/ExploreResultOfPEGwikiParser.png


More information about the Squeak-dev mailing list