创建我自己的编程语言[duplicate]

时间:2022-10-20 13:57:44

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

可能的重复:在C/ c++中实现解释器所需的引用,这些天如何创建语言?学习编写编译器

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++,非常擅长php, css html, javascript。我在想c++是如何创建的我的意思是,计算机怎么能理解代码是什么意思?它怎么读…那么我是否有可能创造出自己的语言?

13 个解决方案

#1


47  

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.

如果您对编译器设计感兴趣(“计算机如何理解代码的含义”),我强烈推荐《龙之书》。我在大学的时候就用过它,甚至自己创造了编程语言。

#2


37  

"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

“我时不时地感到设计一种编程语言的诱惑,但我只是躺下来,直到它消失。”彼得- l . 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."

“(L。Peter Deutsch还编写了PDP-1 Lisp的实现,基本的PDP-1 Lisp,在12-15岁的时候还穿着短裤。

#3


19  

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.

为了更低层次的理解,你需要深入学习电子学。数字逻辑向您展示了如何使用电子“门”并实现一个通用CPU,该CPU可以理解从汇编语言代码生成的机器码。

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.

你听起来像个足智多谋的人。你会想要搜索关于这些主题的书籍和/或网站,这些主题是根据你的理解程度和你最感兴趣的。如果你有计算机科学或计算机工程的学士学位,你就能完全理解这一切,但是对于你这个位置上有进取心的人来说,很多事情都是可以理解的。

#4


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.

是的,创造自己的语言是可能的。看看编译器编译器。或者是一些脚本语言的源代码。一些有用的工具是yacc, bison和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.

没有必要学习汇编程序来编写语言。例如,Javascript运行在所谓的解释器中,解释器是一个执行Javascript文件的应用程序。在这种情况下,解释器通常内置在浏览器中。

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.

最简单的启动程序语言可能是编写一个简单的基于文本的计算器。例如,获取一个文本文件,运行它并执行计算。你可以很容易地用c++写出来。

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.

我的大学项目的第一语言是在BNF中定义的一种语言。然后,我们必须编写一个解析器,将其解析为内存中的树结构,然后再解析为3个地址代码(类似汇编程序)。您可以很容易地将3个地址代码转换为实际的汇编程序或为其编写解释器。

#5


7  

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/

是的!这绝对是可能的。还有人提到过《龙书》,但网上也有很多信息。例如,llvm有一个关于实现编程语言的教程:http://llvm.org/docs/tutorial/

#6


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.

我非常推荐编程语言语用学。这是一本很棒的书,通过编译器的工作方式和创建你自己的语言,你可以从中学到语言。它比《龙之书》更容易理解,它解释了事情是如何运作的。

#7


3  

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

这是可能的。您应该了解编译器和/或解释器:它们的用途和制作方法。

#8


3  

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

开始学习ASM,学习字节码的工作原理,你可能有机会:)

#9


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

如果你知道C——听起来就像你一样——拿起一本旧书的旧拷贝:http://www.amazon.com/craft -- book - 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.

在书中有一章,作者创造了一个“C”翻译,在C。这不是学术严重喜欢龙书,但我记得是很简单,很实用,很容易,因为你刚刚开始,这将是一个很棒的思想概论语言的“语法”,和“分”程序。

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

对你来说,这将是一个完美的起点。而且,一本旧书的价格是0.01美元,比《龙之书》便宜。,)

#10


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.

首先创建一个解析器。阅读EBNF语法。这将回答你关于计算机如何读取代码的问题。这是一个非常高级的话题,所以不要对自己期望太高,但是要有乐趣。我使用过的一些资源是野牛、flex和PLY。

#11


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.

是的!对编译器感兴趣是我进入专业计算机科学的原因(以前我在去EE的路上,在大学里只是正式地转换了方向),这是了解大量计算机科学主题的好方法。你比我小一点(我上高中的时候就开始和解析员和口译员鬼混了),但是现在你的指尖上有更多的信息。

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.

从小做起:设计你能想到的最微小的语言——从一个简单的数学计算器开始,它允许变量赋值和替换。当你喜欢冒险的时候,尝试添加“如果”或循环。忘记像lex和yacc这样晦涩难懂的工具,尝试手工编写一个简单的递归下降解析器,或者转换为简单的字节码,并为它编写一个解释器(避免理解特定机器的汇编、寄存器分配等的所有困难部分)。在这个项目中你会学到很多东西。

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

和其他人一样,我推荐《龙书》(1986年版,我不喜欢新版本)。

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.

我将在您的其他项目中添加这一点,我建议您使用C或c++,抛弃PHP,不是因为我是一个语言偏见者,而是因为我认为解决c++中的困难将教会您更多关于底层机器架构和编译器问题的知识。

(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.)

