|Restricted C++ parser firstname.lastname@example.org (1994-12-28)|
|Keywords:||C++, parse, question, comment|
|Date:||Wed, 28 Dec 1994 12:10:25 GMT|
We have a homebrew object database built on top of a relational
database. For query purposes I've made an SQL like interface to this
OODB. Essentially it is a question of mapping a string to a method of
some class. E.g. if we have a class
String getName() const;
a user can issue the command
select name from ...
and the getName()-method for some object will be executed and the
returned value is printed (I skip the actual query as it is not part
of the current problem).
The mapping from the name of a method to the actual method is achieved
simply by creating an instance of a template class. The method above
would be described by the following instance.
Attribute<Object, String> attribute("name", &Object::getName);
So, for each public const method (taking no arguments) of a class I
create an Attribute instance. The file containing these instances is
then compiled in an ordinary way and linked with the actual browser
Currently I do this by hand, i.e., each time a method is added or
removed I must manually edit the file containing the Attribute
instances and add or remove an entry.
Doing this manually is, of course, tedious and rather error prone so I
decided to make this process automatic. I.e., to parse the C++ headers
and create the appropriate Attribute entries automatically.
I started from the C++ grammar as presented in section 17 of the ARM
(Annotated Reference Manual) and wrote the equivalent yacc source. As
only public methods of a class are of interest I can skip a great deal
already in the lexer,
-everything outside a class declaration
-all non-public parts of a class
-all template declarations
thus making the parser simpler. I also need not consider error
handling as I will only parse correct code and I need not pay any
attention to type checking.
Although I currently parse most C++ headers we have correctly and
undoubtly will be able to fix the remaining problems I have this
feeling that I have chosen the wrong path. Instead of having to edit
the file containing the Attribute entries I may have to change the
parser as the C++ language evolves and/or somebody writes some code my
parser isn't prepared to handle.
So, what might be the easiest and most robust way of extracting all
public const methods taking no arguments of class from a C++ header?
email@example.com; C=FI,A=Elisa,P=Nokia Telecom,S=Wikman,Gi=Johan
TL4E - P.P. Box 12, SF-02611 Espoo 61, Finland
[About ten years ago I did something like this as a front end to C. My
approach was to write the declarations in an easy to parse data language and
generate the C structures and access routines from that. -John]
Return to the
Search the comp.compilers archives again.