37

Possible Duplicates:
References Needed for Implementing an Interpreter in C/C++
How to create a language these days?
Learning to write a compiler

I know some c++, VERY good at php, pro at css html, okay at javascript. So I was thinking of how was c++ created I mean how can computer understand what codes mean? How can it read... so is it possible I can create my own language and how?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ramilol
  • 3,394
  • 11
  • 52
  • 84
  • 7
    Already a large selection of resources here: http://stackoverflow.com/questions/1669/learning-to-write-a-compiler –  Sep 07 '10 at 20:28
  • 1
    Forget text based 'language' and do something more domain specific, why not tell the computer what you want it to do graphically, through hand waves or something more natural, like facial recognition. http://www.ted.com/talks/peter_molyneux_demos_milo_the_virtual_boy.html – Greg Domjan Sep 07 '10 at 20:49
  • I wanted to put this as an answer, but since it got closed, I'll put it here: This is a book which has been used in many colleges as a comp sci 101 book. It takes you through the rudiments of designing computer hardware, assembly language, compilers, and high level languages. It won't make you an over-night expert at any of them but it makes them all very approachable. You'll amaze yourself with what you'll learn how to do. "The Elements of Computing Systems": http://www1.idc.ac.il/tecs/ – Dinah Sep 08 '10 at 00:43
  • 4
    @Yi Jiang: If you're not happy with that link, here are some more duplicates: http://stackoverflow.com/questions/294852/references-needed-for-implementing-an-interpreter-in-c-c, http://stackoverflow.com/questions/1550025/how-to-create-a-language-these-days, http://stackoverflow.com/questions/2413565/creating-a-language-interpreter, http://stackoverflow.com/questions/2853412/books-on-creating-interpreted-languages, http://stackoverflow.com/questions/2923287/what-is-the-process-of-creating-an-interpreted-language, etc. – gnovice Sep 08 '10 at 14:17
  • 1
    Don't rush into this. If you're "ok" with C++, learn the language. Sit down and do A LOT (I mean, a lot) of throwaway projects so you can understand how programming languages work. PHP is a nice precursor, but it ruins your sense of how programming works. PHP is nice; they do all the work for you and you never have to include things and use libraries, etc. C++, on the other hand, is not so nice, but is has much more capabilities (for whatever basis of comparison you have seeing as PHP is a web *interpreted* [that word again] language and C++ is platform first gen.). Good luck anyway! – RageD Nov 30 '10 at 04:09

13 Answers13

50

If you're interested in compiler design ("how can computer understand what codes mean"), I highly recommend Dragon Book. I used it while in college and went as far as to create programming language myself.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • 37
    I don't think I would recommend the dragon book to a 13-year-old. – Michael Myers Sep 07 '10 at 21:16
  • 23
    @mmyers, if he already knows some C++ and *very* good at php...he may have the skills and knowledge necessary to understand Dragon book. If nothing else it'll help to teach him what else he needs to know/learn *first*. – David Thomas Sep 07 '10 at 21:47
  • 7
    @mmyers Just because he's 13 doesn't make it easier to write a compiler. – Tyler Smith Sep 08 '10 at 00:39
38

"Every now and then I feel a temptation to design a programming language but then I just lie down until it goes away." — L. Peter Deutsch

EDIT (for those who crave context):

"[L. Peter Deutsch] also wrote the PDP-1 Lisp 1.5 implementation, Basic PDP-1 LISP, 'while still in short pants' between the age of 12-15 years old."