(注:如果你是专业人士,建议你不要创造一门新的语言。这几乎从来都不是正确的解决方案。但作为一个学习和探索的项目,这是非常棒的。

#12


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.

如果你想要对这个主题——计算基础——有一个非常全面(但写得非常好)的介绍,我强烈推荐查尔斯·佩佐德(Charles Petzold)的《代码》(Code)一书。他解释了许多你感兴趣的话题,从这些话题中你可以决定自己想要创造什么。

#13


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.

看看这本书,计算系统的元素:从最初的原则构建一个现代计算机,它让你一步步地从设计计算机语言、编译器、虚拟机、汇编程序和计算机的几个方面着手。我认为这可以帮助你回答你的一些问题。

#1


47  

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.

如果您对编译器设计感兴趣(“计算机如何理解代码的含义”),我强烈推荐《龙之书》。我在大学的时候就用过它,甚至自己创造了编程语言。

#2


37  

"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

“我时不时地感到设计一种编程语言的诱惑,但我只是躺下来,直到它消失。”彼得- l . 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."

“(L。Peter Deutsch还编写了PDP-1 Lisp的实现,基本的PDP-1 Lisp,在12-15岁的时候还穿着短裤。

#3


19  

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.

为了更低层次的理解,你需要深入学习电子学。数字逻辑向您展示了如何使用电子“门”并实现一个通用CPU,该CPU可以理解从汇编语言代码生成的机器码。

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.

你听起来像个足智多谋的人。你会想要搜索关于这些主题的书籍和/或网站,这些主题是根据你的理解程度和你最感兴趣的。如果你有计算机科学或计算机工程的学士学位,你就能完全理解这一切,但是对于你这个位置上有进取心的人来说,很多事情都是可以理解的。

#4


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.

是的,创造自己的语言是可能的。看看编译器编译器。或者是一些脚本语言的源代码。一些有用的工具是yacc, bison和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.

没有必要学习汇编程序来编写语言。例如,Javascript运行在所谓的解释器中,解释器是一个执行Javascript文件的应用程序。在这种情况下,解释器通常内置在浏览器中。

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.

最简单的启动程序语言可能是编写一个简单的基于文本的计算器。例如,获取一个文本文件,运行它并执行计算。你可以很容易地用c++写出来。

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.

我的大学项目的第一语言是在BNF中定义的一种语言。然后,我们必须编写一个解析器,将其解析为内存中的树结构,然后再解析为3个地址代码(类似汇编程序)。您可以很容易地将3个地址代码转换为实际的汇编程序或为其编写解释器。

#5


7  

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/

是的!这绝对是可能的。还有人提到过《龙书》,但网上也有很多信息。例如,llvm有一个关于实现编程语言的教程:http://llvm.org/docs/tutorial/

#6


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.

我非常推荐编程语言语用学。这是一本很棒的书,通过编译器的工作方式和创建你自己的语言,你可以从中学到语言。它比《龙之书》更容易理解,它解释了事情是如何运作的。

#7


3  

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

这是可能的。您应该了解编译器和/或解释器:它们的用途和制作方法。

#8


3  

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

开始学习ASM,学习字节码的工作原理,你可能有机会:)

#9


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

如果你知道C——听起来就像你一样——拿起一本旧书的旧拷贝:http://www.amazon.com/craft -- book - 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.

在书中有一章,作者创造了一个“C”翻译,在C。这不是学术严重喜欢龙书,但我记得是很简单,很实用,很容易,因为你刚刚开始,这将是一个很棒的思想概论语言的“语法”,和“分”程序。

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

对你来说,这将是一个完美的起点。而且,一本旧书的价格是0.01美元,比《龙之书》便宜。,)

#10


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.

首先创建一个解析器。阅读EBNF语法。这将回答你关于计算机如何读取代码的问题。这是一个非常高级的话题,所以不要对自己期望太高,但是要有乐趣。我使用过的一些资源是野牛、flex和PLY。

#11


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.

是的!对编译器感兴趣是我进入专业计算机科学的原因(以前我在去EE的路上,在大学里只是正式地转换了方向),这是了解大量计算机科学主题的好方法。你比我小一点(我上高中的时候就开始和解析员和口译员鬼混了),但是现在你的指尖上有更多的信息。

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.

从小做起:设计你能想到的最微小的语言——从一个简单的数学计算器开始,它允许变量赋值和替换。当你喜欢冒险的时候,尝试添加“如果”或循环。忘记像lex和yacc这样晦涩难懂的工具,尝试手工编写一个简单的递归下降解析器,或者转换为简单的字节码,并为它编写一个解释器(避免理解特定机器的汇编、寄存器分配等的所有困难部分)。在这个项目中你会学到很多东西。

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

和其他人一样,我推荐《龙书》(1986年版,我不喜欢新版本)。

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.

我将在您的其他项目中添加这一点,我建议您使用C或c++,抛弃PHP,不是因为我是一个语言偏见者,而是因为我认为解决c++中的困难将教会您更多关于底层机器架构和编译器问题的知识。

(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.)

(注:如果你是专业人士,建议你不要创造一门新的语言。这几乎从来都不是正确的解决方案。但作为一个学习和探索的项目,这是非常棒的。

#12


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.

如果你想要对这个主题——计算基础——有一个非常全面(但写得非常好)的介绍,我强烈推荐查尔斯·佩佐德(Charles Petzold)的《代码》(Code)一书。他解释了许多你感兴趣的话题,从这些话题中你可以决定自己想要创造什么。

#13


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.

看看这本书,计算系统的元素:从最初的原则构建一个现代计算机,它让你一步步地从设计计算机语言、编译器、虚拟机、汇编程序和计算机的几个方面着手。我认为这可以帮助你回答你的一些问题。