Re: A very productive day using FORTH



On 19 mrt, 19:00, hel...@xxxxxxxxx wrote:

Problem was to analyze a big database of linguistic data, represented
in XML and custom SGML-like representations. <snip>
Has anyone else used FORTH for linguistic research tasks?

Not as much as for linguistic research. However, I did use Forth to
analyse an XML file. I treated the tags as words (and at times as
terminator). This approach was not only much faster than using
ordinary languages, but also very easy to maintain. For those
interested, I added the significant parts here. The file was generated
by Cisco Works.

Hans Bezemer

[needs lib/throw.4th]
\ structure to store data
struct
1 field Dirty \ flag: dirty buffer
17 field IPServer \ IP address of server
33 field AltServer \ Hostname of server
33 field AltSwitch \ Hostname of switch
17 field Poort \ Port of server
33 field PoortNaam \ Naam van poort
33 field Duplex \ Duplex description
17 field Mbit \ Speed in Mbit
33 field V-LAN \ Applicable VLAN
33 field MAC \ MAC address of server
end-struct /Patch

/Patch string Patch

today weekday value thisday \ calculate weekday

char < constant "<" \ ASCII code character "<"
char > constant ">" \ ASCII code character ">"
char / constant "/" \ ASCII code character "/"
char . constant "." \ ASCII code character "."
char , constant "," \ ASCII code character ","
char ' constant "'" \ ASCII code character "'"

250 constant /GO \ maximum number of statements/
GO

: is-bl bl = ; \ true if a space
: is-slash "/" = ; \ true if a slash
: is-dot "." = ; \ true if a dot
: is-no-digit is-digit 0= ; \ true if not a digit
: <tag "<" parse ; ( -- a n)
: tag> ">" parse ; ( -- a n)
: <tag> <tag 2drop tag> ; ( -- a n)
: chop 1 /string ; ( a n -- a+1 n-1)
: .' "'" emit ; ( --)
: ., "," emit space ; ( --)
: >> count .' type .' ., ; ( a n --)
: GO cr ." GO" cr ; ( --)
: ?GO dup /GO = if /GO - GO else cr 1+ then ;
: clean s>lower -leading -trailing ; ( a1 n1 -- a2 n2)
: >cell clean number ; ( a n1 -- n2)
: Numfield> is is-type split >cell ; ( a1 n1 xt -- a2 n2 n3)
: DateField> NumField> -rot chop ; ( a1 n1 xt -- n3 a2 n2)
: clear Patch /Patch 0 fill ; ( --)
: tag>? tag> compare if E.USER throw then ;
: field! <tag clean rot place tag>? ; ( a1 n1 a2 --)
: IP> ['] is-dot NumField> <# "." hold # # # #> Patch -> IPServer ;
( a1 n1 -- a2 n2 a3 n3 a4)
: /UTData ( --)
Patch -> AltServer count dup 0= if 2drop Patch -> PoortNaam count
then
dup >r Patch -> AltServer place r> \ use PortName if Hostname
empty

0<> Patch -> Dirty c@ and if \ if we have a dirty buffer,
write sql
." INSERT INTO Patch ( IPServer, AltServer, AltSwitch, Poort,
Duplex, Mbit, VLAN, MAC, WeekDay ) VALUES ( "
Patch -> IPServer >>
Patch -> AltServer >>
Patch -> AltSwitch >>
Patch -> Poort >>
Patch -> Duplex >>
Patch -> Mbit count type .,
Patch -> V-LAN >>
Patch -> MAC >>
thisday . [char] ) emit ?GO
then clear \ signal buffer is clean
;
\ check if data is recent
enough
: LastSeen ( --)
today jday <tag \ get julian date today
['] is-slash DateField> \ get year
['] is-slash DateField> \ get month
['] is-bl DateField> 2drop swap rot \ get day
jday - 30 > if clear then \ less than a month old?
s" /LastSeen" tag>? \ check closing tag
;
\ convert the IP address
: IPAddress ( --)
<tag IP> place chop IP> +place chop IP> +place chop
>cell <# # # # #> Patch -> IPServer +place s" /IPAddress" tag>?
;
\ convert the hostname
: HostName ( --)
['] is-dot is is-type \ check for a dot
<tag split clean Patch -> AltServer place 2drop
s" /HostName" tag>? \ check closing tag
;

: Port ( --)
['] is-no-digit is is-type \ setup for -SPLIT
<tag clean -split Patch -> Poort place
number <# # # #> Patch -> Poort +place
s" /Port" tag>? \ check closing tag
;
\ convert the following fields
: UTData true Patch -> Dirty c! ; ( --)
: PortSpeed <tag clean 1- Patch -> Mbit place s" /PortSpeed" tag>? ;
: MACAddress s" /MACAddress" Patch -> MAC field! ;
: DeviceName s" /DeviceName" Patch -> AltSwitch field! ;
: VLAN s" /VLAN" Patch -> V-LAN field! ;
: PortName s" /PortName" Patch -> PoortNaam field! ;
: PortDuplex s" /PortDuplex" Patch -> Duplex field! ;

.



Relevant Pages

  • Microsoft XML Diff/Patch -- how to output differences
    ... Thanks to this newsgroup I've been pointed to Microsoft's XML Diff & Patch ... I've been reading about how I can use the tools to generate a ... I was wondering if there's any way I can use the diffgram to ...
    (microsoft.public.dotnet.xml)
  • Microsoft patches IE, but Firefox is still safer
    ... Microsoft recently announced that a special, out-of-cycle patch would ... If you haven't yet patched IE to protect against the XML exploit, ... visit Microsoft's December 2008 security advisory. ... Server 2003, and Server 2008. ...
    (misc.news.internet.discuss)
  • Re: windowsupdate und KB832894
    ... >> file, trusted sites list, exceptions list in the Internet Options ... > Der Patch korrumpiert die XML-Funktionalität ... Hallo Jörg, ... den XML patch habe ich ausgeführt. ...
    (microsoft.public.de.german.inetexplorer.ie6)
  • BizTalk 2004 :: Business rules engine conumdrum
    ... Normally when calling rules in a orchestration you need to target a ... specific document schema and then modify the XML ... XML to be processed? ... I have this patch of xml within about 2 dozen documents and all need to ...
    (microsoft.public.biztalk.general)

Loading