how to start writing a very simple programming lan

2020-02-17 05:35发布

Recently, I was going around looking for ideas on what I can build using C this summer and I came across this post: Interesting project to learn C?

Implement a programming language. This doesn't have to be terribly hard - I did the language that must not be named - but it will force you to learn a lot of the important parts of C. If you don't want to write a lexer and/or parser yourself, you can use lex/flex and yacc/bison, but if you plan on that you might want to start with a somewhat smaller project.

I was kinda intrigued about the implementing a programming language answer and I'm wondering how do I go about starting this? I've gone through the whole K&R book and I've done some of the exercises as well. I also have a bit of experience in C++ and Java if that matters. Any tips? Thanks!

14条回答
做个烂人
2楼-- · 2020-02-17 05:58

I've made a simple language parser in Java some time ago, basically evaluated mathematical expressions, replaced constants and variables and provided some feedback on syntax/type errors.

The easiest way I found to do such a thing was to make a parse tree. This can be done easily by using two stacks, an operator stack and a result stack. Afterwards you could just parse it recursively using a DFS, maybe use the visitor pattern if you decide to implement this in a object oriented language.

There is a lot to say about these things and if you want to I can explain them more in-depth, I didn't because I thought you'd want to try implementing the above mentioned yourself, but if you do, just notify me and we can talk.

查看更多
甜甜的少女心
3楼-- · 2020-02-17 06:00

Another alternative is to build a language without looking at anything else. Figure out what you can do easily, and go from there. For example, you could parse expressions into a list of tokens, separating with whitespace, and use prefix notation (which is quite simple to deal with). This sort of thing is a huge amount of fun, and you can learn a lot from experimenting.

查看更多
相关推荐>>
4楼-- · 2020-02-17 06:01

Can I just say, I have seen many people asking questions like "How do I make a programming language?" or "How hard is it to make a programming language" and most of the answers just tell them that you have to go through years of university and read books that are 1000 pages long. I am here to tell everyone that you may post those answers, but it doesn't help them at all in their journey to make a programming language. I am 16 and have been doing programming for almost 2 years and I write programming languages. Quite advanced object orientated ones as well, but I haven't read any books, no have I done 8 years of university. To get people started, here's a simple programming language written in C#:

string code = "print Hello World";
foreach (string a in code.Split('\n'))
{
    if (a.StartsWith("print "))
    {
        Console.WriteLine(a.Substring(6));
    }
}

anyone who knows basic C# should be able to understand this. You can't start making programming languages without having some programming experience. Make sure you learn a programming language and make sure you know a lot about it, then just start writing simple little bits of code, like I've posted, and with experimentation and practice, you'll start writing some complex programming languages in no time :)

查看更多
smile是对你的礼貌
5楼-- · 2020-02-17 06:02

I'd start with a simple desk calculator program that can read things like:

5 + 10 * 3

and print the answer. Then you can progress it to add variables, control flow, even functions.

查看更多
一夜七次
6楼-- · 2020-02-17 06:02

Learn about regular expressions, grammars, and a good parser generator.

Even if you end up implementing your own parser, these are the fundamental concepts to implementing any programming language.

查看更多
forever°为你锁心
7楼-- · 2020-02-17 06:02

Well, I think something like that is really hard to do but also it would be a great pet project. You should have notions of parsers, lexers, flow control, paradigms (imperative, functional, OO) and many other things.

Many people says the Dragon Book is one of the best books for this. Maybe you can take a look at it :)

Good Luck!

查看更多
登录 后发表回答