{"id":20777597,"url":"https://github.com/semgrep/project-reason-tree-sitter","last_synced_at":"2025-12-16T08:22:41.119Z","repository":{"id":43150357,"uuid":"243384072","full_name":"semgrep/project-reason-tree-sitter","owner":"semgrep","description":"project for program analysis candidates","archived":false,"fork":false,"pushed_at":"2022-03-16T08:12:59.000Z","size":18,"stargazers_count":2,"open_issues_count":0,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-01-18T08:28:14.614Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Reason","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/semgrep.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}},"created_at":"2020-02-26T23:02:05.000Z","updated_at":"2022-03-16T08:13:03.000Z","dependencies_parsed_at":"2022-09-03T08:43:49.118Z","dependency_job_id":null,"html_url":"https://github.com/semgrep/project-reason-tree-sitter","commit_stats":null,"previous_names":["semgrep/project-reason-tree-sitter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semgrep%2Fproject-reason-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semgrep%2Fproject-reason-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semgrep%2Fproject-reason-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semgrep%2Fproject-reason-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/semgrep","download_url":"https://codeload.github.com/semgrep/project-reason-tree-sitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243119661,"owners_count":20239319,"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-11-17T13:16:14.459Z","updated_at":"2025-12-16T08:22:36.083Z","avatar_url":"https://github.com/semgrep.png","language":"Reason","funding_links":[],"categories":[],"sub_categories":[],"readme":"# project-reason-tree-sitter\n\nThe goal of this project is to write code to convert the definition\nof a programming language *grammar* (e.g., `tests/arithmetic/grammar.json`) into\nthe definition of an *Abstract Syntax tree* (AST) for this grammar\n(e.g., `tests/arithmetic/ast_arithmetic.re` for the expected output on the\nprevious grammar). \nThis AST definition can be automatically derived from the grammar.\n\nThe grammar definition we are using (e.g., `tests/arithmetic/grammar.json`)\ndoes not use a traditional format (e.g., a BNF grammar using the Yacc syntax).\nInstead, it uses a particuliar format (see the Context section below). \nThis `grammar.json` file is derived itself from another file\n(e.g., `tests/arithmetic/grammar.js` for the arithmetic grammar) which is\neasier to read (but harder to analyze). \nSee http://tree-sitter.github.io/tree-sitter/creating-parsers#the-grammar-dsl \nif you need to understand the format of `grammar.js`\n(which itself will help to understand the format of `grammar.json`).\n\nYou can use any programming language to solve this problem, but we provide\nskeleton code only for ReasonML.\n\n## Main problem\n\nTo test your code, run: \n```bash\n./_build/default/bin/main_codegen.exe -codegen_types tests/arithmetic/grammar.json \u003e tests/arithmetic/ast_arithmetic_output.re\n```\nand compare your result in `tests/arithmetic/ast_arithmetic_output.re` with\nthe expected output in `tests/arithmetic/ast_arithmetic.re`.\nYour result does not have to match exactly `tests/arithmetic/ast_arithmetic.re`,\nbut it should compile, and it should be mostly equivalent to\n`tests/arithmetic/ast_arithmetic.re`.\n\nNote that your code should be general enough that it can be applied to\nother grammar files, not just `tests/arithmetic/grammar.json`.\n\nThe code you have to implement is mostly in `lib/codegen_types.re`.\n\n## Hint\n\nBecause the format of the grammar in `grammar.json` allows nested\nalternatives, it can be difficult to generate directly from the\n`grammar.json` the ReasonML type definitions. You could find useful to\ndefine an intermediate `ast_normalized_grammar.re` that would be closer\nto what ReasonML can accept for type definitions.\n\n## Bonus\n\nAs a bonus, you can generate the type definitions in a certain order,\nfrom the toplevel types (e.g., `program`) to its leaves (e.g., `number`)\nas done in `tests/arithmetic/ast_arithmetic.re`. You will need\nto perform a topological sort of the type dependencies to do so.\n\n## Context \n\nThe `tests/arithmetic/grammar.json` file is part of the tree-sitter project\nhttps://github.com/tree-sitter/tree-sitter. tree-sitter is a parser\ngenerator (similar to Yacc), which takes as input a `grammar.js` file\n(e.g., `tests/arithmetic/grammar.js`). From this `grammar.js` file it\ncan generate a JSON file defining the same grammar but that is easier\nto analyze (e.g., `tests/arithmetic/grammar.json`) and from this file\nit can generate a parser for this grammar in C. \n\nNote that you do not need to understand or use tree-sitter for this project.\nWe just use the same format for the grammar definition (`grammar.json`).\n\n## What we are looking for:\n\n* Comfort with the language of choice: e.g. JSON parsing and matching should be easily understood\n* Grasp of grammar, abstract datatypes, polymorphic types\n* Test driven development\n* Communication of complex graph algorithms like recursive top-down walk\n* Good solution involves use of intermediate normalized AST definition\n\n\n## Installation from source\n\nTo compile the code, you first need to [install OCaml](https://opam.ocaml.org/doc/Install.html) and its package manager OPAM.\nOn macOS, it should simply consist in doing:\n\n```bash\nbrew install opam\nopam init\nopam switch create 4.07.1\nopam switch 4.07.1\neval $(opam env)\n```\n\nOnce OPAM is installed, you need to install\nthe OCaml frontend reason, and the build system dune, as well as\nthe pfff library (just for its commons/ sub-library):\n\n```bash\nopam install reason\nopam install dune\nopam install pfff\n```\n\nThen you can compile the program with:\n\n```bash\ndune build\n```\n\n## Run\n\nThen to test on a file, for example `tests/arithmetic/grammar.json`\nrun:\n\n```bash\n./_build/default/bin/main_codegen.exe -parse_grammar tests/arithmetic/grammar.json\n...\n```\n\n## Development Environment\n\nYou can use Visual Studio Code (vscode) to edit the code.\nThe [reason-vscode](https://marketplace.visualstudio.com/items?itemName=jaredly.reason-vscode) Marketplace extension adds support for OCaml/Reason.\n\nThe OCaml and Reason IDE extension by David Morrison is another valid\nextension, but it seems not as actively maintained as reason-vscode.\n\nThe source contains also a .vscode/ directory at its root\ncontaining a task file to automatically build the code from vscode.\n\nNote that dune and ocamlmerlin must be in your PATH for vscode to correctly\nbuild and provide cross-reference on the code. In case of problems, do:\n\n```bash\ncd /path/to/here\neval $(opam env)\ndune        --version # just checking dune is in your PATH\nocamlmerlin -version  # just checking ocamlmerlin is in your PATH\ncode .\n```\n\n## Debugging code\n\nSet the `OCAMLRUNPARAM` environment variable to `b` for backtrace. \nYou will get better backtrace information when an exception is thrown.\n\n```bash\nexport OCAMLRUNPARAM=b\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemgrep%2Fproject-reason-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsemgrep%2Fproject-reason-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemgrep%2Fproject-reason-tree-sitter/lists"}