Ask a Question, Get Deep!

If you want to publicly ask a specific member a question, do so here.
User avatar
Chozon1
Site Admin
Site Admin
Posts: 22806
Joined: Sun Dec 10, 2006 12:00 am
Location: In the shadows. Waiting for an oppurtune moment to create a dramatic entrance.
Contact:
Mmm...sometime, I'd like you to sit me on your knee and explain how compilers were created. It's sort of dark magic to me how they were invented, since I can't imagine how to code something when you have no way to translate it into machine language. Are you needing to fabricate one from scratch or sommat?

But yeah, finals. I have three on one day, I believe. I know I have at least two.

Next week is fun times?
Image
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
Heh. Compilers....they are complicated. I'll spoiler a general overview for ya. :P
Spoiler:
So there's a few major portions to a compiler: A scanner/tokenizer, a parser, a type checker, and a code generator.

The scanner/tokenizer essentially takes code and attempts to break it into meaningful chunks (called tokens). For example, a program that was "print("hello "+name);" would be tokenized like so:
token: print
token: (
token: string. value: "hello "
token: +
token: identifier. value: <Whatever we set it to>
token: )
token: ;

This is to simplify parsing, which is where the meaning of the tokens is determined. A parser would grab tokens to make sure that a given expression or statement makes sense. With the above example, it would grab "print", which it knows needs to be followed by an opening parenthesis, followed by a string expression (Not just a string, for reasons that shall be explained later), followed by a closing parenthesis. So if instead of an opening parenthesis, it got a string, it would return an error.

Different parsers do different things with this "output" from the parser. Some (Like the one we're building in class) generate a structure referred to as an Abstract Syntax Tree, or AST. In this tree structure, nodes are different types of token combinations (Example: a + b would be considered a binary expression because it contains an operator which takes two operands, while neg a [convert negative number to positive, and vice versa] is a unary expression as it has an operator which takes one operand). This structure makes the following processes easier.

Type checking uses this generated AST to make sure that the program uses proper types for things (For example: Having a function which returns a boolean value [true/false] return a number like 42 is bad). This is usually done by referencing a "Symbol Table" which is generated during scanning to accommodate for the complexity of interacting variables.

Now that we know that the code actually is valid (According to compiler rules), we have to generate machine code. This can be done in a few ways. Either generate assembly code and assemble it, or directly write the code an assembler would generate. This is where things get super hard. Dealing directly with a CPU is a horribly dangerous operation, and can cause some problems. Incorrectly dealing with the stack or the registers can lead to, as my professor calls it, 'badness'. :P

Once all that is done, you should have an executable file that can now be run on the target system.
As for next week....goodness no. I have two projects due on Thursday. :/
User avatar
baconisgood23
VIP Member
VIP Member
Posts: 793
Joined: Tue Oct 02, 2012 8:23 pm
Are you human?: Yes!
Location: In the mystical magical Bacon Dimension.
Contact:
Do you like chinese food?
Image

"I always wanted to be somebody, but now I realize I should've been more specific." -Lily Tomlin
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
Oh, I love it. I eat way too much of it though...curse the little Chinese Shop down the street with their cheap lunch deals. :P
User avatar
Chozon1
Site Admin
Site Admin
Posts: 22806
Joined: Sun Dec 10, 2006 12:00 am
Location: In the shadows. Waiting for an oppurtune moment to create a dramatic entrance.
Contact:
Interesting...I do remember reading something about modern CPU's using a buffer of sorts between the programs and the hardware.

Point is, do you feel like you actually need to know how to write a compiler, or just use premade ones?

Mostly just curious. To me, it seems like making paint yourself.
Image
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
The point of the class is twofold: Learn compiler theory, and gain practical experience with the elements of a compiler.

Theory is a good thing because the theory of compiler design can often let you design programs more efficiently, and theory is a blast. :P

Practical experience lets you see just what is going on with all this theory that you learn. It's a good experience, if not difficult.


The whole point of a compilers course is this: Learning how compilers operate will help you become a better programmer. I've learned enough about how compiler theory can be applied to write a book, and I only took an undergraduate level course in it.


Though for most programmers, simply knowing how to use existing compilers will suffice. As an inquisitive Computer Science student though, I want more than cursory knowledge. :P
User avatar
Chozon1
Site Admin
Site Admin
Posts: 22806
Joined: Sun Dec 10, 2006 12:00 am
Location: In the shadows. Waiting for an oppurtune moment to create a dramatic entrance.
Contact:
You give me a headache sometimes. Stop breaking the curve. XD

Trains?
Image
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
Hey, it's what I do best. :P

I like trains.

User avatar
Orodrist
CCGR addict
Posts: 7831
Joined: Mon Nov 03, 2008 6:38 pm
Location: Surrounded by blood and bathed in fire on a frozen lake
Contact:


?

I can't decide if that's a boy band pretending to be metal or a really good song.
I am free, no matter what rules surround me. If I find them tolerable, I tolerate them; if I find them too obnoxious, I break them. I am free because I know that I alone am morally responsible for everything I do - Robert A Heinlein

Courage ~ Discipline ~ Fidelity ~ Honor ~ Hospitality ~ Industriousness ~ Perseverance ~ Self Reliance ~
User avatar
Chozon1
Site Admin
Site Admin
Posts: 22806
Joined: Sun Dec 10, 2006 12:00 am
Location: In the shadows. Waiting for an oppurtune moment to create a dramatic entrance.
Contact:
August Burns Red?
Image
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
Orodrist wrote:I can't decide if that's a boy band pretending to be metal or a really good song.
That was really good. It seems like some of the lighter metal as far as Progressive Acts go, but I don't think that's a bad thing.
Chozon1 wrote:August Burns Red?
I hate to say it, but I've not actually listened to them. I know the story behind their name though, and it's kinda sad. :/
User avatar
Orodrist
CCGR addict
Posts: 7831
Joined: Mon Nov 03, 2008 6:38 pm
Location: Surrounded by blood and bathed in fire on a frozen lake
Contact:


?
I am free, no matter what rules surround me. If I find them tolerable, I tolerate them; if I find them too obnoxious, I break them. I am free because I know that I alone am morally responsible for everything I do - Robert A Heinlein

Courage ~ Discipline ~ Fidelity ~ Honor ~ Hospitality ~ Industriousness ~ Perseverance ~ Self Reliance ~
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
That's got a nice riff to it. I like it. Only have one song by The 69 Eyes from a sampler collection, and I like it.
User avatar
Orodrist
CCGR addict
Posts: 7831
Joined: Mon Nov 03, 2008 6:38 pm
Location: Surrounded by blood and bathed in fire on a frozen lake
Contact:


?

Who's idea was that?
I am free, no matter what rules surround me. If I find them tolerable, I tolerate them; if I find them too obnoxious, I break them. I am free because I know that I alone am morally responsible for everything I do - Robert A Heinlein

Courage ~ Discipline ~ Fidelity ~ Honor ~ Hospitality ~ Industriousness ~ Perseverance ~ Self Reliance ~
User avatar
Deepfreeze32
Site Admin
Site Admin
Posts: 7041
Joined: Sun Oct 01, 2006 12:00 am
Are you human?: Yes!
Location: On the run from Johnny Law; ain't no trip to Cleveland
Contact:
That was a pretty decent cover. I think I like the original better, but it's Soundgarden. :P

I am a little confused as to why he chose to cover a Soundgarden song (Let alone that one) though...
Post Reply