{"id":18627336,"url":"https://github.com/dizys/pl-homework-1","last_synced_at":"2025-11-04T01:30:35.039Z","repository":{"id":96764709,"uuid":"338501620","full_name":"dizys/pl-homework-1","owner":"dizys","description":"NYU programming languages homework 1 project: build an EBNF-to-BNF Bison rule translator using Flex and Bison.","archived":false,"fork":false,"pushed_at":"2021-02-16T01:56:39.000Z","size":81,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-20T20:03:12.676Z","etag":null,"topics":["bison","bnf","conversion","ebnf","flex","nyu","programming-languages","translator"],"latest_commit_sha":null,"homepage":"","language":"C++","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/dizys.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":"2021-02-13T05:07:38.000Z","updated_at":"2021-10-24T04:00:17.000Z","dependencies_parsed_at":"2023-03-16T18:00:44.283Z","dependency_job_id":null,"html_url":"https://github.com/dizys/pl-homework-1","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizys%2Fpl-homework-1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizys%2Fpl-homework-1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizys%2Fpl-homework-1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizys%2Fpl-homework-1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dizys","download_url":"https://codeload.github.com/dizys/pl-homework-1/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239425311,"owners_count":19636346,"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":["bison","bnf","conversion","ebnf","flex","nyu","programming-languages","translator"],"created_at":"2024-11-07T04:41:58.865Z","updated_at":"2025-11-04T01:30:35.010Z","avatar_url":"https://github.com/dizys.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Programming Languages Homework 1\n\nAn EBNF-to-BNF Bison rule translator built using Flex and Bison. Actions\nare completely ignored during translation.\n\nSupported extended expressions:\n- `\u003cA\u003e` or `A*`: zero or more occurrences of A\n- `A+`: one or more occurrences of A\n- `[A]`: zero or one occurrences of A\n- `(A||B)`: grouping, in which an `or` operator should appear as `||` instead of a single `|`\n\n## 1. Project structure\n\n```\nproject\n├─.vscode\n│  └─settings.json                  VSCode workspace settings\n│\n├─dist                          Build artifacts (created after build)\n│  ├─bin\n│  │  └─trans                           Executable binary of translator\n│  │\n│  ├─c_src                          Folder to hold generated C codes\n│  │  ├─trans.tab.c                     Parser C code generated by Bison\n│  │  ├─trans.tab.h                     Parser C header generated by Bison\n│  │  └─trans.yy.c                      Tokenizer C code generated by Flex\n│  │\n│  └─package                        Folder to hold package for submission\n│\n├─examples                      Bison-with-EBNF Examples used to test the translator\n│  └─...\n│\n├─src                           Translater source folder\n│  ├─trans.l                        Tokenizer source in Flex\n│  ├─trans.y                        Parser source in Bison\n│  ├─treenode.cpp                   AST treenode source in C++\n│  └─treenode.h                     AST treenode header file\n│\n├─load_modules.sh               Shell script for fast loading Flex and Bison\n├─make.sh                       Shell script for easily building the translator\n├─package.sh                       Shell script for building package for submission\n├─run.sh                Shell script for easily running examples\n│\n...\n```\n\n## 2. Build\n\nThis project requires Bison 3.5+ and Flex 2.5+ to build. It's developed with \nBison 3.7.1 and Flex 2.6.4.\n\nTo build the translator, simply run the following shell script via CLI:\n\n```sh\n./make.sh\n```\n\nThen, the executable binary should be available at `dist/bin/trans`\n\n## 3. Usage\n\n### 3.1 `dist/bin/trans`\n\nWhen running, the translator binary reads from standard input (`stdin`) and\noutput its translation to standard output (`stdout`).\n\n#### 3.1.1 Direct input and output (not recommended)\n\nYou can type all the content that needs to be translated and end it with\n`Ctrl+D`. But as is shown by the following example, the output would be\nmixed up with the input, which is messy.\n\n```\n$ ./dist/bin/trans\n%%\na: digit+;\n%%\n\n%%\n\na:\n    a_quant\n  ;\n\na_quant:\n    digit\n  |\n    digit a_quant\n  ;\n\n%%\n```\n\n#### 3.1.2 Input file content through pipe (recommended)\n\n```\n$ cat examples/cal1.y | ./dist/bin/trans\n```\n\n#### 3.1.3 Redirect output to file (recommended)\n\n```\n$ cat example/cal1.y | ./dist/bin/trans \u003e example/cal1.out.y\n```\n\n## 3.2 Example runner / helper script: `run.sh`\n\nThis project includes 7 example bison source files, 2 with BNF rules\n(`cal1.y`, `cal2.y`), the others with EBNF rules (`test1.y`, `test2.y`,\n`test3.y`, `test4.y`, `test5.y`).\n\nTo translate an example file or any file, you can try:\n\n```\n$ ./run.sh ./examples/test1.y\n```\n\nOutput translation result to a file:\n\n```\n$ ./run.sh ./examples/test1.y \u003e ./examples/test1.out.y\n```\n\n`./examples/test1.y` could be replaced with any path to a valid Bison file\nwith BNF/EBNF rules.\n\n## 4. Limitations\n\n### 4.1 Actions are ignored\n\nBecause new productions could be generated during translation, and it's\ndifficult to match original actions to new productions, actions are\ncompletely ignored during translation, and the translation result would\ncontain no action code blocks.\n\n### 4.2 Nested blocks in actions are not supported\n\nBecause it's impossible to represent the recursive structure of nested\nblocks using a single standard regular expression, action block with only\none level of matching curly brackets can be matched by the regular\nexpression. A possible solution would be further defining C/C++ tokens and\ngrammar structure in Flex and Bison. But that would be out of scope for\nthis project.\n\nFor example, the following file would throw an error when being translated.\n\n```bison\n%%\n\ndigit:\n    '2' {\n      if (flag) {\n        flag = 0;\n        $$ = new node($1);\n      } // if block cannot be matched\n    }\n  |\n    '3'\n  ;\n\n%%\n```\n\n### 4.3 Generated rules are not optimal\n\nThe generated rules are correct BNF rules, but sometimes can be too verbose\nand even with meaningless nodes.\n\nFor example, a possible translation result could be as follow:\n\n```\n%%\ndigit:\n    digit_expr\n  ;\n\ndigit_expr:\n    'a'\n  |\n    'b'\n  ;\n%%\n```\n\nIn this example, non-terminal `digit_expr` is completely unnecessary as\n`digit_expr` here is just an alias for `digit`.\n\nThis is due to how the Bison rules are defined in this project to support\nEBNF . Because `digit_expr` could also be used inside, for example, a\nrepetition rule like `number -\u003e digit_expr+` where `digit_expr` is a\nnecessary non-terminal for it to be translated into BNF rule\n`number -\u003e digit_expr | digit_expr number`.\n\nOptimizations can be done to eliminate redundancies like this. For example,\nby visiting the AST, find out rules like `A -\u003e B` where `B` is only used\nhere. Then just delete this rule and rename `B` as `A`. Unfortunately due\nto time limit, this has not been implemented yet.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdizys%2Fpl-homework-1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdizys%2Fpl-homework-1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdizys%2Fpl-homework-1/lists"}