{"id":22354757,"url":"https://github.com/pprp/mnc-javacc","last_synced_at":"2025-08-29T06:26:27.730Z","repository":{"id":112699102,"uuid":"163962905","full_name":"pprp/Mnc-javacc","owner":"pprp","description":"Implementing the Minic compiler with javacc","archived":false,"fork":false,"pushed_at":"2019-01-03T12:23:53.000Z","size":6617,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T12:47:20.208Z","etag":null,"topics":["javacc","minic","mnc-javacc","switch-case"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pprp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-03T11:14:18.000Z","updated_at":"2020-12-15T14:44:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"96e886c3-c516-4198-86bc-fa658986b29d","html_url":"https://github.com/pprp/Mnc-javacc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pprp/Mnc-javacc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2FMnc-javacc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2FMnc-javacc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2FMnc-javacc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2FMnc-javacc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pprp","download_url":"https://codeload.github.com/pprp/Mnc-javacc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2FMnc-javacc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272641541,"owners_count":24968802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-29T02:00:10.610Z","response_time":87,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javacc","minic","mnc-javacc","switch-case"],"created_at":"2024-12-04T13:15:03.710Z","updated_at":"2025-08-29T06:26:27.702Z","avatar_url":"https://github.com/pprp.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mnc-javacc\nImplementing the Minic compiler with javacc\n\n---\n\n编译原理课程实习：\n\n### 功能要求\n\n1. 用流的形式读入要分析的MiniC源程序。\n\n2. 能够根据单词的构词规则，完成MiniC语言中的单词的解析(词法分析)，如果不符合单词的构词规则，请给出错误信息。如果源语言符合单词的词法规则，请输出\u003c单词种别，单词自身值\u003e二元式。（注：利用JavaCC实现）\n\n3. 在词法分析的基础上，构造MiniC的递归下降分析文法，利用JavaCC实现递归下降分析文法。判断源语言是否符合MiniC的语法，如果符合，输出语法树；否则，请给出语法错误信息。\n\n4. 在语法分析的基础上，根据属性文法制导翻译，进行语义分析，输出四元式。如果源语言不符合MiniC的语义，请指出错误信息。\n\n5. 在平时实习课的基础上，整个编译系统要能够翻译声明语句、数组(二维，多维)翻译，数学表达式、关系表达式、布尔表达式，if-else, for, while，do-while，switch-case，赋值等语句嵌套的分析与翻译。\n\n6. 实现符号表，并能够检查变量重复声明、使用时未定义错误。\n\n7. 实现扩展内容：\n   1. 扩展内容：可以考虑多个函数及函数调用翻译。\n   2.  扩展内容：可以添加功能，如break、continue、return语句、一维指针。\n   3.  扩展内容：可以检查赋值精度错误、函数参数类型不匹配错误、静态数组元素下标越界错误、break错误、continue错误等。\n   4.  扩展内容：为编译器添加界面。\n\n### 实现的功能\n\n(1) 采用流的形式读入要分析的MiniC源程序。\n\n(2) 编写一个简单的编译器界面来对成果进行展示。\n\n(3) 实现词法分析和语法分析，并将其结果输出到文件。\n\n(4) 实现语义分析，主要有：声明语句、数学表达式、关系表达式。\n\n(5) 实现if-else, for, while，do-while，switch-case，赋值等语句嵌套的分析与翻译。\n\n(6) 实现符号表，能检查变量的重复声明，使用时未定义错误。\n\n### 界面展示\n\n![](./PPRP/1.png)\n\n![](./PPRP/2.png)\n\n![](./PPRP/3.png)\n\n### 设计思路\n\n![img](./PPRP/4.png)\n\n### 文法设计\n\n\n\n命名:\n\n```\n程序：Program\n语句块：StateBlock\n语句：State\n顺序语句：SeqState\n分支语句：BranchState\n循环语句：LoopState\n\n条件语句：IfState\nswitch语句：SwtichState\n\nwhile语句：WhileState\ndowhile语句：DoWhileState\nfor语句：ForState\n\n自增自减语句：AutoAS\n声明语句：DecState\n赋值语句：AssState\nbreak语句： BreakState\n\n类型：Type\n表达式：Expression\n条件：BoolExpression\n条件：Condition\n逻辑运算符：LogicalOperator\n运算符：Operator\n关系符：Relation\n```\n\n文法：\n\n```\n\u003c程序\u003e -\u003e \u003c类型\u003e main \"(\" (\u003c类型\u003e ID)* \")\" \u003c语句块\u003e\n\u003c语句块\u003e -\u003e \u003c语句\u003e | { \u003c语句块\u003e*}\n\u003c语句\u003e -\u003e \u003c顺序语句\u003e | \u003c分支语句\u003e | \u003c循环语句\u003e\n\u003c顺序语句\u003e -\u003e (\u003c声明语句\u003e | \u003c赋值语句\u003e | \u003cbreak语句\u003e | \u003c自增自减语句\u003e) \";\"\n\u003c声明语句\u003e -\u003e \u003c类型\u003e ID (,ID)*\n\u003c类型\u003e -\u003e int | void | double | float | char\n\u003c赋值语句\u003e -\u003e ID = \u003c表达式\u003e\n\u003cbreak语句\u003e -\u003e break\n\n//分支部分： 包括if-else和switch结构\n\u003c分支语句\u003e —\u003e \u003c条件语句\u003e | \u003cswitch语句\u003e\n\u003cswitch语句\u003e -\u003e switch\"(\" ID \")\" \n\"{\" \n(case \u003c表达式\u003e \":\" \u003c语句块\u003e \u003cbreak语句\u003e)*\n (default \":\" \u003c语句块\u003e \u003cbreak语句\u003e)?\n (case \u003c表达式\u003e \":\" \u003c语句块\u003e \u003cbreak语句\u003e)*\n \"}\"\n\u003c条件语句\u003e -\u003e if \"(\" \u003c条件\u003e \")\" \u003c语句块\u003e (else \u003c语句块\u003e)？\n//循环部分： 包括while，do-while,for三个结构\n\u003c循环语句\u003e -\u003e \u003cwhile语句\u003e | \u003cdoWhile语句\u003e | \u003cfor语句\u003e\n\u003cwhile语句\u003e -\u003e while \"(\" \u003c条件\u003e \")\" \u003c语句块\u003e\n\u003cdoWhile语句\u003e -\u003e do \u003c语句块\u003e while\"(\" \u003c条件\u003e \")\" \";\"\n\u003cfor语句\u003e -\u003e for \"(\" \u003c赋值语句\u003e ? \";\" \u003c条件\u003e \";\" \u003c赋值语句\u003e? \")\" \u003c语句块\u003e\n\u003c条件\u003e -\u003e \u003c条件\u003e (\u003c逻辑运算符\u003e \u003c条件\u003e)*\n\u003c条件\u003e -\u003e \u003c表达式\u003e(\u003c关系符\u003e \u003c表达式\u003e)?\n\u003c表达式\u003e -\u003e 默认javacc\n\u003c运算符\u003e -\u003e + | - | * | /\n\u003c关系符\u003e -\u003e \u003c | \u003c= | \u003e= | == | !=\n\u003c逻辑运算符\u003e -\u003e \"\u0026\u0026\" | \"||\"\n```\n\n### 测试文件\n\n\u003e 1.txt--if else\n\n```c\nint main()\n{\n    int a;\n    int b;\n    if(a \u003e 0)\n    {\n        a = -1;\n    }\n    else\n    {\n        b = -1;\n    }\n}\n```\n\n\u003e 2.txt--for\n\n```c\nint main()\n{\n    int a;\n    a = 0;\n    int i;\n    for(i = 0 ; i \u003c 10 ; i++)\n    {\n        a = a+1;\n    }\n}\n```\n\n\u003e 3.txt -- do-while\n\n```c\nint main()\n{\n    int a;\n    a = 0;\n    int c;\n    c = n;\n    do\n    {\n        a = a+1;\n        n = n-1;\n    }\n    while(n\u003e0);\n}\n```\n\n\u003e 4.txt -- switch-case\n\n```c\nint main()\n{\n    int a;\n    a = 2;\n    int output;\n    switch(a)\n\t{\n        case 1:\n        \toutput = 2;\n        \tbreak;\n        case 2:\n        \toutput = 1;\n        \tbreak;\n        default:\n        \toutput = 3;\n        \tbreak;\n\t}    \n}\n```\n\n\u003e 5.txt - 嵌套测试\n\n```c\nint main()  \n{  \n    int ans;ans = 0;int i;  \n    int num;num = 12;int output;\n    for(i = 0 ; i \u003c 10 ; i++)  \n    {\n        if(ans \u003c n){  \n            ans = ans * 2 + 3 - 1;  \n        }else{  \n            ans = ans * 3 - 1;  \n        }  \n        do{\n            ans = ans-2;  \n        }while(ans \u003e 12);   \n        while(n \u003e 3){  \n            ans = ans-2;          \n        }  \n        switch(ans){\n            case 1:\n                output = num;\n                break;\n            case 2:\n                output = num * num;\n                break;\n            default:\n                output = 0;                \n        }\n    }\n    int ans;\n}  \n```\n\n\u003e 6.txt while\n\n```c\nvoid main()\n{\n    int i;\n    int j;\n    while(i \u003c j)\n    {\n        i = i+2;\n        j = j-2;\n    }        \n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprp%2Fmnc-javacc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpprp%2Fmnc-javacc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprp%2Fmnc-javacc/lists"}