Re: Weird spelling fixes in 2.1.107

Peter T. Breuer (ptb@it.uc3m.es)
Sat, 27 Jun 1998 18:35:45 +0200 (MET DST)


"A month of sundays ago ptb wrote:"
> Perhaps one thing people need is a dynamically configurable command
> line parser, like regexec, except that it takes a grammar as a -e option
> and then filters the input against that. There are already a few such
> things (tcl's regexp?). I just had a 5 minute try at implementing a
> dynamically cionfigurable parser as a shell script. Looks like it works
> (i.e. it parsed abcabab OK when I told it to!). One more thing for the
> TODO queue.

This is the trial script that I just wrote to make sure that it was easy
to do arbitrary parsing in shell script (or anything ...). I only post
it in case there really are lots of people who think that parsing is
weird and difficult, in the hope that they'll look and see how the trick
is done and hence not write inflexible stuff that breaks needlessly when
a minor interface change is made.

I don't claim this is a sophisticated approach, or anything. In
particular I didn't bother with more than one token of lookahead.
I only post it to point out that it's easy to do this.

Peter ptb@it.uc3m.es

#! /bin/sh

# a little example parser generator/interpreter in sh. This is 1TLA.
# It could be more since the input is buffered. Example of use at end.
# ptb@it.uc3m.es 1998

FAILURE=-1
SUCCESS=0

and() {

# And of parsers. Expect a list
# p1 p2 p3 ...

while [ $# -gt 0 ] ; do

# to do more than 1TLA we would have to save and restore context here

$1 && shift && continue
return $FAILURE

done

return $SUCCESS

}

or() {

# Or of parsers. Expect a list
# p1 p2 p3 ...

echo or got $* >&2

while [ $# -gt 0 ] ; do

$1 && return $SUCCESS
shift

done

return $FAILURE

}

nothing() {

# no args. The trivial parser.

return $SUCCESS
}

option() {

# An optional parser constructor. Expect args
# p1

or $1 nothing && return $SUCCESS \
|| return $FAILURE
}

# ------ input buffer (get and unget) ---------------

line=""
char=""

get(){
while [ -z "$line" ]; do
read line || return -1
line="$line"
done
char="`expr substr \"$line\" 1 1`"
line="`expr substr \"$line\" 2 10000`"
return 0
}

unget(){
line="$1""$line"
return 0
}
# ----- end of input buffer ----------------

exactly() {

# Parser that recognizes a specific token.
# The single argument is the token to be recognized

get || return $FAILURE
if [ "$1" = "$char" ]; then
return $SUCCESS
else
unget "$char"
return $FAILURE
fi
}

attach() {
# arg is p1 something_to_do which MUST return SUCCESS

$1 && shift && $* && return $SUCCESS \
|| return $FAILURE
}

################### example ###############################

p2(){
and "exactly a" "exactly b" "exactly c"
}
p3(){
and "exactly b" "exactly c"
}
p1(){
or p2 p3
}

main() {

# example: abc | bc

p1 && echo I saw abc or bc

}

main

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu