r/AskComputerScience • • Jul 11 '16

How do you program a programming language?

I.e. How do you get a computer to understand a language? How does it actually make the transition from that language into assembly language?

45 Upvotes

18 comments sorted by

7

u/PastyPilgrim Jul 12 '16

This is the kind of thing that takes years of study to become proficient at, so it's very difficult to explain in just a handful of words but I'll try.

The core of it is that you need to write a compiler. A compiler takes your code and all of its friendly syntax, and turns it into assembly (the simplest instructions understood by your computer). Then an assembler takes your assembly and turns it into machine code (0s and 1s). The assembler's job is super easy because there's usually a 1 to 1 correspondence between a line of assembly and a line of machine code (each line of assembly becomes precisely one line of machine code and vice versa (with the exception of pseudo instructions)).

So the hard part in writing a language is writing the compiler, but that's usually because your compiler does a ton of optimization to make what you write run especially fast on your specific configuration of hardware. It wouldn't be very difficult to write a simple compiler, it just wouldn't be very efficient.

Anyway, assembly itself is already a pretty capable language (in most implementations), you can do any and everything (by definition) in assembly. So the core of your compiler will just be recognizing the syntax you create and translating that into the equivalent assembly code, which is then translated into machine code for whatever machine is running the code.

For example, assembly doesn't really have variables. Instead, you have registers (lightning fast little memory cells that are used to perform mathematical operations) and memory (slow but plentiful long-term storage). You can't keep everything in registers because there aren't enough of them. So what you might do to get variables in your language is to use a lookup table. To do this, you'll create a table in memory that maps a variable name to a type (if desired) and the location in memory where the value is stored. This way, when I create a variable in my language, my compiler will translate that instantiation into the instructions needed to allocate memory for the variable's value and to store the metadata in the lookup table. Then, when I use the variable, my compiler will translate my syntax into the equivalent assembly instructions needed to read the table and then load the value into a register (so that I can do work on it).

That's a compiler in a nutshell. You come up with rules, grammar, and syntax for a language, then you write the necessary code for understanding those rules and translating them into the equivalent assembly code, which is then translated into machine code.

What's interesting, is that in the modern day you don't even need to write your compiler in assembly. Instead, most languages use a language like C to implement their compiler, which has its compiler(s) written in assembly. By using a well-developed language like C, you can exploit the heavy optimization that other people that really know hardware and assembly are doing, and take advantage of the fact that C is easier (and more portable) to write than assembly.

2

u/lepriccon22 Jul 12 '16

Awesome, thanks :)

2

u/lordvadr Jul 12 '16

We wrote a compiler for one of my CS classes in college. The language was called YAPL (yet another programming language).

First thing first, as other's have mentioned, a compiler translates from one language to another...typically assembly...but could be any other language. Our compiler compiled YAPL, which was a lot like Pascal, into C, which we then fed to the C compiler...which in turn was fed to the assembler. We actually wrote working programs in YAPL. For my final project, I wrote a functional--albeit VERY basic--web server.

With that said, it's quite a bit different for an interpreted language, but the biggest part for each is still the same. By far, the most complicated part of a compiler is the parser.

The parser is what reads a source code file and does whatever it's going to do with it. Entire bookshelves have been written on this subject, and PhD's given out on the matter, so parsing can be extremely complicated.

In a theoretical sense, higher level languages abstract common or more complicated tasks from the lower level languages. For example, to a CPU, variables don't have sizes or names, neither do functions, etc. On one hand, it greatly speeds up development because the code is far more understandable. On the other hand, certain tricks you can pull of in the lower-level languages (that can vastly improve performance) can be abstracted away. This trade-off is mostly considered acceptable. An extra $500 web server (or 100 for that matter) to handle some of the load is far less expensive than 10 extra $100,000 a year x86 assembly developers to develop, optimize, and debug lower level code.

So generally speaking, the parser looks for what are called tokens, which is why there are reserved words in languages. You can't name a variable int in C because int is a reserved word for a type. So when you name variable, you're simply telling the compiler "when I reference this name again, I'm talking about the same variable." The compiler knows an int is 4 bytes, so does the developer. When it makes it into assembly, it's just some 4 bytes somewhere in memory.