anthony
  • 40,424
  • 5
  • 55
  • 128
  • 1
    +1 for advocating common sense, and the avoidance of masochism... =) (**edited** to add link to a source for the quote: http://books.google.com/books?id=nneBa6-mWfgC&lpg=PA436&ots=gDzsJhOQ3A&dq=Every%20now%20and%20then%20I%20feel%20a%20temptation%20to%20design%20a%20programming%20language%20but%20then%20I%20just%20lie%20down%20until%20it%20goes%20away&pg=PA436#v=onepage&q=Every%20now%20and%20then%20I%20feel%20a%20temptation%20to%20design%20a%20programming%20language%20but%20then%20I%20just%20lie%20down%20until%20it%20goes%20away&f=false) – David Thomas Sep 07 '10 at 21:49
  • 2
    At age 13, I was debugging an assembler in Signetics 2650 assembly language. The project became easier after I learned Algebra. After the university Compiler Theory & Design course, I now fully understand it. – Thomas Matthews Sep 07 '10 at 22:45
  • Not a good answer, given the context of the OP. It's great to have created a simple version of most parts of a system yourself once. It's the best way to get a better idea of what is involved, and gives you better judgement of how your system might work internally, and thus when you hit weird bugs, there is a better likelihood of you knowing what could have caused it. Reinventing parts of your computer (as a learning exercise) makes you a better programmer. – uliwitness Apr 22 '17 at 09:24
20

If you want to understand how the computer understands the code, you might want to learn some assembly language. It's a much lower-level language and will give you a better feel for the kinds of simple instructions that really get executed. You should also be able to get a feel for how one implements higher level constructs like loops with only conditional jumps.

For an even lower-level understanding, you'll need to study up on electronics. Digital logic shows you how you can take electronic "gates" and implement a generic CPU that can understand the machine code generated from the assembly language code.

For really-low level stuff, you can study material science which can teach you how to actually make the gates work at an atomic level.

You sound like a resourceful person. You'll want to hunt down books and/or websites on these topics tailored to your level of understanding and that focus on what you're interested in the most. A fairly complete understanding of all of this comes with a BS degree in computer science or computer engineering, but many things are quite understandable to a motivated person in your position.

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
  • Neither an understanding of assembly nor electronics is necessary for writing a high-level language (although they can certainly help). For example, a lot of recently-created languages run in the JVM: http://en.wikipedia.org/wiki/List_of_JVM_languages – Frank Farmer Sep 07 '10 at 20:37
  • 3
    Although these are fine things to learn, except for the first none of these things seem necessary for writing a compiler. Even the first isn't needed if one uses LLVM or similar technology. – ergosys Sep 07 '10 at 20:38
  • 6
    'high-level' wasn't specified in the question – Greg Domjan Sep 07 '10 at 20:39
  • 1
    Plus he also said “ you might want to “. It’s not that he has. – Martin Marconcini Sep 07 '10 at 23:23
  • 4
    "Compiler" wasn't specified in the question. He said he wants to create a new language. When designing a new language, typically a machine-code compiler is not the first implementation. – Potatoswatter Sep 08 '10 at 01:26
  • 1
    I second the assembler part of this: That is as low as you need to go to have a good idea. It doesn't matter which assembler, though. You just need to know about the stack/heap, registers like the program counter, and typical instructions most assemblers have (conditionals, flow control, stuff involved in calling functions). Ideally, start with a teaching assembler, or a clean and simple one like Motorola 68000 assembler, if you can create a simple assembler program that recursively calls a function, uses conditionals and loops, and maybe makes one system call like printf(), you know enough. – uliwitness Apr 22 '17 at 09:27
11

Yes it's possible to create your own language. Take a look at compiler compilers. Or the source code to some scripting languages if you dare. Some useful tools are yacc and bison and lexx.

Others have mentioned the dragon book. We used a book that I think was called "compiler theory and practice" back in my university days.

It's not necessary to learn assembler to write a language. For example, Javascript runs in something called an interpreter which is an application that executes javascript files. In thise case, the interpreter is usually built into the browser.

The easiest starting program language might be to write a simple text based calculator. i.e. taking a text file, run through it and perform the calculations. You could write that in C++ very easily.

My first language for a college project was a language defined in BNF given to us. We then had to write a parser which parsed it into a tree structure in memory and then into something called 3 address code (which is assembler like). You could quite easily turn 3 address code into either real assembler or write an interpreter for that.

hookenz
  • 36,432
  • 45
  • 177
  • 286
  • 4
    +1 for starting with writing a calculator. I would however try to read something like the Dragon Book and use a compiler compiler such as Bison or ANTLR. – Andre Holzner Sep 07 '10 at 20:42
  • Agreed, writing a simple expression parser and interpreter is a good first step, but will probably not answer the OP's questions about how C++ works, how the computer runs code. Not sure I agree about the compiler compilers/parser generators though (in this particular case of learning how stuff works). It might be worth writing your own tokenizer and parser from hand first, just to understnd what those tools do for you. – uliwitness Apr 22 '17 at 09:32
8

Yup! It's definitely possible. Others have mentioned the Dragon Book, but there is also plenty of information online. llvm, for example, has a tutorial on implementing a programming language: http://llvm.org/docs/tutorial/

Niki Yoshiuchi
  • 16,883
  • 1
  • 35
  • 44
  • 3
    Ugh, I have professionally written several compilers for small domain-specific languages, and I recently did my first LLVM project and found the learning curve very very steep. Surely for a student it would be better to make a very tiny toy language and write their own simple recursive descent compiler and bytecode interpreter, rather than try to understand all the industrial-strength compiler concepts you need to make use of LLVM. – Larry Gritz Sep 08 '10 at 00:20
  • @Larry Gritz: depend if you are interested in writing the compiler or designing the language. If you contend yourself with generating llvm assembly I think it could be quite simple (once you have generated your AST), it doesn't mean you shouldn't read a good book though. – Matthieu M. Sep 08 '10 at 06:49
4

I really recommend Programming Language Pragmatics. It's a great book that takes you all the way from what a language is through how compilers work and creating your own. It's a bit more accessible than the Dragon Book and explains how things work before jumping in headfirst.

Rohan Singh
  • 20,497
  • 1
  • 41
  • 48
3

It is possible. You should learn about compilers and/or interpreters: what they are for and how they are made.

Bernard
  • 7,908
  • 2
  • 36
  • 33
3

Start learning ASM and reading up on how byte code works and you might have a chance :)

