I’m coding a project in C++, and I’m looking for a way to put in a text file ( usergrammar.txt ), in a humanly readable/writable form, a user-defined grammar which defines objects.
Once done this I’d like to be able to instance some of those objects in the code, and save the created instances in another file ( instances.txt ), always in humanly accessible format. This second file is obviously dependent on the first to be read correctly.
What I’m having trouble with is how to define the grammars and parsing them. I’ve been looking at Boost::Spirit, but while it would do a good job on reading the first file ( since its rules are predefined ), I don’t think it is applicable to the second one since Spirit grammars are only defined a compile time and cannot be loaded at runtime ( at least that’s what I understood ).
Now I’m reading about the ENBF form, but I also have the problem that I not only have the language rules, but that each object has a particular and different name/description/options every time its included in another one ( For example, if I have the object colour, the integers inside it would be called red, blue, yello, while if the object is ruler, its integer would be called length ), so I have a way to include these informations into the file as well, and getting them to be associated with the corrisponding values correctly.
Do you have any pointers to what to look/study/use/do for a project like this?
Thanks in advance.
If you are open to having some restrictions on the format of your files, you could look into using XML or JSON to describe your grammars.
You could have a grammar file that looks like
After reading in this file (using some pre-existing library, most likely), you could then read in another file that would have your actual objects. It could look something like this:
And so forth. A more descriptive overview of JSON is given here, which includes links to several C++ libraries that handle JSON (My personal favorite is JsonCpp).