General parsing function

Hi all,
I would like to parse a txt file in order to import it into Matlab so it is much more easier to treat.
The format of the txt file is kind of homemade, it looks like json but it doesn't seem to be so.
This is how it looks:
generator : "PAM-RTM 2011.0";
format_version : 2011.0;
date : "Thu May 03 15:04:21 2012";
General_Parameters {
simulation_description : "";
sampling_period : 10;
recover_sampling_period : 500;
max_experiment_time : 1.000000E+006;
fixed_dt_max : 1.000000E+025;
}
material {
name : "Default Resin";
family : material;
type : resin;
density : 1.000000E+003;
nb_vars_specific_heat : 0;
specific_heat {
family : function;
name : noname;
}
}
Of course I could do it with regexp() function, but I would like something robust. So I am looking for a function in which I could say what are the separating characters (such as {} : ;) and that generates a Matlab structure. Does it exists?
BR,
Max

2 Comments

Jan
Jan on 4 May 2012
As long as you do not define the wanted output format, an answer must contain a lot of guessing.
What makes you think that doing it with regexp is going to be robust? Or is it that you are worried that it is not going to be robust if you write it? It doesn't seem to me to be that hard of a question.

Sign in to comment.

Answers (2)

Jan
Jan on 4 May 2012

1 vote

No, due to the high level of generality, I'm sure there is no such function.
maxroucool
maxroucool on 4 May 2012

0 votes

Thanks for your answers.
It's not that i don't trust regexp but rather that i don't trust my regexp skills. Anyway i will try it myself!
Br,

3 Comments

Jan
Jan on 4 May 2012
I would do this without REGEXP. Creating a robust version will be complicated. E.g. the function should not be confused by the curly braces in:
General_Parameters {
simulation_description : "}";
So what would you suggest without using regexp?
What *I* would suggest would be to write a BNF grammar that matches your needs, and implement it in yacc and lex. That will produce C code, that you can write a mex wrapper for.
If you do not already know yacc and lex, I would suggest to you that it would be worth your time studying them: not necessarily for the syntax, but because they deal with fundamental text parsing concepts that you need to reproduce in whatever implementation you use.

Sign in to comment.

Categories

Tags

Asked:

on 4 May 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!