{"id":22777833,"url":"https://github.com/a1exwang/decaf","last_synced_at":"2025-03-30T13:41:57.388Z","repository":{"id":95285101,"uuid":"44853840","full_name":"a1exwang/decaf","owner":"a1exwang","description":"Alex's Decaf Compiler","archived":false,"fork":false,"pushed_at":"2015-10-24T06:17:37.000Z","size":464,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T15:28:10.453Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a1exwang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-24T05:38:02.000Z","updated_at":"2015-10-24T06:13:40.000Z","dependencies_parsed_at":"2023-03-11T03:01:12.435Z","dependency_job_id":null,"html_url":"https://github.com/a1exwang/decaf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1exwang%2Fdecaf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1exwang%2Fdecaf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1exwang%2Fdecaf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1exwang%2Fdecaf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a1exwang","download_url":"https://codeload.github.com/a1exwang/decaf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246326598,"owners_count":20759436,"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","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":[],"created_at":"2024-12-11T19:17:26.377Z","updated_at":"2025-03-30T13:41:57.366Z","avatar_url":"https://github.com/a1exwang.png","language":"Java","readme":"# Alex's Decaf Compiler\nhttps://github.com/a1exwang/decaf\n## Stage 1: Lexer and Parser\n\n### Lexer\n\n需要添加的token有以下, 由于JFlex从上到下依次匹配正则表达式并创建token, 所以应注意, 关键字需要写在identifier之前\n\n\n    \"++\"\t\t\t\t{ return operator(Parser.INC); \t\t\t}\n    \"--\"\t\t\t\t{ return operator(Parser.DEC);\t\t\t}\n    \"|||\"\t\t\t\t{ return operator(Parser.GUARD_SEPERATOR); }\n    \"fi\"\t\t\t\t{ return keyword(Parser.FI);\t\t\t}\n    \"do\"\t\t\t\t{ return keyword(Parser.DO);\t\t\t}\n    \"od\"\t\t\t\t{ return keyword(Parser.OD);\t\t\t}\n    ...\n同时在Parser中需要添加对应的token常量.\n\n### Parser\n解决移进冲突主要有两种方式. 一是通过添加语法规则, 二是使用BYACC提供的优先级.\n通过阅读代码可以知道, 原来Decaf语言中除了Expr外均使用方法一解决冲突, Expr使用方法二解决\n冲突.\n\n所以虽然文档中要求了自增操作符等操作符只能和Identifier连用, 但是我们在这里对所有的操作符, 都使用Expr, 而不是Identifier,\n这样就可以方便地用优先级来解决冲突, 然后再在以后的语义分析中, 解决非Identifier的Expr和操作符连用的问题.\n\n对Parser的修改如下:\n\n1. 添加了一下的语法规则\n\n\n    Expr:...\n    ...\n       |    INC Expr\n            {\n                $$.expr = new Tree.Unary(Tree.PREINC, $2.expr, $1.loc);\n            }\n        |   DEC Expr\n        ...\n        |   NUMINSTANCES '(' IDENTIFIER ')'\n            {\n                $$.expr = new Tree.NumInstances($3.ident, $1.loc);\n            }\n    ...\n    Stmt:\n        ...\n        |   GuardStmt // 这里GuradStmt既可以是if又可以是while, 这两种共享同一种树节点, 用type来区分\n    ...\n    // 在这里实现了两种GuardStmt\n    GuardStmt :         IF GuardClauses FI\n                        ...\n                    |   DO GuardClauses OD\n                        ...\n    // 考虑到两种GuardStmt除了关键字外, 结构是完全相同的, 所以这里统一为GuardClauses\n    GuardClauses :      GuardClauses GUARD_SEPERATOR Expr ':' Stmt\n                        ...\n                    |   Expr ':' Stmt\n                        ...\n\n2. 同时在Tree中添加了对应的语法分析树节点类型.\n\n\n    // 在Unary中加入了++和--操作符\n\n    // numinstances\n    public static class NumInstances extends Expr { ... }\n\n    // 三元操作符, 用来表示 : ?\n    public static class Ternary extends Expr {\n        public Expr left;\n        public Expr middle;\n        public Expr right;\n        ...\n    }\n\n    // Guard语句\n    public static class GuardClause extends Tree { ... }\n    public static class Guard extends Tree {\n        public enum Type {\n            Condition,\n            Loop\n        } // 用来区分Guard语句的类型\n        Type type;\n        List\u003cGuardClause\u003e clauses; // Guard内部的各个条件语句\n        ...\n    }\n\n3. 下一步备注\n    1. 检测Expr和Identifier语法正确, 语义不正确的情况\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa1exwang%2Fdecaf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa1exwang%2Fdecaf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa1exwang%2Fdecaf/lists"}