LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-25-2009, 02:11 AM   #1
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Rep: Reputation: 0
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?
 
Old 09-25-2009, 03:21 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
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
 
1 members found this post helpful.
Old 09-25-2009, 04:41 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by famsinyi View Post
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.
 
Old 09-25-2009, 11:29 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,542

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
GCC-XML could be useful.
 
Old 09-25-2009, 12:05 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
1 members found this post helpful.
Old 09-26-2009, 11:41 PM   #6
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
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.
 
Old 09-27-2009, 12:33 AM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by famsinyi View Post
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?
 
Old 09-27-2009, 10:26 AM   #8
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smeezekitty View Post
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.
 
Old 09-27-2009, 11:56 AM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
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.
 
Old 09-27-2009, 02:13 PM   #10
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,241

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 09-27-2009, 09:47 PM   #11
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
i happen to be writing a kernel myself
 
Old 10-16-2009, 04:21 AM   #12
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
Does anyone know where to get good c++, pl/sql, and c preprocessor grammar?
 
Old 10-16-2009, 06:11 AM   #13
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
Ooops! The gnu c preprocessor cpp can be used to output source code that been processed by c preprocessor.
 
Old 10-19-2009, 06:42 PM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
yes.
gcc -E

gcc -M
does make dependencies too!
 
1 members found this post helpful.
Old 10-19-2009, 07:47 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by bigearsbilly View Post
yes.
gcc -E

gcc -M
does make dependencies too!
did you read the original question?
 
  


Reply

Tags
gcc, sourcecode


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to 'make' a source code? Adh Iman Linux - Newbie 6 05-24-2009 11:58 AM
gcc 4.1 vs 4.3 source code compatibility mistretzu Programming 1 07-04-2008 04:27 PM
about `make` and debug source code naihe2010 Programming 9 12-13-2005 06:03 AM
C source file Parse error before 38 exvor Programming 5 09-19-2005 03:10 PM
Parse error returned when compiling a source file with Kdevelop spilios Programming 1 09-09-2003 07:00 AM


All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration