Coder Social home page Coder Social logo

agzparsergen's Introduction

试着做个Parser Generator,算是一次做一百个人的编译原理作业吧XD……

使用姿势大概是这样的:

  1. 先写文法描述脚本,描述想要的Parser的上下文无关文法,脚本大概长这样:

     # 注释以'#'开头
     import [Utility/Utility.agz] # 支持从其他文件导入
    
     # 支持命名空间
     namespace MyNamespace
     {
         # 定义非终结符
         A := Utility.S + A; # 使用'.'访问命名空间成员
         A := "token_name";  # 使用双引号包住token类型名
     }
    
     # 定义开始符号
     AGZ_Start := MyNamespace.A;
    
  2. 然后在自己的程序中调用AGZParserGen的生成接口,得到Parser实例:

    Parser<TokenAdaptor> parser;
    parser.BuildFromSourceFile(tokenAdaptor, filename);

    其中TokenAdaptor是词法单元类型适配器,用来实现文法脚本中的词法单元和进行实际语法分析时使用的词法单元之间的映射,由用户自己指定。当然,我会写个简单的字符串映射版本作为默认。这么设计一是为了更加灵活,二是我不想处理字符编码问题XD……

  3. 接下来就想怎么用就怎么用了,可以直接拿来分析,也可以输出静态分析器的代码:

    // 直接分析源代码,得到AST
    auto AST = parser.Parse(tokenAdaptor, tokemStream);
    
    // 输出成C++静态分析代码
    // 待完成
    parser.PrintAs<AGZ::CppBackend>("staticCppParser.h", "staticCppParser.cpp");

详细的用法参见Usage

agzparsergen's People

Contributors

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