Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tutorial: REBOL parse
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
btiffin




PostPosted: Fri Apr 04, 2008 10:35 pm   Post subject: Tutorial: REBOL parse

An introduction to REBOL PARSE

By Brian Tiffin
April 2008

PARSE

REBOL, the Relative Expression Based Object Language has many features. One of the key elements of this scripting language is PARSE. PARSE opens the door to Domain Specific Languages or Dialects using REBOL terminology.

Unlike Regular Expressions, PARSE works more like the pattern matching made famous by SNOBOL and Icon and can resemble Backus-Naur Form.

Code examples use
>> to signify the console prompt
with [ used during line continuations and
== to signify the result display

or as complete scripts, with a REBOL header suitable for
>> do %script.r

Major Modes

PARSE has two major modes
    string parsing
    block parsing

String parsing

At its simplest, PARSE is used to pull strings apart, using default delimiters

code:
>> parse "Hello World!" none
== ["Hello" "World!"]

REBOL ate the space and returned a block! with two elements
    the string! "Hello"
    and the string! "World!"

This is using the default parse rules; specified by none. none is a REBOL word that has a type of none! and a value of none.

Other delimiters can be specified, such as comma
code:
>> parse "one,two,three" ","
== ["one" "two" "three"]

or dash
code:
>> parse "613-555-1212" "-"
== ["613" "555" "1212"]

or null
code:
>> parse/all c-strings "^(null)"


Here the /all refinement was used so that spaces, tabs and newlines are included.

Grammar Rules

String splitting is all well and good, but parse rules can be far more sophisticated.

The rules for PARSE use a dialect of REBOL and as part of the Relative Expression acronym, symbols can have different meanings in different contexts.

Alternates
The vertical bar allows alternates in a pattern match
code:
rebol []
parse "a phone" [
    ["a" | "the"]
    ["phone" | "radio"]
]
== true

The above would be true for "a radio", "the radio", "a phone" or "the phone". The rules can also be in variables, looking a little more BNF.

code:
rebol [Title: "Nerds and Jocks"]
article: ["a" | "the"]
person: ["nerd" | "jock"]
parse "a nerd" [article person]
== true

REBOL datatype parsing

This is where things can get fun. The script
code:
rebol [Title: "REBOL values"]
parse/all read http://compsci.ca [
    thru <title> copy title to </title>
]
print title
Computer Science Canada
==false

will display the HTML title line from the compsi.ca website.

REBOL knows about the tag! datatype. Lexically, anything within angle brackets is treated as a tag!

So; the thru keyword asks the parser to scan through the title tag, leaving the parse scanner pointing just past the title open tag.

copy title informs the parser to keep a copy of the next match that follow in the REBOL variable title.

That pattern match happens to be to </title>.

Unlike thru, to asks the parser to match up to the tag, leaving the scanner just before the closing </title> tag. And the REBOL variable title, will end up having the text that is between the <title> and </title> tags of the page that is returned by reading http://compsci.ca, be that index.html, index.php or whatever the webserver delivers.

Note, this example will return false from the parse. There is more text beyond the </title> so the parse expression actually fails, but we still get our title.

A proper parse expression returning true would be
code:
rebol [
    Title: "truer title parse"
    Author: "Brian Tiffin"
    Date: 04-Apr-2008
]
parse read http://compsci.ca [
    thru <title> copy stuff to </title> to end
]
print stuff
The to end advancing the parse scanner to the tail of the input; something required for a successful and true parse expression.

More to it than what is shown here
This is just a sample of what can be done with PARSE. There are more keywords, more patterns, more features. But being REBOL; not hundreds, just a dozen or so. With that toolkit of features, a lot of power exists.

Last example

To end this little tutorial; evaluated parsing. After each successful match, the parse dialect allows expression evaluation. This is where the power of PARSE becomes more apparent, (though perhaps not in this trivial example).

This example includes parse looping, REBOL value parsing and expression evaluation. Note that the input is not a string!, but a block! of loadable REBOL values. The some keyword causes a one or more repeat to occur inside the parse, and skip advances the parse scanner (used here to skip over non integers).
code:
rebol []
parse [a 1 and a 2 and a 3 and a 1024] [
    some [
        set val integer! (print [val "squared is" val * val])
        | skip
    ]
]

1 squared is 1
2 squared is 4
3 squared is 9
1024 squared is 1048576
== true


Part of the reason for this example is also to raise curiosity so that you will want to visit the online manual at rebol.com to find out more.

