Code Monkey home page Code Monkey logo

hython's Introduction

Hython语言设计与开发

项目描述:artificial_satellite:

海贼 19-8 班 C++ 综合项目,设计一门自己的编程语言(类似Python解析器)

介绍:arrow_heading_down:

为了实战C++设计模式,因此编写一个类似Python的解析器。不言而喻,这是一个很有意义的事情,并且很容易成为了我做过的最酷的项目。通过使用了Antlr解析成抽象语法树,可以自定义语法,这很nice!

状态:statue_of_liberty:

更新中...

文件介绍

  1. 1.antlr_testAntlr的基本使用方法的一个小例子(建议先研究)

  2. 2.hython-v1Hython语言的设计开发源代码

    bin目录下存放的是生成可执行文件hythonhython xx.py类似python xx.py

    include存放的头文件,其中ExprTreeEvaluator.hhythonLexer.hhythonParser.hAntlr生成的,通常没必要理会。

    lib存放的你所安装Antlr后的libantlr3c.a(自行找一下),和环境有关系,clone下的代码,你必须先把这个.a换成你自己的,否则环境不同跑不通。

    src存放所有的.c.cc文件。

    testdata存放所有的测试数据文件。

    thirdpart中的.jar是编译的时候必备的,和java有关系,具体编译参考makefile文件,此处.g文件是存放文法文本的,自定义语法的地方,.tokesAntlr生成的,里面给每种语法字符进行了编上数字,以便于在编写设计程序时的switch()方便。

    makefile编译文件。

  3. 3.Others存放的其他。

测试

测试很简单makefile中把所有的运行自动化在此套件中

$make #make debug
$make run

Antlr 的基础使用

Antlr将源码解析成抽象语法树.它为包括Java,C++,C#在内的语言提供了一个通过语法描述来自动构造自定义语言的识别器(recognizer),编译器(parser)和解释器(translator)的框架。

安装:

http://blog.csdn.net/wfp458113181wfp/article/details/9148577

http://contrapunctus.net/blog/2012/antlr-c

在文法文件中含有输出结构信息(tree ...)的语句 通过 jar 嵌入到生成的 c 中

  1. 增加除法表达式的支持(增加文本[源代码]中的关键字)
  2. 增加取余表达式的支持(增加文本[源代码]中的关键字)
  3. 修改解释器,对于每一行内容,都输出其表达式的值(修改解析器的功能)
  4. 增加定义变量的语法结构
  5. 增加作用域的功能(完成文发文件解释器的程序部分)

简单的流程控制

  1. 整理工程目录,inclue/lib/bin/src/...
  2. 整理main.cpp内容,分成头文件和源文件,main.cpp尽量简单
  3. print子句
  4. 条件表达式(==,<=,>=,&&,||)
  5. if结构的增加
  6. for结构的增加
  7. while结构的增加
  8. do...while结构的增加
  9. 找 bug非常关键,能够理解 bug,才能锻炼思维

拆解 ExprTreeEvaluator 的功能

  1. 用责任链模式改写整体 ExprTreeEvaluator 类的功能
  2. 增加责任链的单例模式,MasterChianSingle 类
  3. 增加 master 类
  4. 增加了作用域 parameter 类
  5. 找到程序中的bug

增加弱类型系统

其他

.g的作用

// The suffix '^' means make it a root.   ^作用:提升至抽象语法树的根节点。
// The suffix '!' means ignore it.   !作用:忽略

expr表达式冒号后面是表达式*代表:0多次+代表1多次

atom原子操作INTID, (),INT代表整数ID代表标识符stmt代码段->代表重映射ASSIGN代表= ;

^(ASSIGN ID expr)代表=号为根节点IDexpr 分别左右子树

抽象语法树

image-20200829110259868

责任链模式

image-20200830232450309

hython's People

Contributors

fangsong0517 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.