LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   make use of gcc source code to parse c++ source file (http://www.linuxquestions.org/questions/programming-9/make-use-of-gcc-source-code-to-parse-c-source-file-757655/)

famsinyi 09-25-2009 01:11 AM

make use of gcc source code to parse c++ source file
 
Hi,

My application needs the functionality of parsing c/c++ source code. I found it can be very time consuming and error prone to write the parser. I tried to read the gcc source code to see if I can make use any of them. But the gcc code is too complex, can anyone help to give a direction on how to read the gcc source?

smeezekitty 09-25-2009 02:21 AM

YIIKKEEEESSS!!!!!
i have been trying to parse basic C for the last year
it sucks!
lex/yacc help but still you are in big trouble

lutusp 09-25-2009 03:41 AM

Quote:

Originally Posted by famsinyi (Post 3696437)
Hi,

My application needs the functionality of parsing c/c++ source code. I found it can be very time consuming and error prone to write the parser. I tried to read the gcc source code to see if I can make use any of them. But the gcc code is too complex, can anyone help to give a direction on how to read the gcc source?

The solution depends on what you are trying to do. If you want to parse the source exactly as gcc does it, why bother -- gcc already exists. So it's safe to conclude that you want something else. But you don't say what you want, and there are all sorts of parsers and parser generators.

ntubski 09-25-2009 10:29 AM

GCC-XML could be useful.

theNbomr 09-25-2009 11:05 AM

There are a few free yacc/lex grammars for C/C++. One which is allegedly somewhat of a classic is ANSI C grammar (Yacc). Depending on your requirements, it may be a very good starting point. Writing a grammar/parser in C, by hand, would be a challenge.
--- rod.

famsinyi 09-26-2009 10:41 PM

Quote:

Originally Posted by lutusp (Post 3696541)
The solution depends on what you are trying to do. If you want to parse the source exactly as gcc does it, why bother -- gcc already exists. So it's safe to conclude that you want something else. But you don't say what you want, and there are all sorts of parsers and parser generators.

I'm trying to study how to build an OS from scratch. I get to know that the Linux kernel and Glibc both written in c. I'm in doubt about how c itself could do all these, without any assembly language involved. When I try to read the source code of glibc, I feel it is too complex, sounds like not possible for a single person to finish. So I come out an idea to write a parser to help making the analysis easier.

smeezekitty 09-26-2009 11:33 PM

Quote:

Originally Posted by famsinyi (Post 3698190)
I'm trying to study how to build an OS from scratch. I get to know that the Linux kernel and Glibc both written in c. I'm in doubt about how c itself could do all these, without any assembly language involved. When I try to read the source code of glibc, I feel it is too complex, sounds like not possible for a single person to finish. So I come out an idea to write a parser to help making the analysis easier.

you do not need to write a C compiler to write a kernel
p.s. do you happen to know anything about bootloading?

famsinyi 09-27-2009 09:26 AM

Quote:

Originally Posted by smeezekitty (Post 3698210)
you do not need to write a C compiler to write a kernel
p.s. do you happen to know anything about bootloading?

well, I read some of it. It should be ok. I'm using the book "Developing Your Own 32-BIT Operating System" as the guide. I wonder how the kernel can handle raw device input and output if no assembly language involved in the kernel code. For my book example, the kernel is written in assembly language.

PTrenholme 09-27-2009 10:56 AM

As a historical note, C is called C because it was written to add data types to B, which, in turn, was written to abstract programming in A from (most) machine dependencies. (The "A" language was/is "assembly.") Both B (and BCPL) were, I believe, just a set of assembly language macros, and, if I recall correctly, the first C compiler was also written as a set of PDP-8 assembly language macros.

Bottom line: Almost anything you can do in "assembly language" can be done in C, and, with some knowledge of how C is converted to machine code - for a specific machine - (almost) as well as a good assembly programmer.

If I were you, rather than trying to see how the compiler parses C, look at the examples in your book and try to write them in C. Then compile your C-code and compare the assembly output (gcc -S) with the book's code. Note that you'll need to set the -m options for the correct target to get anywhere, but it might help you understand how C can be used to create (portable) kernel code.

jlinkels 09-27-2009 01:13 PM

Continuing on the history lesson, the first version of Unix was written in assembly (for the PDP-11), but it was rewritten in 1973 in C. Even the C proved to be efficient enough to run on the existing hardware, and also powerful enough to replace assembly.

I know of some (real time) OS-es written in assembly, but those were fairly primitive kernels, not comparable with the capabilities of the kernels as we know them today. Even in those OS-es (I am talking wabout mid 1980-ies for 680x0 platform) device drivers were written in Pascal and later in C. In those days it was said that C was about 20% less efficient than assembly.

The point? Unless you are writing something for a 8008 processor, C might be your language of choice for the OS. If not for anything else, you productivity will be much higher while programming.

jlinkels

smeezekitty 09-27-2009 08:47 PM

i happen to be writing a kernel myself

famsinyi 10-16-2009 03:21 AM

Does anyone know where to get good c++, pl/sql, and c preprocessor grammar?

famsinyi 10-16-2009 05:11 AM

Ooops! The gnu c preprocessor cpp can be used to output source code that been processed by c preprocessor.

bigearsbilly 10-19-2009 05:42 PM

yes.
gcc -E

gcc -M
does make dependencies too!

smeezekitty 10-19-2009 06:47 PM

Quote:

Originally Posted by bigearsbilly (Post 3725391)
yes.
gcc -E

gcc -M
does make dependencies too!

did you read the original question?


All times are GMT -5. The time now is 09:13 AM.