From: lonewolf on
For our project, we need to analyze C source code (our input) and
determine where or not a for loop exists (so we can add OpenMP
directives beforehand if it will help). How would you recommend we do
this? Our current approach is a string parser but we do not know in
detail better approaches that might exist such as syntax trees, etc.
Any available libraries?

Thanks

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ira Baxter on
"lonewolf" <jon.sero(a)gmail.com> wrote in message
news:fc2201c7-331d-44fc-ae98-db33151ff5c7(a)j4g2000yqe.googlegroups.com...
> For our project, we need to analyze C source code (our input) and
> determine where or not a for loop exists (so we can add OpenMP
> directives beforehand if it will help). How would you recommend we do
> this? Our current approach is a string parser but we do not know in
> detail better approaches that might exist such as syntax trees, etc.
> Any available libraries?

"String parsers" are an extremely unreliable way to analyze program
source code.

The DMS Software Reengineering Toolkit is a program transformation system
with C and C++ front ends for many dialects of these languages, including
GCC.
It can be used to parse C or C++ code to abstract syntax trees,
carry out arbitrary analyses and transformations on the code,. and
regenerate compilable source code complete with comments.
See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html
It has been used to analyze and transform very large source code
bases automatically.

Transformations are stated as a mixture of source-to-source rewrite rules
("if you see <this syntax>, replace it by <that syntax>") and procedural
transforms (change the abstract syntax tree directly), each conditioned
on some applicability analysis. These could
be used to insert OpenMP directives.
See http://www.semanticdesigns.com/Products/DMS/ProgramTransformations.html
for background on source-to-source transformations.

While it would be very easy to use the rewrite rule patterns to represent
so-called looping constructs ("for", "while", ..) often the language syntax
for loops is misleading. One can construct loops with gotos, for instance,
and one can unconditionally goto out of a loop in the middle making it
not-a-loop.
DMS provides control and data flow analysis for C programs; the control
flow analysis determines what the the actual control flow is and makes
that available to drive the transformations.

Check out the discussion on flow analysis at
http://www.semanticdesigns.com/Products/DMS/FlowAnalysis.html.

DMS is designed to carry out automated program changes.

--
Ira Baxter, CTO
www.semanticdesigns.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Frank Birbacher on
Hi!

lonewolf schrieb:
> Our current approach is a string parser but we do not know in
> detail better approaches that might exist such as syntax trees, etc.

for loops are detected by searching for the keyword "for". There is a
program "grep" which does this
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
In order to ignore comments in the source and in order to detect loops
generated by marcos you should run the c++ preprocessor. gcc has a mode
which does this (compiler option -E). The preprocessed output will
contain hints to find source file and line for any loop.

Frank

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]