[SOLVED] make use of gcc source code to parse c++ source file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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.
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.
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.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094
Rep:
Quote:
Originally Posted by famsinyi
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.