Giter Site home page Giter Site logo

lichenqin / toycompiler Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 617 KB

This toy compiler is designed base on Tiny compiler which written in C. This project focus on the Front-end of the compiler and doesn't perform well on code generation. I will finish this compiler in few day.

C 95.59% C++ 1.21% Yacc 3.20%
compiler c

toycompiler's Introduction

Project: Toy Compiler

说明:

暂时有三个文件夹:

  • Source中是源码部分
  • Yacc中保存了 toy language 的Yacc实现语法和 toy.y

Source中有:

  • type.h 定义token类型 定义抽象语法书结构 导入<stdlib.h> <stdio.h> 是所有文件的基础
  • scan.h 声明scanner部分所用函数与结构
  • scan.c 实现 其中getToken()最重要
  • parse.h 声明parse过程用到的函数
  • parse.c 实现 其中parse()最重要
  • main.c 主函数 串连scanner parser code三部分
  • etc

Yacc中有:

  • Yacc/Bison BNF文法: toy描述
  • toy.y
  • etc

符号定义规则:

Identifier: 字母开头, 可以包括数字、字母、下划线,最多31位

Number: 包括int_10、int_8、int_16、float_10、float_8、float_16,最多31位

Special Number: 包括 + - * / ( ) > < = == 其余均不包含

Reserve Word: 包括IF THEN ELSE DO WHILE IN OUT END

用法

>> toy filename.tiny

控制台输出TokenType以及对应的字符串, 相关语法树。

同时包含 filename.tiny.out的输出文件 输出内容与控制台显示内容相同(暂未实现)。

BNF语法:

  • 添加了三个新的关键字: end in out

  • 在exp状态下,既要保持左优先性,也要保持双目运算符号的优先级

start --> statement_sequence

statement_sequence --> statement_sequence; statement| statement

statement --> if-stm | while-stm | in-stm | out-stm | assign-stm

if-stm --> if exp then statement_sequence end

​ | if exp then statement_sequence else statement_sequence end

while-stm --> while exp do statement_sequence end

in-stm --> in ID

out-stm --> out ID

​ | out number

assign-stm --> ID = exp

exp --> exp1 compare exp1 | exp1 equal exp1 | exp1

equal --> ==

compare --> > | <

exp1 --> exp2 addop exp2 | exp2

addop --> + | -

exp2 --> exp3 mulop exp3 | exp3

mulop --> * | /

exp3 --> ( exp ) | INT10 | INT8 | INT16 | FLO10 | FLO8 | FLO16 | ID

细节说明

16进制只能包含大写字母

所有Statement产生式之间以;相连,最后一句Statement不能以;结尾

运算符优先级为(优先级自高至低):

  • ()
  • * /
  • + -
  • > < =

toy语言文件以 .toy 结尾

toycompiler's People

Contributors

lichenqin avatar

Stargazers

Andrew Johnson avatar

Watchers

 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.