« april 2009 | Main | augusti 2009 »

maj 03, 2009

spipat: SNOBOL/SPITBOL-patterns i C++ och Python

spitpat är ett C/C++-bibliotek som implementerar SNOBOL/SPITBOL-patterns för C++ och Python. Så här presenteras spitpat i distributionens fil README:
This is a transliteration (largely mechanincal translation) to C99
of the GNU Ada Translator (GNAT) SPITBOL.Pattern (spipat) package by
Robert Dewar, the creator of SPITBOL GCC v3 provides all needed
features.

The primary goal is to enable embedding SNOBOL4 style patterns in
popular scripting languages (ie; Ruby, Python, Perl, JavaScript),
which are almost universally written in C.

Testing and programming pattern construction in C is painful, due to
the lack of operator overload. To hasten testing, I've written a C++
wrapper "Pattern" around the C spipat library. The goal is to enable
testing, and provide an experience familiar to SNOBOL programmers. A
more pure C++ approach might be to use method chaining to construct
patterns, and to implement the entire package as a template that takes
an arbitrary scalar type to represent a character.
Naturligtvis är det Phil Budne som gjort denna translitterering; det är han som ligger bakom den fria SNOBOL4-implementationen CSNOBOL4 full av klockor och visslor.

C++
Så här kan SNOBOL/SPITBOL se ut i C++:
#include 
#include 

using namespace std; 

#include 

int main() {
 string subject1("Hello World!");
 string hello("Hello");
 Pattern hello_pattern(hello);
 Pattern world_pattern("World");
  
 // pattern w/ concatenation
 Pattern p1 = hello_pattern & ' ' & world_pattern;
 Pattern p2 = hello_pattern & ' ' & world_pattern & Arb();
 Pattern p3 = hello_pattern & Arb() & world_pattern;

 Matchres m1;
 cout << "p1: " << p1 << "\n";
 cout << "p2: " << p2 << "\n";
 cout << "p3: " << p3 << "\n";
 cout << "subject1 ~ p1: " << Match(subject1, p1) << "\n";
 cout << "subject1 ~ p2: " << Match(subject1, p2) << "\n";
 cout << "subject1 ~ p3: " << Match(subject1, p3) << "\n";

 return(0);
}

Pythonstöd
Än så länge finns det - av de agila programspråken - endast stöd för Python men ovanstående antyder att det även kan komma stöd för andra programspråk. Det finns ett par exempel i distributionen, t.ex. nqueens där det emfatiska området är de SNOBOL-specifika delarna
# from Victor Berstis' Minnesota SNOBOL4 distribution
# N queens problem, a string oriented version to demonstrate the power
# of pattern matching.

# translated to Python w/ spipat by Phil Budne

from spipat import *

n = 8
nm1 = n - 1
np1 = n + 1
nsz = n * np1

# This pattern tests if the first queen attacks any of the others:
test = break_("Q") + "Q" + (arbno(len_(n) + "-") + len_(n) + "Q"  |
			    arbno(len_(np1) + "-") + len_(np1) + "Q" |
                            arbno(len_(nm1) + "-") + len_(nm1) + "Q")
p = len_(nm1) * 'x' + len_(1)
l = "Q" + "-" * nm1 + " "

solution = 0
def solve(b):
    global solution

    if len(b) == nsz:
        solution = solution + 1
        print "Solution number", solution, "is:"
        while len(b) >= np1:
            print b[0:np1]
            b = b[np1:]
    else:
        # Add another row with a queen:
        b = l + b
        # "LOOP"
        i = 0
        while i < n:
            i += 1
            if not test.match(b):
                solve(b)

            # "NEXT"
            # Try queen in next square:
            m = p.match(b)
            if m:
                b = m.repl('-' + m.dict()['x'])

solve('')
Se även
* För andra SNOBOL/SPITBOL-relaterade saker, se www.snobol4.org.
* David Shield har nyligen skrivit en del SNOBOL/SPITBOL-relaterat på sin blogg The Wayward Word Press , t.ex. Announcing New LinkedIn Group, SPITBOL , Announcing spitbol, a New Project Hosted at Google Code.

Posted by hakank at 09:42 FM Posted to SNOBOL/SPITBOL