GithubHelp home page GithubHelp logo

go-pl0-compiler's Introduction

Go-pl0-compiler

Golang 实现 pl0 编译器

目录架构

GO-PL0-COMPILER
│   .gitignore
│   go.mod
│   LICENSE
│   README.md
│
├───.idea // Goland 配置文件
│
├───assets
│       a.txt // pl0代码样例
│
├───fp
│       fp.go // 文件读取器
│
├───lexer
│       lexer.go // 词法分析器
│       lexer_test.go // 样例测试 包含exp1和exp2
│       state.go // DFA自动机状态声明
│
├───token
│       token.go // 符号枚举声明
│
└───utils
        charparse.go // 字符处理相关函数

词法分析器

Lexer 结构体封装了文件读指针、当前扫描行号(便于错误处理时的定位)和符号表,其方法 GetSym 扫描代码文件,并采用 DFA 方法获取符号信息:

// Lexer 词法分析器
type Lexer struct {
	file    *fp.File
	line    int      // 当前所在行号
	symbols []Symbol // 符号数组
}

Symbol 结构体维护了一个符号的分类信息以及字面量信息

// Symbol 符号
type Symbol struct {
	Id    token.Token // 符号枚举编号
	Value []rune      // 用户自定义的标识符值(若有)
	Num   int         // 用户自定义的数值(若有)
}

exp1 识别标识符

输出源程序中所有标识符出现的次数

键入:

cd lexer
go test -v -run TestExp1

image-20220310140915531

exp2 词法分析

输出程序中各个单词符号(关键字、专用符号以及其他标记)

键入:

cd lexer
go test -v -run TestExp2

image-20220310141031932

image-20220310141305925

go-pl0-compiler's People

Contributors

trouvaille0198 avatar oxotn3 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.