Posted By: Shining Arcanine | Nov 12th, 2008 @ 12:14 PM
page 1 of 1
Comments: 10 | Views: 727
I am taking a data structures class which is taught 73% in Java and 27% in C. Right now, we are now doing C and the examples of C code on the lecture slides are is what I consider to be Java-style C code in that it is very verbose. C is a programming language in which I am very comfortable in programming, but C is also my favorite programming language and each time I see Java-style C code, it feels like a piece of me dies inside, so to resolve this, I would like to write a program that will do the following things in the following order:
  1. Read a C source file from the file system
  2. Verify it has no syntax errors (if it does, it will stop here)
  3. Generate a parse tree containing a description of the program
  4. Do manipulation to the parse tree to apply optimizations to the code
  5. Output a neatly formatted C source file to the file system
After I have written this program, I will give it to my professor in the hope that he uses it to fix his lecture slides. I know all of the optimizations that I want the program to do and I could list them, but I have a more fundamental problem.

I have a copy of the dragon book and I have read its first few chapters, but it but it is very abstract in its descriptions. Consequently, its examples only show how portions of things are done, such as mathematical expressions. It would take several months for me to derive how all of this would be applied to something as complicated as the C language and to write a working program that does what I want, so I am wondering, is there any sort of shortcut I can take?

More specifically, is there any sort of idiot's guide for specialized tools compiler writers use like LEX and YACC that would enable me to quickly get code that will produce a parse tree so I can do the manipulations I want and then output the tree's information to a text file without having to concern myself with the algorithms behind how the parse tree was generated in the first place?
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Google around, you can find the source of lex/yacc (or preferably flex/bison) parsers for C. For C++ it would be harder because the C++ grammar is not LALR(1) so cannot be parsed with yacc or bison generated parsers.

I for one would like to see an example of what you think is "Java-style C code" and what improvements you want to make, because I have a feeling I'm going to disagree with you. Wink
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
Let me be blunt here - if I were the professor, I'd give you a smack upside the head and tell you to first mind your own business, and second that part of a programmer's job is to read all sorts of source code in all sorts of different formats. Write one way, read in any way. Going to him and telling him to "fix" his code is like going to someone and telling them how to dress, what to eat and how to behave. It's obnoxious.
TommyCarlier
TommyCarlier
I want my scalps!
I once pointed out a bug in the code of one of my professors, during class, in front of everybody. I'm not saying I was wrong, but I never did that again. I also never went to her class again.
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
A good professor will tolerate that sort of thing (probably not what Shining Arcanine is doing, but pointing out a bug shouldn't be a problem).

I remember having a discussion with my Networks teacher about ISDN. I thought ISDN used a real digital signal, while he thought it uses a modulated analog signal like ADSL. I was right. I also remember a discussion with another professor on what of IETF stands for, where I was right again (I'm nearly always right with acronyms and abbreviations, I have an uncanny ability to remember them). In both cases the professor didn't mind being corrected. Smiley
TommyCarlier
TommyCarlier
I want my scalps!

You're absolutely right. But we don't live in lollypop-land where all professors are good Wink

I was (and always am) polite and careful in my words, but her answer was to start yelling stuff like "you think you know better than me?" and "if you're so good, maybe you don't need to learn this anymore". And she was right. So I never went to her class anymore.

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
True, I've had fewer good professors than I'd like. I'd like to know what lollypop land you live in where they have female professors in CompSci. Tongue Out

Actually, I had one female teacher, she taught Algorithms and her last name meant "graph" (Graaf). And my data structures teacher's last name meant "tree" (well, "tall tree" (Hogeboom) but close enough). I always thought that was funny. Smiley
hmmm,  so you were taught by Mr. Tree and Mr. Graph? nice Tongue Out

As for correcting my professors, I've always done that, always! Even if they weren't that happy for doing it Smiley

I don't remember correcting professors during courses but I know I "corrected" a question that was part of my final exam (graduation or whatever it is called). I don't know if someone was unhappy about that but the question was removed after all.

However, complaining about your teacher's coding style is a rather childish behavior. Is that code semantically correct? If so you have nothing to complain about, no teacher I know would waste his/her time to "fix" the slides. And I'm really curious, how does this Java-style C code looks anyway? Smiley

And to partially answer your question: YACC does not produce "parse trees". All it does is to call your custom written code depending on what grammar rules match the input token stream. You'll have to define your own tree data structures and write that custome code to build the tree.

Heh, I corrected the instructor at the Visual Studio 2005 course that our company sent a few of us to.  He honestly did not know what polymorphism was.  I took him aside at a break and explained it to him so I wouldn't make him look bad in front of the class.  But when we came back he never corrected what he had said earlier.
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
Correcting instructors when they're wrong is perfectly fine, though a bit smart-alecy.
"Correcting" instructors because their coding style is "wrong", i.e. different than yours, is downright obnoxious. Not to mention narrow-minded.
page 1 of 1
Comments: 10 | Views: 727
Microsoft Communities