[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Is Rockit abandoned?

Berger, Daniel

9/23/2003 9:47:00 PM



> -----Original Message-----
> From: Robert Feldt [mailto:feldt@ce.chalmers.se]
> Sent: Tuesday, September 23, 2003 3:38 PM
> To: ruby-talk@ruby-lang.org
> Subject: Re: Is Rockit abandoned?
>
>
> Berger, Daniel <djberge@qwest.com> skrev den Wed, 24 Sep 2003
> 00:39:46 +0900:
>
> > And please, please, please add a tutorial for parser generator
> > dummies, like me. :)
> >
> Its like regexps but more powerful. How hard can it be? :)
>
> Seriously, regexps really are parser generators that are
> integrated into the language. The languages you can parse
> with them are somewhat restricted but more importantly they
> are kind of hard to use when you need to parse larger, more
> complex languages with lots of different cases. Parser
> generators tries to address that situation. So its
> regexps-on-steriods or something like that.
>
> We'll see about a tutorial but I can promise lots of
> examples. Learning-by-example is supposed to be good so... ;)
>
> Regards,
>
> Robert

Thanks. I also kinda suck at grammars, though I've gotten better at it.
I meant to mention that before.

I guess what I'm looking for is something like:

* here is a source file we must parse where regular expressions would be
too big a pain (or impossible)
* here is a grammar
* here is a parser
* here's how it all works together

Regards,

Dan

1 Answer

Robert Feldt

9/23/2003 10:34:00 PM

0

> I guess what I''m looking for is something like:
>
> * here is a source file we must parse where regular expressions would be
> too big a pain (or impossible)
> * here is a grammar
> * here is a parser
> * here''s how it all works together
>
Would examples like this be helpful:

# Rockit Tutorial example number X
#
# NOTE that the API and behavior below is not fixed but the current
# design/view of how Rockit 0.6 will work.
#

# We want to parse brace-delimited balanced string literals like:

input_example = "%{1{2}3}"

# which should return the parsed string "1{2}3".
# This is hard to do with regexps (although I''m sure there is some
# special notation added that makes it possible!).

# In Rockit we can write a grammar for this like:

require ''rockit''

g = <<EOG
StringLiteral -> ''%{'' CurlyInner ''}''. StringLiteral[inner]
CurlyInner = Balanced*
Balanced = ''{'' Balanced* ''}''
| /[^{}]/
EOG

grammar = Rockit::Grammar.new g

# generate a parser class from the grammar
parser_class = grammar.generate_parser_class

# instantiate a parser
parser = parser_class.new

# then parse
p parser.parse(input_example) # => StringLiteral[''1{2}3'']

# Note that Rockit by default builds an Abstract Syntax Tree (AST)
# for you by creating and calling an AstBuilder. If you want to # change how the AST is built or even not build an AST you can define
# your own builder:

class MyBuilder < parser_class.default_builder_class
# Name of method is lower_case version of the AST name specified
# in the grammar above.
def string_literal(inner)
inner
end
end

# now specify that your builder should be used by a parser
parser2 = parser_class.new MyBuilder.new

p parser2.parse(input_example) # => ''1{2}3''

?

/Robert