0

I am trying parse a c file and find all the variables that begin with "Apple_". I read the entire c file to a string and then use a regex to get all the variables. But I have a macro defined as:

#define IAMMACRO(var,ext) var##ext

so if I have

IAMAMACRO(Apple_,Variable)

I need to get the value Apple_Variable after the parsing and not Apple_.

PS: I prefer an easy solution.

learn_code
  • 47
  • 5
  • The easiest solution is to preprocess the file with a C compiler before processing. The next easiest is to write a standards-compliant C preprocessor. – molbdnilo Jan 14 '16 at 05:10
  • Is there any way to preprocess the c File in Java itself :| there is no gcc compiler in the systems here. or I could've used gcc -e. – learn_code Jan 14 '16 at 05:18
  • Use gcc's -E flag: `gcc -E -o test.p test.c` – Ram Jan 14 '16 at 05:19
  • You could try a Java based C preprocesor like [jcpp](http://www.anarres.org/projects/jcpp/). – Elliott Frisch Jan 14 '16 at 05:21
  • @Ram I am trying to create a software that is using this function. What if the user who is using the software does not have the gcc compiler? – learn_code Jan 14 '16 at 05:23
  • Perhaps you can find a suitable tool [in this SO question thread](http://stackoverflow.com/questions/1645538/is-there-a-standalone-c-source-preprocessor). – M Oehm Jan 14 '16 at 06:00

1 Answers1

0

As as understand, you want :

  • to read, then parse C (C++?) file ;
  • to embed it in a software;
  • to have an easy solution :)

So, solutions to use gcc galaxy is rather complicated, and heavy (in fact, you dont care about compiling).

Advice : dont try to parse yourself. It is a big job !

Solution: just parse. Use a library to do that.

Use ANTLR4 (not so easy), with an already-made grammar for C/C++:

ANTLR4: http://www.antlr.org/

Grammar for C++: https://github.com/antlr/antlr4-cpp

see also that: Antlr4 C++ target

hope it helps !

Community
  • 1
  • 1