So the parser starts looking for keywords or symbols. When it sees int, the next thing it's going to expect is a label, and if that label is followed by (, it knows it's a function, if it's followed by ; it's a variable--it's more complicated than this but you get the idea.

The parser builds a big structure in memory of what's what and essentially the functionality. From there, either the interpreter goes through and interprets the language, or for a compiler, that gets handed to what's called the emitter. The emitter is the function that spits out the assembly (or whatever other language) equivalent a = b + c; happens to be.

This is complicated, but if you take it in steps, it's not really that hard. This is the book we used. There's a much newer version out now. If I can find my copy, I'll give it to you if you pay shipping. PM me.

5

u/[deleted] Jul 12 '16

Instead, most languages use a language like C to implement their compiler, which has its compiler(s) written in assembly.

GCC and LLVM, at least, are written primarily in C++. I'm not aware of any major compilers written only in assembly (but I admit to not having much in-depth knowledge of various compilers). Compilers that are written in the same language they compile are known as self-hosting.

3

u/anamorphism Jul 11 '16

you write a compiler or interpreter.

1

u/lepriccon22 Jul 11 '16

What does that mean practically? How do I teach a computer to understand what naming a variable is? How do I teach it to understand what a for loop is, etc?

5

u/anamorphism Jul 11 '16 edited Jul 11 '16

you're not teaching the computer anything.

you're basically just writing a translation program.

you would write a program that takes in for (int i = 0; i < 10; ++i); and outputs assembly to represent what you want that line of code to do.

something like

  • allocate 32 bits of memory for i

  • write 0 to all of those bits

  • allocate 32 bits for 10

  • write 10 to those bits

  • declare label 1

  • compare i to 10

  • jump to label 2 if i is less than 10

  • add 1 to i

  • jump to label 1

  • declare label 2

1

u/lepriccon22 Jul 12 '16

allocate 32 bits of memory for i write 0 to all of those bits allocate 32 bits for 10 write 10 to those bits declare label 1 compare i to 10 jump to label 2 if i is less than 10 add 1 to i jump to label 1 declare label 2

How does it know how to understand this, though?

1

u/juuular Jul 12 '16

At the end of the day these simple statements like these (move bit A to spot B, etc) boil down to hardware instructions (aka just 1's and 0's). At the very bottom is the circuit board - different electronic parts that interact through electricity.

The most important part is the transistor - you can apply some voltage to "set" it to 1 of 2 states, and you can also apply some voltage differently to get which state it is in. That "move bit" command would be converted to an electrical signal to a transistor that makes it output it's state, and then route that signal to a different transistor that gets set.

This is massively oversimplified of course, but at the end of the day these instructions are converted into electrical signals being sent from one place to another and changing the state of different electrical devices.

Theoretically you could implement a CPU (and "compile" assembly instructions) by having slaves run around shuffling notecards with different numbers on them, just like we shuffle numbers around on silicon transistors. That would be much slower and more memory inefficient, though.

6

u/[deleted] Jul 12 '16 edited Jun 18 '23

[removed] — view removed comment

2

u/anamorphism Jul 12 '16

sure, but you don't try and teach someone who doesn't know algebra multivariable calculus.

2

u/giggly_kisses Jul 12 '16

That talk with Anders Hejlsberg was very interesting. Thanks for sharing!

1

u/CQQL Jul 11 '16

The book Structure and Interpretation of Computer Programs is a great introduction into that topic.

16

u/simonorono Jul 11 '16

First, you define your language. Define its syntax, its semantics, everything.

Then, it comes the hardest part: developing a compiler/interpreter for it.

A compiler is just a translator, it takes source code from the language you defined and pours the equivalent program in another programming language (generally, with a lower level).

For example, the C compilers, read C code and translate them to assembly, them an assembler converts this assembly code into an executable.

3

u/trenchgun Jul 12 '16

So C-compiler is written in assembly? And assembler is written in machine code?

12

u/Teslatronic Jul 12 '16

The first compilers/assemblers were. But once you write one C compiler in assembly, you can write another one in C itself, and use the assembly-based compiler to compile the C-based one, and then compile each new version of the compiler with the previous one. This is called bootstrapping.

Edit: see /u/bhrgunatha's comment for more detail.