1 Star 1 Fork 0

fawdlstty / Facc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

MIT License Copyright (c) 2021 fawdlstty Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于.NET5的编译器生成工具 展开 收起
C# 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/fawdlstty/Facc.git
git@gitee.com:fawdlstty/Facc.git
fawdlstty
Facc
Facc
main

搜索帮助