Marc Towler
  • 705
  • 11
  • 32
  • 3
    Most compilers are written in their own languages [ http://en.wikipedia.org/wiki/Bootstrapping_(compilers) ]. Similarly, the PHP interpreter is primarly written in C -- there's little, if any assembly involved. – Frank Farmer Sep 07 '10 at 20:34
  • 1
    Err, you can write compilers for any language in any other language. I mean for practice in college we created lisp using java, then java using lips. Of course it was very exact syntax and not extremely smart. – Parris Sep 07 '10 at 23:53
  • 1
    @Frank: @Parris: I don't think Marc's point was that the compiler should be written in assembly, but that it should compile to assembly. Or maybe he was just answering the part of the question that said "how can computer understand what codes mean?". – sepp2k Sep 08 '10 at 10:15
  • @sepp2k There are many compilers that don't "compile to assembly". javac, any interpreted language... – Frank Farmer Sep 08 '10 at 16:24
  • Assembler doesn't have to be the implementation language *nor* the output format. However, knowing assembler means you have a base model for the functionality a byte-code or interpreter will have to provide under the hood, particularly for implementing recursive function calls. That said, feel free to just muddle your way through. Creating a language of your own that uses a std::map to hold variables, then finding recursion doesn't work is a good learning experience. – uliwitness Apr 22 '17 at 09:34
3

If you know C -- it sounds like you do -- grab a used copy of this ancient book: http://www.amazon.com/Craft-Take-Charge-Programming-Book-Disk/dp/0078818826

In it there's a chapter where the author creates a "C" interpreter, in C. It's not academically serious like the Dragon book would be, but I remember it being pretty simple, very practical and easy to follow, and since you're just getting started, it would be an awesome introduction to the ideas of a "grammar" for languages, and "tokenizing" a program.

It would be a perfect place for you to start. Also, at $0.01 for a used copy, cheaper than the Dragon Book. ;)

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
3

Start with creating a parser. Read up on EBNF grammars. This will answer your question about how the computer can read code. This is a very advanced topic, so don't expect too much of yourself, but have fun. Some resources I've used for this are bison, flex, and PLY.

gtrak
  • 5,598
  • 4
  • 32
  • 41
3

Yes! Getting interested in compilers was my hook into professional CS (previously I'd been on a route to EE, and only formally switched sides in college), it's a great way to learn a TON about a wide range of computer science topics. You're a bit younger (I was in high school when I started fooling around with parsers and interpreters), but there's a whole lot more information at your fingertips these days.

Start small: Design the tiniest language you can possibly think of -- start with nothing more than a simple math calculator that allows variable assignment and substitution. When you get adventurous, try adding "if" or loops. Forget arcane tools like lex and yacc, try writing a simple recursive descent parser by hand, maybe convert to simple bytecodes and write an interpreter for it (avoid all the hard parts of understanding assembly for a particular machine, register allocation, etc.). You'll learn a tremendous amount just with this project.

Like others, I recommend the Dragon book (1986 edition, I don't like the new one, frankly).

I'll add that for your other projects, I recommending using C or C++, ditch PHP, not because I'm a language bigot, but just because I think that working through the difficulties in C/C++ will teach you a lot more about underlying machine architecture and compiler issues.

(Note: if you were a professional, the advice would be NOT to create a new language. That's almost never the right solution. But as a project for learning and exploration, it's fantastic.)

Larry Gritz
  • 13,331
  • 5
  • 42
  • 42
1

If you want a really general (but very well written) introduction to this topic -- computing fundamentals -- I highly recommend a book titled Code by Charles Petzold. He explains a number of topics you are interest and from there you can decide what you want to create yourself.

Ulysses
  • 11
  • 1
1

Check out this book, The Elements of Computing Systems: Building a Modern Computer from First Principles it takes you step by step through several aspects of designing a computer language, a compiler, a vm, the assembler, and the computer. I think this could help you answer some of your questions.

cirons42
  • 74
  • 1
  • 3