如何在C#中构建一个简单的编译器?

如何在C#中构建一个简单的编译器?

问题描述:

大家好
我需要一点帮助
我得到了具有简单规则的简单源语言
程序的一般外观

编程my-prog; //任何程序都以关键字program
开头
var//只需一次即可开始声明变量

x:datatype//此部分用于初始变量

X,y:datatype//用于两个变量

开始//现在我们开始执行程序了

.
.
.
.
结束

说明

1-如果(条件)则

陈述//仅一条指令

2- if(条件)

开始
statment1;
statment2;
结束

其他

开始
.
.
.
结束//如果有多条指令

3- while(条件)做​​
开始
.
.
.
结束

4- for counter = val1至val2做
开始
.
.
.
结束

5- var_name:= exp;

6-读(x); //读取x是一个变量
7- readln(x);
8- write(exp);
9- writeln(exp);

该语言有一个现成的功能,例如:

sin,cos,tan,tan,square,sqrt,max,min.

数学上的东西:

+,+ =,-,-=,*,* =,/,/=

比较事物:

>,<,< =,> =,=,!=


第一步是词法分析,它读取字符流并返回标记

然后进行语义分析

如果有人可以帮助,那么它会很酷,否则,谢谢您.

Hello everyone
I need a little help here
i got a simple source language with simple rules
the general look of the program

Program my-prog; // any program start with the keyword program

var // just one time to start stating the variables

x:datatype // this part is for intial variables

X,y:datatype // for two varibles

Begin // now we start the dosy of the program

.
.
.
.
End

the Instructions

1- if(condition) then

statment; // one instruction only

2- if(condition)

Begin
statment1;
statment2;
End

else

Begin
.
.
.
End // in case of more than one instruction

3- while(condition) do
Begin
.
.
.
End

4- for counter=val1 to val2 do
Begin
.
.
.
End

5- var_name := exp;

6- read(x); // for reading x is a variable
7- readln(x);
8- write(exp);
9- writeln(exp);

there is a ready functions in this language such as:

sin, cos, tan, square, sqrt, max, min.

mathimatical things:

+, +=, -, -=, *, *=, /, /=

comparing things:

>, <, <=, >=, =, !=


first step is the lexical analysis which read a stream of characters and returns tokens

then semantic analysis

If anyone can help it would be cool otherwise thanks

您应该阅读书籍来执行此任务.编译器构造是一个广泛(且深入)的主题.
您可能会开始编写词法分析器,并在遇到困难时提出具体问题.
:)
You should read books to perform this task. Compiler construction is a broad (and deep) topic.
You may start writing the lexical analyzer and ask specific questions when you are stuck.
:)


希望您可以阅读有关IronPython,ToyScript和DLR的更多信息.这可能是方便且容易启动的.另外,如果您不能制作编译器,则不要采用该项目,如果您采用了该项目,请继续进行.不要寻找简单的解决方案.如果您在职业生涯中也会遇到类似的情况,该怎么办?
I hope you can read more about IronPython, ToyScript, and DLR. This might be handy and easy to start. Also, if you can’t make a compiler then don’t take that project and if you have taken it then work on it. Don’t look for easy solutions. What will you do if you will face similar situations in your professional life too?


我必须在这里同意CPallini的意见.一个简单的命令处理器可能会充满挑战.一个成熟的编译器(基于Pascal?Oy,那个会带回记忆)在任何方面都是一项艰巨的任务.

有为期一年的大学课程涵盖了这个主题.恐怕您无法通过浏览网络来学习.
I have to agree with CPallini here. A simple command processor can be challenging enough; a full-fledged compiler (based on Pascal? Oy, does that bring back memories) is a non-trivial undertaking by any stretch.

There are year-long college classes that cover this topic. It is not something that you can learn by browsing the net, I''m afraid.