When someone writes a new programming language, what do they write it IN?

前端 未结 13 969
再見小時候
再見小時候 2020-12-07 06:57

Please excuse my ignorance. I\'m dabbling in PHP and getting my feet wet browsing SO, and feel compelled to ask a question that I\'ve been wondering about for years:

相关标签:
13条回答
  • 2020-12-07 07:34

    Pretty much any language, though using one suited to working with graphs and other complex data structures will make many things easier. Production compilers are often written in C or C++ for performance reasons, but languages such as OCaml, SML, Prolog, and Lisp are arguably better for prototyping the language.

    There are also several "little languages" used in language design. Lex and yacc are used for specifying syntax and grammars, for example, and they compile to C. (There are ports for other languages, such as ocamllex / ocamlyacc, and many other similar tools.)

    As a special case, new Lisp dialects are often built on existing Lisp implementations, since they can piggyback on most of the same infrastructure. Writing a Scheme interpreter can be done in Scheme in under a page of code, at which point one can easily add new features.

    Fundamentally, compilers are just programs that read in something and translate it to something else - converting LaTeX source to DVI, converting C code to assembly and then to machine language, converting a grammar specification to C code for a parser, etc. Its designer specifies the structure of the source format (parsing), what those structures mean, how to simplify the data (optimizing), and the kind of output to generate. Interpreters read the source and execute it directly. (Interpreters are typically simpler to write, but much slower.)

    0 讨论(0)
  • 2020-12-07 07:34

    Many languages were first written in another available language and then reimplemented in itself and bootstrapped that way (or just kept the implementation in the foreign language, like PHP and perl), but some languages, like the first assembler was hand compiled to machine code like the first C-compiler was hand compiled to assembly.

    I've been interested in bootstrapping ever since I read about it. To learn more I tried doing it myself by writing my own superset of BF, which i called EBF, in itself. the first version of EBF had 3 extra primitives and I hand compiled the first binary. I found a two step rhythm when doing so. I implemented a feature in the current language in one release and had a sweet release where I rewrote the code to utilize the implemented feature. The language was expressive enough to be used to make a LISP interpreter.

    I have the hand compiled version together with the source in the first release tag and the code is quite small. The last version is 12 times bigger in size and the code and allows for more compact code so hand compiling the current version would be hard to get right.

    Edmund Grimley Evans did something similar with his HEX language

    One of the interesting things about doing this yourself is that you understand why some things are as they are. My code was product if small incremental adjustments an it looks more like it has evolved rather than been designed from scratch. I keep that in mind when reading code today which I think looks a little off.

    0 讨论(0)
  • 2020-12-07 07:37

    Even further binary ,or assembly operations must be translated into functions, thats the assemblers/compilers job, then into object,from data and functions, if you don't have a source file to see" how these objects functionality should be represented in your language implementation ,Then you have to recognize "see" implement, or define your own functions ,procedures, and data structures, Which requires a lot of knowledge, you need to ask yourself what is a function.Your mind then becomes the language simulation.This Separate a Master programmer from the rest.

    0 讨论(0)
  • 2020-12-07 07:40

    A lot of languages are bootstrapped- that is written in themselves. As to why you would want to do this, it is often a good idea to eat your own dogfood.

    The wikipedia article I refer to discusses the chicken and egg issue. I think you will find it quite interesting.

    0 讨论(0)
  • 2020-12-07 07:42

    I too had this question few months back. And I read few articles and watched some videos which helped me to start writing my own language called soft. Its not complete yet but I learned a lot of stuff from this journey.

    Basic things you should know is how compiler works when it has to execute a code snippet. Compiler has a lot of phases like lexical analysis, semantic analyzer, AST(Abstract Syntax Tree) etc.

    What I did in my new language can be found here - http://www.singhajit.com/writing-a-new-programming-language/

    If you are writing a language for first time then all the best and you have a long way to go.

    0 讨论(0)
  • 2020-12-07 07:46

    "Writing a new programming language" technically doesn't involve any code. It's just coming up with a specification for what your language looks like and how it works. Once you have an idea of what your language is like, you can write translators and interpreters to actually make your language "work".

    A translator inputs a program in one language and outputs an equivalent program in another language. An interpreter inputs a program in some language and runs it.

    For example, a C compiler typically translates C source code (the input language) to an assembly language program (the output language). The assembler then takes the assembly language program and produces machine language. Once you have your output, you don't need the translators to run your program. Since you now have a machine language program, the CPU acts as the interpreter.

    Many languages are implemented differently. For example, javac is a translator that converts Java source code to JVM bytecode. The JVM is an interpreter [1] that runs Java bytecode. After you run javac and get bytecode, you don't need javac anymore. However, whenever you want to run your program, you'll need the JVM.

    The fact that translators don't need to be kept around to run a program is what makes it possible to "bootstrap" your language without having it end up running "on top of" layers and layers of other languages.

    [1] Most JVMs do translation behind the scenes, but they're not really translators in that the interface to the JVM is not "input language -> output language".

    0 讨论(0)
提交回复
热议问题