[fpc-pascal] Parsing parameters inside an interpreter
Mark Morgan Lloyd
markMLl.fpc-pascal at telemetry.co.uk
Mon May 14 10:45:16 CEST 2012
luciano de souza wrote:
> Hello all,
>
> I trying to build a very small interpreter. I can type commands like:
>
> $ add "Luciano de Souza" luchyanus at gmail.com
>
> Somewhere in my code, I can have something like:
>
> procedure parse(commandline: string; var params: array of string);
>
> In this case, the commandline has the format: "add %s %s". So I would obtain:
>
> parse('add "Luciano de Souza" luchyanus at gmail.com', params);
>
> After this command, the parameter would have the following values:
>
> params[0] := 'add';
> params[1] := 'Luciano de Souza';
> params[2] := 'luchyanus at gmail.com';
>
> My question is: there is a way to do it easily using a standard class
> of Freepascal?
I'd suggest that this depends on two things: the first is whether input
is guaranteed to be on a single line, if it is (or if multiple lines
could be merged) then you could use something like the standard regular
expression unit.
The second is the complexity of your quoting and escaping, and also
whether you want to handle Unicode. If you only allow (say) " and
restrict it to a single depth, or if you could do an initial rewrite to
change nested quotes into something innocuous such as #$ff, then things
are comparatively simple.
In other cases use something like recursive descent, or investigate
(p)yacc, (p)lex etc.
--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk
[Opinions above are the author's, not those of his employers or colleagues]
More information about the fpc-pascal
mailing list