[DGD] Help with parse_string() grammar

Petter Nyström md1pette at mdstud.chalmers.se
Sun Apr 9 21:47:01 CEST 2006


I have a scenario where I want to parse and process parts of a string 
before it is output to a user. There are three things I am interested of 
in the string: newline characters, macro codes on the form "&c" where c is 
a single character and macro codes on the form "$name(paramlist)" where 
name is an identifier as in LPC and paramlist is a comma separated list of 
string representation of LPC data values.

Before I have tried to do this parsing with a one-pass for-loop version 
and with a three-pass version using sscanf() to search for the different 
elements. Both of these have worked okay. I would now like to do the same 
thing using parse_string() to learn how it works and see how it compares 
perfomance and feature-wise.

But I get into trouble and am not getting the grammar to work. I started 
out with just looking for the & and $ macro codes. It would be great if 
someone felt like pointing out what's not working and why and how one 
could go ahead doing it instead! This is what my basic attempts at a 
grammar looks like:

     plain      = /[^&$]*/
     name       = /[a-zA-Z0-9]+/
     char       = /[a-zA-Z0-9]/

     Output     : Output Elem
     Output     : Output
     Output     :

     Elem       : '$' name '(' ')'
     Elem       : '&' char
     Elem       : plain

I have obviously tried many alteration of this type of grammar, but as 
none yield a complete result I think I am missing something crucial.

What I for starters would like is for a string like, "A &gstring&n with a 
$macro()" to return something like, ({"A ", "&g", "string", "&n", " with a 
", "$get_macro()"}). Possible? What do I need to think of?

Regards,

Jimorie



More information about the DGD mailing list