Please note; I froth at the mouth a little bit when I evangelize REBOL, out on the lunatic fringe. Cooler heads do prevail in the majority of the REBOL community and others can do a better job of explaining things than I. Think of me as the nut case sports fan all dressed in body paint with a flame spewing top hat, far too excited to actually make any sense, but meaning well, and hopefully adding to the fun of the event.

And I just realized, I didn't even show a keyword Dialect or DSL. Maybe next time if anyone is curious for more.

Cheers
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Sat Apr 05, 2008 1:04 pm   Post subject: Re: Tutorial: REBOL parse

Nice!
You should ask Tony if you could make a REBOL Forum Very Happy
btiffin




PostPosted: Mon Apr 07, 2008 9:35 am   Post subject: Re: Tutorial: REBOL parse

Thanks A.J.

I'd do my best to populate a REBOL forum. Smile

Next Tutorial will either be PARSE and DSL or something on the ease of Client / Server when using the message passing features of REBOL.

Cheers,
Brian
Mackie




PostPosted: Mon Apr 07, 2008 9:44 am   Post subject: RE:Tutorial: REBOL parse

If you want to start a Rebol forum, you should make some more, introductory tutorials. If people start leanring, and posting the creation, a forum would probably follow, if you want REBOL forum that is.
btiffin




PostPosted: Mon Apr 07, 2008 12:23 pm   Post subject: Re: Tutorial: REBOL parse

Mackie; I was thinking the same thing. Let things build up until it seems reasonable to have a full forum. One thing that happened on RebolTalk ... too many forum divisions. There is definitely a balance; one that is easily broken, but hard to define.

I've been plopping recent REBOL related posts in this Functional Programming group. A small misnomer, as REBOL isn't clearly defined as Functional ... nor is it OO, nor declarative, nor ... but it has features of many of these paradigms.

I won't be pestering Tony anytime soon. Wink But I do hope that the august members of this board give REBOL at least a glance.

Cheers
Mackie




PostPosted: Mon Apr 07, 2008 6:42 pm   Post subject: RE:Tutorial: REBOL parse

I'll be glad to check out REBOL, once I have more experience. For now I'm sticking with Ruby. Very Happy but it does look cool.
reboltutorial




PostPosted: Sun Jun 28, 2009 11:44 pm   Post subject: RE:Tutorial: REBOL parse

I read chapter 15:
http://www.rebol.com/docs/core23/rebolcore-15.html#section-8"

Code:
spacer: charset reduce [tab newline #" "]
spaces: [some spacer]
rule: ["a" spaces "b" spaces "c"]
parse/all "a b c" rule

is OK but if I change rule to just
Code:
rule: ["a" spaces copy varb to spaces "c"]
parse/all "a b c" rule

Rebol Console outputs error:

Code:
** Script Error: Invalid argument: some spacer
** Where: halt-view
** Near: parse/all "a b c" rule
>>

Why ?
btiffin




PostPosted: Tue Jul 21, 2009 7:13 pm   Post subject: Re: Tutorial: REBOL parse

TO sadly only accepts datatype! or value, not a pattern or dialect command.

http://www.rebol.com/docs/core23/rebolcore-15.html#section-10.3

If you'd like to work out alternative parse mechanics, we can talk more.

Something like
code:

parse/all "a b c" ["a" some spacer copy varb to "c" to end]
maybe? Depends on what you actually want to capture in varb. This will give "b " and the to end will allow the parse to succeed. Without the to end, parse will return false (having not exhausted the input), but will still set varb to "b ".

Cheers
Sponsor
Sponsor
Sponsor
sponsor
tbrown01




PostPosted: Thu Jan 14, 2010 12:57 am   Post subject: RE:Tutorial: REBOL parse

Great!

You've done your assignment well!

Keep it up....


*removed links*
chiropractic marketing




PostPosted: Sat Sep 25, 2010 2:14 am   Post subject: Re: Tutorial: REBOL parse

Is there anything anymore that's considered a general programming language? Kind of like C used to be, or is it still?





*removed link*
DtY




PostPosted: Sat Sep 25, 2010 6:54 pm   Post subject: Re: Tutorial: REBOL parse

chiropractic marketing @ Sat Sep 25, 2010 2:14 am wrote:
Is there anything anymore that's considered a general programming language? Kind of like C used to be, or is it still?






*removed link*
I love how this message is *almost* passable as something a human would say.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: