{"id":25526300,"url":"https://github.com/teixdavide/FEUP_PFL_Parser","last_synced_at":"2026-01-05T21:30:15.628Z","repository":{"id":219504585,"uuid":"727834053","full_name":"Davide64-dev/FEUP_PFL_Parser","owner":"Davide64-dev","description":"λ | Low-level machine interpreter and compiler project, including stack management, parsing, and compiling imperative programs","archived":false,"fork":false,"pushed_at":"2024-04-17T12:21:32.000Z","size":360,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-06T01:25:35.850Z","etag":null,"topics":["assembler","compiler","feup","functional-parsing","functional-programming","parser","pfl"],"latest_commit_sha":null,"homepage":"","language":"Haskell","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/Davide64-dev.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}},"created_at":"2023-12-05T17:07:29.000Z","updated_at":"2024-04-19T21:09:40.000Z","dependencies_parsed_at":"2024-01-27T23:42:20.012Z","dependency_job_id":null,"html_url":"https://github.com/Davide64-dev/FEUP_PFL_Parser","commit_stats":null,"previous_names":["davide64-dev/feup_pfl_parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Davide64-dev%2FFEUP_PFL_Parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Davide64-dev%2FFEUP_PFL_Parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Davide64-dev%2FFEUP_PFL_Parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Davide64-dev%2FFEUP_PFL_Parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Davide64-dev","download_url":"https://codeload.github.com/Davide64-dev/FEUP_PFL_Parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239735249,"owners_count":19688262,"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":["assembler","compiler","feup","functional-parsing","functional-programming","parser","pfl"],"created_at":"2025-02-19T21:17:18.092Z","updated_at":"2026-01-05T21:30:15.554Z","avatar_url":"https://github.com/Davide64-dev.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Low Level Interpreter and a Compiler in a Functional Programming Language\n\n## Group T09_G07\n- Davide Pinto Teixeira - up202109860\n- Maria Abreu da Costa - up202108849\n\n## Instalation and Execution\nTo run the project, download the PFL_TP2_T09_G07.zip file and unzip it. Inside the 'src' directory, use the following commands to compile the code:\n\n```\nghc -package parsec main.hs\n./main\n```\n\nWhen in the ghci:\n\n```\n:set -package parsec\n:l main.hs\n```\n\n## Project's Description and Implementation\n\n### First Part\nIn the initial phase of the project, we were tasked with defining and implementing various components of a low-level machine along with its interpreter. This machine operates on an instruction set, evaluation stack, and storage. The instruction set encompasses arithmetic and boolean operations. Additionally, we implemented instructions that facilitate modifications to the stack and control flow, such as loops and branches.\n\n#### Implementation\nTo facilitate the implementation, we began by defining essential data types. The NumberOrBool type was introduced to represent values that could be either boolean or integer. Additionally, the Stack type was established as a list composed of elements with the NumberOrBool type. Finally, the State type was defined as a list of pairs, each consisting of a string and its associated NumberOrBool value.\nSubsequently, we developed several essential functions to manage the state and stack components.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I1.png\" /\u003e\n\u003c/p\u003e\n\nThese included functions to create an empty stack (createEmptyStack) and an empty state (createEmptyState). Additionally, functions like stack2Str and state2Str were implemented to convert the stack and state to strings, respectively. For effective value manipulation, functions such as findValueState and checkKeyExists were introduced to retrieve values associated with variables and check the existence of variables in the state, respectively. Furthermore, functions to add pairs to the state (addValueState), replace a value in the state (replaceValueState), and add or update values in the state (addOrUpdateValue) were also defined.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I2.png\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I3.png\" /\u003e\n\u003c/p\u003e\n\nFinally, the interpreter (run function) was designed to process a list of instructions, ensuring the stack and state were updated appropriately. This function handles the operations specified in the project description. It returns the modified code, stack, and state. In the event of an error, the function raises a runtime error.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I4.png\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I5.png\" /\u003e\n\u003c/p\u003e\n\n\n### Second Part\nIn the final phase of the project, we implemented the compiler to convert statements into machine instructions. Additionally, the parser was developed to facilitate the transformation of imperative programs, represented as strings, into a structured representation utilizing the defined data types.\n\n#### Implementation\nInitially, we established data types to represent arithmetic expressions (Aexp), boolean expressions (Bexp), and statements (Stm).\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I6.png\" /\u003e\n\u003c/p\u003e\n\nFollowing that, we defined the lexer (lexer). This module is tasked with breaking down the input string into individual tokens, facilitating the parsing process.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I7.png\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I8.png\" /\u003e\n\u003c/p\u003e\n\nSubsquently, we formulated the parser functions designed to parse distinct statements and operations, which lead to the creation of the primary parser function (parse).\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I9.png\" /\u003e\n\u003c/p\u003e\n\nFinally, we defined the compilers which are responsible to handle the translation of statements (compStm) and arithmetic (compA) and boolean (compB) expressions into the machine instructions, as mentioned in the project's description.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"img/I10.png\" /\u003e\n\u003c/p\u003e\n\n\n\n## Conclusions\nIn this project, we successfully designed a compiler. We began with the development of a lexer, followed by parsing, and concluding with code generation for machine instructions. The main challenge lay in formulating an effective lexer to facilitate subsequent parsing of instructions. Overall, the project provided a better understanding of the concepts covered in class.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteixdavide%2FFEUP_PFL_Parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteixdavide%2FFEUP_PFL_Parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteixdavide%2FFEUP_PFL_Parser/lists"}