Coder Social home page Coder Social logo

facc's Introduction

Facc

Facc 是一种自制语言的工具,通过极简语法描述文法,自动生成AST代码。

特性:

  • 提供C++、C#实现,能生成C++、C#语言编写的解析器代码
  • 支持左递归、间接左递归

引言

编译原理自始至终都是非常难学的知识,虽然网上能找到各种各样的教程及文档,但也极少有开发者深入研究。本仓库作为另一种方案,以更简单的视角来解读编译原理,提供完善教程协助用户自主完成一个编译器。

开始

首先,NuGet上安装Facc。

生成AST:

var _grammar = @"   // 语法描述字符串
                    // 方括号代表匹配其中任一字符
num                 ::= [0-9]+
                    // 单引号或双引号代表匹配整个字符串,“|”代表“或”关系,匹配任一串字符串
op2_sign            ::= '+' | '-' | '*' | '/'
                    // 空格连接代表“与”关系,所有元素必须同时存在
op0_expr            ::= '(' expr ')'
                    // 匹配 1+2*3-4 这样的字符串
op2_expr            ::= expr (op2_sign expr)+
                    // 表达式允许纯数字、括号或四则运算字符串
expr                ::= num | op0_expr | op2_expr
";
string _path = "D:\\ASTs"; // AST解析文件生成路径
string _namespace = "Facc.Example.ASTs"; // 生成的AST解析文件的命名空间
var _generator = new AstGenerator (_grammar, _path, _namespace);
_generator.ClearPath (); // 清空指定路径下的所有文件
_generator.Generate (null); // 生成AST解析文件

执行生成的AST代码,解析文法:

var _ast_parser = new AstParser ();
var _root = _ast_parser.Parse<ASTs.ExprAST> ("3+2*5-4+(123213213");
if (_root != null) {
    Console.WriteLine ();
    _root.PrintTree (0);
} else {
    var _err =_ast_parser.Error;
    Console.WriteLine ();
    Console.WriteLine ($"Error in Line {_err.Line}: {_err.ErrorInfo}");
    Console.WriteLine (_err.LineCode);
    Console.WriteLine ($"{new string (' ', _err.LinePos)}^");
}

文档

License

代码开源方式:MIT
文档开源方式:CC-BY-SA 4.0

facc's People

Contributors

fawdlstty avatar

Stargazers

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