[Lazarus] C++ parser

Hans-Peter Diettrich DrDiettrich1 at aol.com
Wed Mar 10 16:31:30 CET 2010


Juha Manninen schrieb:

> This is not important but just some ideas:
> 
> I understood ToPas converter is in good shape, except for #defines support.

The technical support of #defines is okay, missing is only an easy to 
use classification of the #defines into constants, functions and macros. 
By default all #defines are expanded, so that e.g. the C equivalent of 
named constants is lost.


> Now, C++ classes map almost directly to OP classes. If you decide to add 
> them to ToPas, it would be interesting to try it for C++ Builder projects.

Classes only map *syntactically* to Class (or more directly to Object), 
but the lifetime (of local objects) and the initialization differs from 
the OPL model. This means that Free has to be added at the end of a 
block or subroutine, and eventually try-finally blocks must protect the 
resources.


> #defines are not used much in C++ because of templates, but that doesn't 
> help pascal conversion much.

In Windows applications a lot of #defines reside in the Windows headers.

> And there are many other syntax additions, 
> I know, so the task is not easy.

My primary goal was a reliable translation of header files, usable 
without knowledge of C/C++. This works fine for C header files, but can 
not be achieved for C++ headers - the result would be unusable.


> The conversion should just leave some parts intact and mark them with 
> proper comments.

That's quite a different goal. I already wrote some (simple) conversion 
tools, that e.g. reverse "int x" into "x: int", in declarations and 
parameter lists. The procedure differs from header (or C) translation in 
so far, as such a translator should work with incomplete information, 
what requires a very different parsing strategy - a bottom-up (LR) 
parser were more appropriate for this task than the top-down (LL) parser 
of ToPas. I noticed this already in an attempt to classify #defines 
automatically.

> There is one C++ parser quite near. SynEdit has one, but I think it is 
> very simple and not suitable for conversion.

I'd prefer a tool like TextTransformer <http://www.texttransformer.com> 
to process C++ source code. AFAIR it already comes with a C++ grammar, 
and allows to insert any translation code into the grammar - just like 
the name suggests.

TextTransformer is written in C++ (BCB6), and can produce parser source 
code in C++. It also includes an C++ interpreter for user supplied 
transformation code. This should make it easy to convert C++ projects, 
when the users are more familiar with C++ than with OPL. The integrated 
debugger comes with single step and breakpoint capabilities, and allows 
to inspect both the input and output text, what makes it easy to write 
and debug the added transformation code.

I also wrote Delphi components for TextTransformer 
<http://sourceforge.net/projects/tetrapack/>, that allow to e.g. build 
parse trees for later use in Delphi code. These components may be usable 
with Lazarus, too, but at that time I was not familiar enough with 
Lazarus to check or adopt my implementation.

DoDi





More information about the Lazarus mailing list