How can you parse simple C++ typedef instructions?

I'd like to parse simple C++ typedef instructions such as

typedef Class NewNameForClass;
typedef Class::InsideTypedef NewNameForTypedef;
typedef TemplateClass<Arg1,Arg2> AliasForObject;

I have written the corresponding grammar that i'd like to see used in parsing.

Name <- ('_'|letter)('_'|letter|digit)*
Type <- Name
Type <- Type::Name
Type <- Name Templates
Templates <- '<' Type (',' Type)* '>'
Instruction <- "typedef" Type Name ';'

Once this is parsed, all i'll want to do is to generate xml with the same information (but layed out differently)

What is the most effective language for writing such a program ? How can you achieve this ?

EDIT : What i have come up with using Boost Spirit (it's not perfect, but it's good enough for me, at least for now)

   rule<> sep_p = space_p;
   rule<> name_p =  (ch_p('_')|alpha_p) >> *(ch_p('_')|alpha_p|digit_p);
   rule<> type_p = name_p
           >> !(*sep_p >>str_p("::") >> *sep_p>> name_p)
           >>  *(*sep_p >> ch_p('*') )
           >> !(*sep_p >> str_p("const"))
           >> !(*sep_p >> ch_p('&'));
   rule<> templated_type_p = name_p >> *sep_p
           >> ch_p('<') >> *sep_p
           >> (*sep_p>>type_p>>*sep_p)%ch_p(',')
           >> ch_p('>') >> *sep_p;

   rule<> typedef_p = *sep_p
                   >> str_p ("typedef")
                   >> +sep_p >> (type_p|templated_type_p)
                   >> +sep_p >> name_p
                   >> *sep_p >> ch_p(';')  >> *sep_p;
   rule<> typedef_list_p = *typedef_p;


I would alter the grammar slightly

ShortName <- ('_'|letter)('_'|letter|digit)*
Name <- ShortName
Name <- Name::ShortName
Type <- Name
Type <- Name Templates
Templates <- '<' Type (',' Type)* '>'
Instruction <- "typedef" Type Name ';'

Also your grammar leaves out the following cases

  1. Multiple typedef targets.
  2. Pointer targets
  3. Function pointers (this is by far the most difficult)

Parsing a grammar (i love the irony) is a fairly straight forward operation. If you wanted to actually use the grammar in a functional way, I would say the best bet is a lex/yacc combination.

But from your question it appears that you want to spit it out to another format. There really isn't a language designed for this so I would say use whatever language you're most comfortable with.


The OP asked about multiple typedef targets. It's perfectly legally for a typedef declaration to have more than 1 target. For Example:

typedef _SomeStruct SomeStruct, *PSomeStruct

This creates 2 typedef names.

  1. SomeStruct which is equivalent to "struct _SomeStruct"
  2. PSomeStruct which is equivalent to "struct _SomeStruct*"

Well, since you're apparently already working with/on C++, have you considered using Boost.Spirit? This allows you to hard-code the grammar inline in C++ as a domain-specific language and program against it in normal C++ code.

Need Your Help

How can I achieve this layout using CSS only?

html css html5 design layout

I am trying to build a layout that has two div blocks next to each other. Both of them are centered to the page. The left block (1) varies in width depending on how much space their is available ...

Is it possible to make an animation icon on the notifications window (Android)

java android statusbar android-notifications

I have done some research and found that it's possible to have a progress bar in the expanded statusbar area but I am still not sure about whether the animation icon is possible or not.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.