Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-05

Parsing parameters inside an interpreter

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,

I trying to build a very small interpreter. I can type commands like:

$ add "Luciano de Souza"

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" ', params);

After this command, the parameter would have the following values:

params[0] := 'add';
params[1] := 'Luciano de Souza';
params[2] := '';

My question is: there is a way to do it easily using a standard class of Freepascal?

Luciano

Reply by email

Mark Morgan Lloyd
to luciano de souza: I trying to build a very small interpreter. I can type commands like: $ add "Luciano de…
luciano de souza wrote 22 lines
Hello all, I trying to build a very small interpreter. I can type commands like: $ add "Luciano de Souza" 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" ', params); After this command, the parameter would have the following values: params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := ''; 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]

Reply by email

Ludo Brands
to luciano de souza: I trying to build a very small interpreter. I can type commands like: $ add "Luciano de…
Show quoted text 23 lines
I trying to build a very small interpreter. I can type commands like: $ add "Luciano de Souza" 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" ', params); After this command, the parameter would have the following values: params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := ''; My question is: there is a way to do it easily using a standard class of Freepascal?

Use TSTringList.DelimitedText:

  params.Delimiter:=' ';
  params.StrictDelimiter:=false;
  params.QuoteChar:='"';
  params.DelimitedText:='add "Luciano de Souza" ';

results in

 params[0] := 'add';
 params[1] := 'Luciano de Souza';
 params[2] := '';

Ludo

Reply by email

Lukasz Sokol
to luciano de souza: I trying to build a very small interpreter. I can type commands like: $ add "Luciano de…
luciano de souza wrote 27 lines
Hello all, I trying to build a very small interpreter. I can type commands like: $ add "Luciano de Souza" 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" ', params); After this command, the parameter would have the following values: params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := ''; My question is: there is a way to do it easily using a standard class of Freepascal? Luciano

In my 'poor-mans interpreter' I use and pass around TStringList's quite a lot; although some special treatment of the string is required, to preserve spaces within parameter strings.

L.

Reply by email

Lukasz Sokol
to Lukasz Sokol
Lukasz Sokol wrote 35 lines
On 13/05/2012 11:20, luciano de souza wrote: > Hello all, > > I trying to build a very small interpreter. I can type commands > like: > > $ add "Luciano de Souza" > > > 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" > ', params); > > After this command, the parameter would have the following values: > > params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := > ''; > > My question is: there is a way to do it easily using a standard > class of Freepascal? > > Luciano In my 'poor-mans interpreter' I use and pass around TStringList's quite a lot; although some special treatment of the string is required, to preserve spaces within parameter strings. L.
Or use StrictDelimiter := False like Ludo suggested :)))

L.

Reply by email

Jorge Aldo G. de F. Junior
to Lukasz Sokol

I have a parser function somewhere around that can do more than that... let me find it...

2012/5/14 Lukasz Sokol :

Show quoted text 44 lines
On 14/05/2012 10:39, Lukasz Sokol wrote: > On 13/05/2012 11:20, luciano de souza wrote: >> Hello all, >> >> I trying to build a very small interpreter. I can type commands >> like: >> >> $ add "Luciano de Souza" >> >> >> 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" >> ', params); >> >> After this command, the parameter would have the following values: >> >> params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := >> ''; >> >> My question is: there is a way to do it easily using a standard >> class of Freepascal? >> >> Luciano > > In my 'poor-mans interpreter' I use and pass around TStringList's > quite a lot; although some special treatment of the string > is required, to preserve spaces within parameter strings. > > L. > Or use StrictDelimiter := False like Ludo suggested :))) L. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

luciano de souza
to luciano de souza: I trying to build a very small interpreter. I can type commands like: $ add "Luciano de…

Fortunately, my case is the simplest one. There will not quotes inside quotes. Therefore, I believe TStringlists will sove the whole problem. It's really amasing what we can do with a TStringlist.

Thank you for the precious tips!

2012/5/13, luciano de souza :

Show quoted text 26 lines
Hello all, I trying to build a very small interpreter. I can type commands like: $ add "Luciano de Souza" 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" ', params); After this command, the parameter would have the following values: params[0] := 'add'; params[1] := 'Luciano de Souza'; params[2] := ''; My question is: there is a way to do it easily using a standard class of Freepascal? Luciano

Reply by email