{"id":19289791,"url":"https://github.com/ragibson/minijava-compiler","last_synced_at":"2025-10-06T01:52:54.311Z","repository":{"id":143220419,"uuid":"186902246","full_name":"ragibson/miniJava-compiler","owner":"ragibson","description":"A compiler for a subset of Java (\"miniJava\") as well as an abstract machine and test suites.","archived":false,"fork":false,"pushed_at":"2019-05-23T16:36:31.000Z","size":118,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-05T19:42:36.902Z","etag":null,"topics":["abstract-machine","compiler","java","minijava"],"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/ragibson.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-15T21:02:18.000Z","updated_at":"2023-01-27T03:43:34.000Z","dependencies_parsed_at":"2023-06-01T21:00:27.316Z","dependency_job_id":null,"html_url":"https://github.com/ragibson/miniJava-compiler","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/ragibson%2FminiJava-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragibson%2FminiJava-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragibson%2FminiJava-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragibson%2FminiJava-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ragibson","download_url":"https://codeload.github.com/ragibson/miniJava-compiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240395740,"owners_count":19794573,"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":["abstract-machine","compiler","java","minijava"],"created_at":"2024-11-09T22:17:11.548Z","updated_at":"2025-10-06T01:52:49.261Z","avatar_url":"https://github.com/ragibson.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# miniJava-compiler\n\nThis repository contains a compiler for a subset of Java known as \"miniJava\".\n\nIn particular, this language is based off of Appel and Palsberg's MiniJava\n(see Appendix A of ISBN-13 9780521820608):\n\n\u003e MiniJava is a subset of Java. The meaning of a MiniJava program is given by\nits meaning as a Java program. Overloading is not allowed in MiniJava. The\nMiniJava statement `System.out.println(...);` can only print integers. The\nMiniJava expression `e.length` only applies to expressions of type `int[]`.\n\n# Table of Contents\n  * [Compilation Steps](#CompilationSteps)\n  * [Example Compilations](#ExampleCompilations)\n    * [Factorial](#Example1)\n    * [Objects and Arrays](#Example2)\n    * [Invalid Program](#Example3)\n  * [Tests](#Tests)\n  * [Installation and Usage](#InstallationAndUsage)\n\n\u003ca name = \"CompilationSteps\"\u003e\u003c/a\u003e\n## Compilation Steps\n\nThis compiler's operation is broken into four primary steps: syntactic\nanalysis, abstract syntax tree construction, type checking, and code\ngeneration.\n\n1. __Syntactic Analysis__: Recognize syntactically correct miniJava programs\nand reject syntactically incorrect inputs.\n\n2. __AST Construction__: Create a syntax tree that represents the abstract\nstructure of source code (including operator precedence).\n\n3. __Type Checking__: Identify the variable/parameter/member/class\ndeclarations associated with each expression. Then, ensure that all\nexpressions, function calls, variable assignments, etc. obey Java's type\nrules.\n\n4. __Code Generation__: Generate machine instructions to execute the program\nof interest (targeting the \"mJAM\" abstract machine).\n\n\u003ca name = \"ExampleCompilations\"\u003e\u003c/a\u003e\n## Example Compilations\n\n\u003ca name = \"Example1\"\u003e\u003c/a\u003e\n### Factorial\n\nThe program\n\n    class Factorial {\n        public static void main(String[] args) {\n            // 7! = 5040\n            System.out.println(factorial(7));\n        }\n\n        static int factorial(int n) {\n            if (n \u003c= 1)\n                return 1;\n            return n * factorial(n - 1);\n        }\n    }\n\nyields the following assembly\n\n      0         PUSH         0\n      1         LOADL        0\n      2         CALL         newarr\n      3         CALL         L10\n      4         HALT   (0)\n      5  L10:   LOADL        7\n      6         CALL         L11\n      7         CALL         putintnl\n      8         RETURN (0)   1\n      9  L11:   LOAD         -1[LB]\n     10         LOADL        1\n     11         CALL         le\n     12         JUMPIF (0)   L12\n     13         LOADL        1\n     14         RETURN (1)   1\n     15         JUMP         L12\n     16  L12:   LOAD         -1[LB]\n     17         LOAD         -1[LB]\n     18         LOADL        1\n     19         CALL         sub\n     20         CALL         L11\n     21         CALL         mult\n     22         RETURN (1)   1\n\nwhich outputs\n\n    \u003e\u003e\u003e 5040\n\nwhen run.\n\n\u003ca name = \"Example2\"\u003e\u003c/a\u003e\n### Objects and Arrays\n\nThe program\n\n    class MainClass {\n        public static void main(String[] args) {\n            A[] aa = new A[2];\n            A b = new A();\n            b.x = 3;\n            aa[0] = b;\n            A c = new A();\n            c.x = 5;\n            aa[1] = c;\n\n            A t = aa[0];\n            A s = aa[1];\n\n            int result = t.x + s.x + 13; // 21\n            System.out.println(result);\n        }\n    }\n\n    class A {\n        int x;\n    }\n\nyields the following assembly\n\n      0         PUSH         0\n      1         LOADL        0\n      2         CALL         newarr\n      3         CALL         L10\n      4         HALT   (0)\n      5  L10:   LOADL        2\n      6         CALL         newarr\n      7         LOADL        -1\n      8         LOADL        1\n      9         CALL         newobj\n     10         LOAD         4[LB]\n     11         LOADL        0\n     12         LOADL        3\n     13         CALL         fieldupd\n     14         LOAD         3[LB]\n     15         LOADL        0\n     16         LOAD         4[LB]\n     17         CALL         arrayupd\n     18         LOADL        -1\n     19         LOADL        1\n     20         CALL         newobj\n     21         LOAD         5[LB]\n     22         LOADL        0\n     23         LOADL        5\n     24         CALL         fieldupd\n     25         LOAD         3[LB]\n     26         LOADL        1\n     27         LOAD         5[LB]\n     28         CALL         arrayupd\n     29         LOAD         3[LB]\n     30         LOADL        0\n     31         CALL         arrayref\n     32         LOAD         3[LB]\n     33         LOADL        1\n     34         CALL         arrayref\n     35         LOAD         6[LB]\n     36         LOADL        0\n     37         CALL         fieldref\n     38         LOAD         7[LB]\n     39         LOADL        0\n     40         CALL         fieldref\n     41         CALL         add\n     42         LOADL        13\n     43         CALL         add\n     44         LOAD         8[LB]\n     45         CALL         putintnl\n     46         RETURN (0)   1\n\nwhich outputs\n\n    \u003e\u003e\u003e 21\n\nwhen run.\n\n\u003ca name = \"Example3\"\u003e\u003c/a\u003e\n### Invalid Program\n\nThe program\n\n    class Fail328 {\n        public static void main(String[] args) {\n            F05 c = new F05();\n            c = c.foo.next;\n        }\n    }\n\n    class F05 {\n        public F05 next;\n        public F05 foo() {return this;}\n    }\n\nyields the compiler output\n\n    *** line 4 function reference cannot appear in the middle of a qualified reference.\n    INVALID program after identification\n\n\u003ca name = \"Tests\"\u003e\u003c/a\u003e\n## Tests\n\nThe tests directory contains the test suites (provided by Jan Prins of\nUNC-Chapel Hill)\n\n  |Directory|Compilation Step  |Success Count|Success Rate|\n  |---------|------------------|-------------|------------|\n  |pa1_tests|Syntactic Analysis|106/106      |100%        |\n  |pa2_tests|AST Construction  |80/80        |100%        |\n  |pa3_tests|Type Checking     |94/94        |100%        |\n  |pa4_tests|Code Generation   |35/35        |100%        |\n  |pa5_tests|All               |53/60        |88%         |\n\nThese can be automatically run by the testers in miniJava.tester package.\n\n\u003ca name = \"InstallationAndUsage\"\u003e\u003c/a\u003e\n## Installation and Usage\n\nIn Eclipse, this compiler can be installed by simply importing the mJAM,\nminiJava, and tester packages into a new Java project.\n\nThe tester subpackage requires the tests directory to be imported into a\nseparate project called \"tests\".\n\nCompiler.java is the main entry point of the compiler. It takes the path to a\nsource code file as the first argument and a final compiler stage as an\noptional second argument for testing purposes (one of \"PARSER\", \"TYPE\nCHECKING\", and \"CODE GENERATION\").\n\nThe compiler generates an object code .mJAM file and an assembly .asm file,\nwhich can be run with the mJAM abstract machine.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragibson%2Fminijava-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fragibson%2Fminijava-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragibson%2Fminijava-compiler/lists"}