Programming Thread

Started by the-pi-guy, Mar 13, 2016, 10:39 PM

previous topic - next topic

0 Members and 2 Guests are viewing this topic.

Go Down

the-pi-guy

One of my slides for class:

Scoping Issues

Same name can be declared multiple times if the declarations occur in different scopes.  


This is a valid Java program:
public class Test{
     int Test;
     public void Test(){
         double Test;
    }
}

This is allowed in C++:
int Test(){

double Test;

  {
  bool Test;
  }

}

darkknightkryta

I remember those grammars.  I learned about them in my formal langauges math course.

the-pi-guy

So in the next two weeks, I basically have to:
Finish a compiler, and finish a P2P file transfer program.  

All while keeping up with my other classes.  Ugh.

darkknightkryta

So in the next two weeks, I basically have to:
Finish a compiler, and finish a P2P file transfer program.  

All while keeping up with my other classes.  Ugh.
There's a reason I dropped compilers  8)

the-pi-guy

There's a reason I dropped compilers  8)
It's so awful.  

darkknightkryta

It's so awful.  
It's weird cause before the math teacher took over it wasn't bad.  You'd kind of brute force your compiler and just parse with if statements. That's how a programmer would go about it.  Once you add formal languages and the rest of the math it becomes a nightmare.

the-pi-guy

So I'm working on the peer to peer file transfer program.  

Struggled for a few hours, with some issue that a third peer wouldn't respond to the first one:
1-->2-->3
Wasn't sure if the message wasn't getting sent or if the message wasn't getting delivered.  

Decided to not worry about it for a little bit, worked on something else.  Tried it again, randomly it worked.  what.  

I'm tired.

the-pi-guy

The best language is lol code
HAI 1.2
CAN HAS STDIO?
PLZ OPEN FILE "LOLCATS.TXT"?
    AWSUM THX
        VISIBLE FILE
    O NOES
        INVISIBLE "ERROR!"
KTHXBYE

Legend

The best language is lol code
HAI 1.2
CAN HAS STDIO?
PLZ OPEN FILE "LOLCATS.TXT"?
    AWSUM THX
        VISIBLE FILE
    O NOES
        INVISIBLE "ERROR!"
KTHXBYE
It hurts

the-pi-guy

It hurts
I like it, because it's so stupid:

I didn't see example 3:
Code: [Select]

HAI 1.0
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
   UP VAR!!1
   VISIBLE VAR
   IZ VAR BIGGER THAN 10? KTHX
IM OUTTA YR LOOP
KTHXBYE


"IM OUTTA YR LOOP"   ....  wow.


I'd hate to use it though....  

darkknightkryta

The best language is lol code
HAI 1.2
CAN HAS STDIO?
PLZ OPEN FILE "LOLCATS.TXT"?
    AWSUM THX
        VISIBLE FILE
    O NOES
        INVISIBLE "ERROR!"
KTHXBYE
I cried when my students showed me this a few years ago.  Good ol' lolcat langauge.

the-pi-guy


the-pi-guy

Compilers are huge beasts. 

I can't wait to be done.

the-pi-guy

A compiler works in phases.  

There's the scanning(lexing) phase, the parsing phase, and then the code generation phase.  Technically you can split it up more.  The code generation phase is actually an intermediate phase, which gets followed by an optimization phase, which gets followed by another code generation phase.  That can get even more complicated than that.  

Lexing is done with a simple DFA.  
It's basically set up with 1000 if statements to match the input character by character.  
Read an i, that means it can be either:
-int
-identifier
-if
etc.  
Read an f, that means it can now be either a:
-if
-identifier

Read an = sign, take the input as an if.  But if you read a t, take the input as an identifier.  

Okay, it's probably usually closer to 100 if statements.  (Although it's not hard to imagine it blowing up even more.)

That's the scanning phase.  The output will be something like
if leftparen id equals id rightparen
etc, where each word is some meaningful thing in regards to the input.

Then that gets passed to the parser.  

The parser can be worked a few different ways.  
Some work really nicely with recursion.  Other's don't work as nice.

The way a language is set up is with a grammar.
Something like this:

Statement -> assign    
Statement -> print
assign -> id = value
print -> print(id)

Then the way the parser works, is that it'll basically check if the first thing is a print or an id.  Then it knows what should come next.  


The parser gives a tree for the code generation.  The bottom of the tree is whatever the first thing that needs to be done is.  

The tree is especially useful for statements like this:

x = x+5*y-3

It'll basically change it to something like:
sub(add(x,mul(5,y)),3)

Although it's not exactly obvious how that's a tree.  

Then the code generation uses that to make something like this:
li $t0, 5
lw $t1, y
mul $t1,$t1,$t0
etc.


Even a very simple 3 phase compiler, can quickly become a massive project.  

For the most part, they are not difficult to make.  

But even very simple ones are huge projects.  Very time consuming.

It does get a little tricky, need to set up some things to manage registers and memory locations.  But for the most part, they are time consuming more than difficult.  

Legend


Go Up