Git Product home page Git Product logo

bupt-compilation-principle-lab's Introduction

README

项目构成

本项目主要实现了c语言的词法分析和基于LR(1)文法的语法分析。词法分析的类和语法分析的类是完全独立分开的,包括后面进行输入表达式分析也需要单独定义词法分析类和语法分析类,这也是为了加强模块的独立性和可拓展性。

详细实现还需参考report

词法分析

词法分析code由头文件Lexical_Analysis.h和主程序文件Lexical_Analysis.cpp构成,词法分析类定义为class Lexical_Analysis,包括源文件输入、词法分析主程序和一些定义的统计结果。

语法分析

语法分析code由头文件Grammer_Analysis.h和主程序文件Grammer_Analysis.cpp构成,头文件定义了主要类class Grammer以及其他必须的数据结构如string_setitem(包括相关方法)等,语法分析类中定义了First集Follow集项目规范集集合/dfaLR1分析表,以及分析必须的方法。可以见思维导图。

测试

main.cpp中完成词法、语法的测试

词法分析

  • lexical_ana()

    输入源文件名进行完整一次扫描的词法分析,如下

    请输入源文件名:
    id.txt
  • lexical_ana_single()

    输入源文件名one token by one token进行词法分析

项目中给出了三个测试源程序文本id.txtop.txtstr.txt

语法分析

  • 文法文件格式要求

    开始符号(不能为S,因为程序给的拓广文法开始符就为S)
    n n个终结符,以空格隔开
    m m个非终结符,以空格隔开
    产生式1 //为了方便代码实现,产生式不能是一对多如A->B|C。
    产生式2
    ...

    实验文法文件: G.txt

    实验文法待分析表达式: G_exp.txt

    一个c++声明拓展文法: G1.txt

  • grammer_ana()

    输入待分析表达式文件名和文法文件名,程序会先对待分析表达式进行词法分析,再自动输入文法计算first、dfa、LR1分析表等并格式化打印,最后利用分析表进行语法分析

    请输入分析串所在文件名:
    G_exp.txt
    请输入文法文件名:
    G.txt
    
  • grammer_ana_realtime()

    同上,不过这个程序是词法分析和语法分析同时进行

注:

  1. 在语法分析输入文法文件名后,程序(void Grammer::generate_graphiviz_code)会自动生成文法dfa的graphiviz可视化代码于gra_code.txt文件。

  2. 其他测试例子可以见report,详细设计说明也需参考report

  3. 你可以任意给定文法得到LR1表并进行LR1分析以及生成可视化dfa的代码,前提是文法的非终结符和词法分析结果要相匹配。

bupt-compilation-principle-lab's People

Contributors

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