Java C Parser/Preprocessor

Java C Parser/Preprocessor

Post by student0 » Wed, 02 Apr 2008 15:26:45


Hi I'm currently looking for a C parser/preprocessor that's written in
Java (since I plan to be using the Prefuse visualisation toolkit) for
a student project.

I looked at ANTLR but the gcc translation framework grammar that's
available on its website is fairly dated and I can't seem to find any
outside projects which use it and can verify its reliability. Also
it's only works with already preprocessed C and may not be able to
emit comments.

Does anyone know any good C parser and preprocessor tools written in
Java? Hopefully it is able to use an abstract syntax tree so that I
can perform my own code transformations easily.
 
 
 

Java C Parser/Preprocessor

Post by Mark Spac » Wed, 02 Apr 2008 23:51:26


Have you looked at these? (First or second link when I Google for "java
parser").

< http://www.yqcomputer.com/ ;

 
 
 

Java C Parser/Preprocessor

Post by Roedy Gree » Thu, 03 Apr 2008 02:10:12

On Mon, 31 Mar 2008 23:26:45 -0700 (PDT), student08
< XXXX@XXXXX.COM > wrote, quoted or indirectly quoted someone who
said :


see http://www.yqcomputer.com/

--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/
 
 
 

Java C Parser/Preprocessor

Post by Vlad Ciubo » Thu, 03 Apr 2008 03:36:29

I'm pretty sure javacc has a C parser in its repository.
 
 
 

Java C Parser/Preprocessor

Post by student0 » Thu, 03 Apr 2008 10:33:17

Hi first off thanks for the replies all.

I've looked at JavaCC however its only has one rudimentary C grammar
in its repository which can't handle preprocessed code. It seems to be
the same for most of the others including Coco so I'm looking into
Rats! in more detail and hopefully it will be the exception.

However if anyone's worked with a Java parser which can handle C
please feel free to mention it.

Thanks
 
 
 

Java C Parser/Preprocessor

Post by EJP » Thu, 03 Apr 2008 18:32:45

You won't find a C preprocessor written with a parser-generation tool.
It's not that kind of problem.
 
 
 

Java C Parser/Preprocessor

Post by Larry A Ba » Sat, 05 Apr 2008 00:12:02


It is that kind of problem. Most C preprocessors are written
using a well-divided lexer and parser (the latest GNU C
preprocessor, for example). I'm sure you can find some that
use parser-generators for the parse, though most are probably
recursive-descent. You can write a C preprocessor as more
of a lexer + state machine, but you would still want to use a
parser for the constant integer expression evaluation.

To the OP (the first post is missing from my news server, so
I can't respond to it):

What is the purpose of your code transformations? Will you
perform transformations at the preprocessor-level, so that
you need a preprocessor syntax tree? Is your goal to modify
the original source code?
 
 
 

Java C Parser/Preprocessor

Post by student0 » Wed, 09 Apr 2008 15:59:17

On Apr 3, 11:12m, "Larry A Barowski"




Hi Larry thanks for the reply. My goal is to be able to highlight
possible refactorings in the source code to the user and then if the
user approves of the refactorings, to perform it by modifying the
original source code.
 
 
 

Java C Parser/Preprocessor

Post by Larry A Ba » Wed, 09 Apr 2008 23:00:12


Preprocessing makes that very complicated. Macros
can join tokens and do other *** things that make
even simple things like variable renaming difficult.
A handful of conditional blocks and macros could
result in thousands of possible syntax trees, and
refactoring for one would likely break others. For
more information on these problems and possible
solutions, as well as books and papers on the topic,
google for:
c++ refactor preprocessor "conditional compilation"

When the C/C++ preprocessor is in play, the actual
refactoring is the easy part. If this is for a
proof-of-concept type of project dealing with
refactoring, you might want to just make it work for
code that is free of preprocessing directives, or
choose a language for which a preprocessor is not
normally used. Otherwise, I suggest you review the
literature on the problem. Maybe there is an
open-source project that you can use as a starting
point, or maybe a thorough understanding of the
problem will cause you to give up on it.
 
 
 

Java C Parser/Preprocessor

Post by Yunzhon » Thu, 10 Apr 2008 04:47:58

Larry, which version of GNU C preprocessor did you check that used a
parser generator?
 
 
 

Java C Parser/Preprocessor

Post by Roedy Gree » Thu, 10 Apr 2008 05:30:16

On Tue, 1 Apr 2008 18:33:17 -0700 (PDT), student08
< XXXX@XXXXX.COM > wrote, quoted or indirectly quoted someone who
said :


There are some preprocessors written in Java, but not C style. Since
there are so many C-style preprocessors written in C, you'd think
someone would want to revamp the aged syntax if they were to write a
new one in Java.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/
 
 
 

Java C Parser/Preprocessor

Post by Roedy Gree » Thu, 10 Apr 2008 06:35:02

On Mon, 31 Mar 2008 23:26:45 -0700 (PDT), student08
< XXXX@XXXXX.COM > wrote, quoted or indirectly quoted someone who
said :



see http://www.yqcomputer.com/
for a list of preprocessors associated with Java. Perhaps one might
serve as a base.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/
 
 
 

Java C Parser/Preprocessor

Post by Larry A Ba » Thu, 10 Apr 2008 08:10:01


I only said it had a well-divided lexer and parser, and
guessed that there are some C preprocessors out there
that use a parser generator. jGRASP, for example,
contains a one-pass C/C++ preprocessor+lexer with
a constant integer expression parser generated by
bison (the rest is a lexer and state machine), but it is
not open source.
 
 
 

Java C Parser/Preprocessor

Post by Lew » Thu, 10 Apr 2008 14:56:28


Good news.

--
Lew