expression parser (simple) in EU

new topic     » topic index » view thread      » older message » newer message

Here's a snippet I wrote some time ago from what is to be my Euphoria
version of a database program I originally wrote in Forth. This takes a
text string (e.g., a line of commands entered by the user), removes
spaces and tabs and puts the items in that string away in a sequence,
where the program can easily test the string for various criteria (e.g.,
whether the string is grammatically correct; so it can also be used in
programs for processing natural languages, etc.).

I've written a few lines around the function to test / demonstrate its
use.

Maybe this function is useful for all those Euphorians out there who are
writing language interpreters etc. (I've written such functions in
Forth, Pascal, Modula-2, C, Basic, but... Euphoria is Progress. The
Euphorian 'sequence', combined with practically unlimited memory, makes
this kind of thing easy to write.)

---SNIP---

-- exper. for testing the fnc. 'dissect'
sequence invoer
sequence uitkomst

function dissect(sequence entry)
--takes a string 'entry' and returns either {0} or
--{number_of_words,{{word},{word}...}}
--by Robert Zydenbos, Mysore (India)
--version date 29.7.1997
sequence arg --contains the words found in 'entry'
sequence a   --word being found
integer wel  --boolean: are we in the middle of a word being found?
integer l    --length of 'entry'
object kar   --character taken from 'entry'
    arg = {}
    a = {}
    wel = 0
    l = length(entry)
    for x=1 to l-1 do
        kar = entry[x]
        --the next line was just for debugging:
        --printf(1,"kar = %s\n",kar)
        if kar=' ' or kar=9 then
            if wel then
                arg = arg & {a}
                wel = 0
                a = {}
            end if
        else
            if not wel then
                wel = -1
            end if
            a = a & kar
        end if
    end for
    if length(a) then
        arg = arg & {a}
    end if
    if not length(arg) then
        return {0}
    else
        return {length(arg)} & {arg}
    end if
end function

--main portion
clear_screen()
puts(1,"Enter some text: ")
invoer = gets(0)
printf(1,"The entered text was: %s\n\n",{invoer})
--now comes what all this is about:
uitkomst = dissect(invoer)
puts(1,"Result:\n")
? uitkomst
if uitkomst[1] then
    puts(1,"I.e. (with a vertical bar after each item):\n")
    for x=1 to uitkomst[1] do
        printf(1,"%s|\n", {uitkomst[2][x]})
        if not compare(uitkomst[2][x],"-") then
            puts(1,"This was a dash.\n")
        end if
    end for
end if

---SNIP---

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu