我怎么会去用C写一个跨preTER? [英] How would I go about writing an interpreter in C?

查看:111
本文介绍了我怎么会去用C写一个跨preTER?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很乐意提供一些参考,或提示,可能是一个电子书或两个。我不希望写一个编译器,只是为了寻找一个教程,我可以跟着和修改,我去。感谢您对理解!

I'd love some references, or tips, possibly an e-book or two. I'm not looking to write a compiler, just looking for a tutorial I could follow along and modify as I go. Thank you for being understanding!

BTW:它必须是C

BTW: It must be C.

更多的答复是AP preciated。

Any more replies would be appreciated.

推荐答案

一个伟大的方式开始编写一个跨preTER是写一个简单的机器模拟器。这里有一个简单的语言,你可以写一个跨preTER:

A great way to get started writing an interpreter is to write a simple machine simulator. Here's a simple language you can write an interpreter for:

该语言有栈和6指示:

推< NUM> #上推数堆栈

弹出#爆开的第一个数字在堆栈上

pop # pop off the first number on the stack

添加#弹出堆栈顶部2项,并推到堆栈的总和。 (请记住,您可以添加负数,所以你减法盖得)。您还可以得到相乘我使用一些其他的说明与这一个创建一个循环。

add # pop off the top 2 items on the stack and push their sum on to the stack. (remember you can add negative numbers, so you have subtraction covered too). You can also get multiplication my creating a loop using some of the other instructions with this one.

IFEQ<地址> #检查堆栈的顶部,如果是0,则继续,否则,跳转到<地址> ,其中<地址> 是一个行号

ifeq <address> # examine the top of the stack, if it's 0, continue, else, jump to <address> where <address> is a line number

跳&LT;地址&gt; #跳转到一个行号

打印#在堆栈的顶部打印值

print # print the value at the top of the stack

DUP #推堆栈顶部什么是副本回压入堆栈。

dup # push a copy of what's at the top of the stack back onto the stack.

一旦你已经写了一个程序,可以把这些指令并执行它们,你已经基本上建立了一个非常简单的基于堆栈的虚拟机。由于这是一个非常低的水平的语言,你不需要了解一个AST是什么,如何解析语法到AST,并将其转化为机器code,等等。这是一个教程项目太复杂了。开始这一点,一旦你创建这个小VM,你可以开始考虑如何才能一些常见的结构翻译成这台机器。例如你可能要想想你会如何转换为C if / else语句或while循环成这种语言。

Once you've written a program that can take these instructions and execute them, you've essentially created a very simple stack based virtual machine. Since this is a very low level language, you won't need to understand what an AST is, how to parse a grammar into an AST, and translate it to machine code, etc. That's too complicated for a tutorial project. Start with this, and once you've created this little VM, you can start thinking about how you can translate some common constructs into this machine. e.g. you might want to think about how you might translate a C if/else statement or while loop into this language.

编辑:

从下面的评论,这听起来像你需要用C多一点经验,才能解决这个任务。

From the comments below, it sounds like you need a bit more experience with C before you can tackle this task.

我的建议是,首先了解以下内容:

What I would suggest is to first learn about the following topics:


  • scanf函数,printf的,的putchar,getchar函数 - 基本的C IO功能

  • 结构 - 在C基本数据结构

  • 的malloc - 如何分配内存和堆栈内存和堆内存的区别

  • 链表 - 以及如何实现一个栈,那么也许二叉树(你需要
    了解结构和malloc的第一)

那么这将是很好的学习多一点关于string.h中图书馆以及
  - STRCMP,的strdup - 一对夫妇有用的字符串函数,这将是有益的。

Then it'll be good to learn a bit more about the string.h library as well - strcmp, strdup - a couple useful string functions that will be useful.

总之,C相比,Python有一个更高的学习曲线,只是因为它是一个低层次的语言,你必须管理你自己的内存,所以这是很好的第一尝试写之前,学习一下C一些基本的东西除preTER,即使你已经知道如何写一个Python编写的。

In short, C has a much higher learning curve compared to python, just because it's a lower level language and you have to manage your own memory, so it's good to learn a few basic things about C first before trying to write an interpreter, even if you already know how to write one in python.

这篇关于我怎么会去用C写一个跨preTER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