{"id":15061094,"url":"https://github.com/mohcineproject/expr-compiler-and-pfx-virtual-machine","last_synced_at":"2026-01-02T08:08:25.144Z","repository":{"id":247255865,"uuid":"810044833","full_name":"MohcineProject/Expr-compiler-and-pfx-virtual-machine","owner":"MohcineProject","description":"An academic project where we developed an Expr compiler (a simple arithmetic language with support for lambda calculus, altough this last part is not yet fully implemented)  and a Pfx virtual machine (a simple stack low level language).","archived":false,"fork":false,"pushed_at":"2024-07-07T18:17:04.000Z","size":1186,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T09:13:37.656Z","etag":null,"topics":["dune","menhir","ocaml","ocamllex"],"latest_commit_sha":null,"homepage":"","language":"OCaml","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/MohcineProject.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":"2024-06-04T00:08:29.000Z","updated_at":"2024-07-07T18:17:08.000Z","dependencies_parsed_at":"2024-10-12T21:20:43.198Z","dependency_job_id":"d39b0a62-1741-40ea-b65b-7443272e16df","html_url":"https://github.com/MohcineProject/Expr-compiler-and-pfx-virtual-machine","commit_stats":null,"previous_names":["mohcineproject/expr-compiler-and-pfx-virtual-machine"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohcineProject%2FExpr-compiler-and-pfx-virtual-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohcineProject%2FExpr-compiler-and-pfx-virtual-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohcineProject%2FExpr-compiler-and-pfx-virtual-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohcineProject%2FExpr-compiler-and-pfx-virtual-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MohcineProject","download_url":"https://codeload.github.com/MohcineProject/Expr-compiler-and-pfx-virtual-machine/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243707382,"owners_count":20334619,"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":["dune","menhir","ocaml","ocamllex"],"created_at":"2024-09-24T23:09:10.937Z","updated_at":"2026-01-02T08:08:25.099Z","avatar_url":"https://github.com/MohcineProject.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"AUTHORS\n-------\n\n- Zahdi Mohcine\n- Pham Hai-Nguyen\n\n===============\n\nDescription of the project\n--------------------------\n\nThe project is about building a tool that can understand simple math equations and eventually handle \n\nmore complicated ones (functions). Instead of directly translating the equations into the language \n\nthat computers understand, we first translate them into a simpler language that computers can work \n\nwith easily. Then, we run them on a virtual machine. We use two languages for this project: Expr and \n\nPfx. We wrote explanations about how everything works in a document using LaTeX. For the coding part,\n\nwe tackled basic math operations (the functional part is not working correctly) and checked if they \n\nwork correctly using Dune. The code is organized neatly into folders (for the functions extensions \n\ncheck folders named 'fun' for both languages.)\n\n\n===============\n\nSources\n-------\n\nGit repository: https://github.com/MohcineProject/Expr-Compiler-.git\n\n(We did not use the git, we usually worked together in the same pc, thinking about the solution and implementing it)\n\n===============\n\nHow to…\n-------\n\n…retrieve the sources?\n\n  git clone https://github.com/MohcineProject/Expr-compiler-and-pfx-virtual-machine.git\n\n...compile and execute?\n\nFor the PFX programs (involving simple stack operations without functions), you can compile and execute programs written in the `ok_prog.pfx` file. You can modify the PFX code inside this file and pass it to the virtual machine by running the following command:\n\n    `dune exec pfx/pfxVM.exe -- pfx/basic/tests/ok_prog.pfx`\n\nFor Expr programs, to compile and execute them (again, with basic arithmetic operations), use the `an_example.expr` file. You can modify this file and write new programs in Expr.\n\n    `dune exec expr/compiler.exe -- expr/basic/tests/an_example.expr`\n\n\n===============\n\nStructure of the project\n------------------------\n\nThe project is organized as following:\n\n```\n├── dune-project\n├── expr\n│   ├── basic                      : This is the main folder for the expr arithmetic operations\n│   │   ├── ast.ml                 : This is the AST of the expr\n│   │   ├── ast.mli                : This is the interface of the AST    \n│   │   ├── dune                    \n│   │   ├── eval.ml                 \n│   │   ├── eval.mli               \n│   │   ├── lexer.mll              \n│   │   ├── parser.mly             \n│   │   ├── tests                  \n│   │   │   └── an_example.expr    : The test file we use for compilation testing\n│   │   ├── toPfx.ml               : This is where we coded the generate function to compile expr AST to pfx AST\n│   │   └── toPfx.mli              \n│   ├── common                      \n│   │   ├── binOp.ml\n│   │   ├── binOp.mli\n│   │   └── dune\n│   ├── compiler.ml                : The file used to compile expr to pfx and print the result of the valuation: it uses our generate function in 'toPfx.ml'\n│   ├── dune\n│   ├── fun                        : The directory used for the functions extension\n│   │   ├── ast.ml\n│   │   ├── ast.mli\n│   │   ├── lexer.mll\n│   │   ├── parser.mly\n│   │   ├── toPfx.ml               : Here we updated the generate function to handle functions compilation (not tested yet and it should still require some patches)\n│   │   └── toPfx.mli\n│   ├── main.ml                    : This file is used to execute expr programs directly using expr own parser and lexer\n│   └── README\n├── pfx\n│   ├── archive\n│   │   └── lexer1.mll\n│   ├── basic\n│   │   ├── ast.ml                 : The AST of the pfx \n│   │   ├── ast.mli\n│   │   ├── dune\n│   │   ├── eval.ml\n│   │   ├── eval.mli\n│   │   ├── lexer.mll\n│   │   ├── parser.mly\n│   │   ├── test_lexer.txt \n│   │   └── tests\n│   │       └── ok_prog.pfx        : The main test file for the pfx \n│   ├── dune\n│   ├── fun                        : The directory for the function extension\n│   │   ├── ast.ml\n│   │   ├── ast.mli\n│   │   ├── dune\n│   │   ├── eval.ml\n│   │   ├── eval.mli\n│   │   ├── lexer.mll\n│   │   ├── ok_prog.pfx            \n│   │   └── parser.mly\n│   ├── main.ml                    : A file used for compiling a pfx program and printing the tokens. Used mainly to test the lexer\n│   └── pfxVM.ml                   : the file containing the virtual machine the execute the pfx program using the parser and the lexer\n├── README\n└── utils \n    ├── dune\n    ├── location.ml                : A file used to determine the location of errors \n    └── location.mli\n\n```\n===============\n\nProgress\n--------\n\n- We stopped at question 12 (we did it)\n- There is still a bug in question 10.3 (new version of generate function)\n- And also in the extended lexer of pfx (more specifcally, there is a problem with a function to tokenize\n  sequence of commands)\n\n===============\n\nKnown bugs and issues\n--------------------\n\n- One difficulty that took us a lot of time is the new lexer of the pfx : \nWe tought about introducing seqences in the form of \"Push [ (Sequence)]\" But we found several problems \nin the lexer we could not define a proper regexp to catch nested Sequences as in \"Push [ ... Push [ ... ] ... ]\"\nand we were not able to create a function to translate this commands from string to AST element \n(to be more specific it is the function 'tokenize_command_str') \n\n- Another problem that we encountred is with the new generate function in the fun directory of expr. \nAltough we believe that the logic is correct according to our defined rules of transformation we \nthink that we did not handle correctly the recursive feature of the function to update the environment\nused to bind variables to their depths. \n\n\n\n===============\n\nHelpful resources\n-----------------\n\n- We used this manual to understand more deeply the regexp syntaxe used by ocamllex \n  \n  https://caml.inria.fr/pub/old_caml_site/ocaml/htmlman/manual026.html\n\n\n===============\n\nDifficulties\n------------\n\n- The project was really beautiful, we enjoyed the realm of functional paradigm, everything seemed coherent\n- The problem was not that it was difficult, i genuinely think it is easy. The problem was in understanding\nthe environment and the syntaxe (dune, regexp, the overall structure ...) they took a a lot of time to understand them. \n\n\nNotes on the code added : \n-------------------------\n\nExercice 4.1 : we added the type command in the mentionned directory pfx/basic/ast : it defines all the \nneeded operations used by the language. \n\nExercice 4.2 : we added the step function in the eval.ml file at the pfx/basic directory. It takes as \nargument too lists (one for command and one for the stack) and modify the stack according to the top \ncommand on the list. It deals as well with errors and returns error messages as defined in the semantics\nrules. \n\nExercice 5.2 : we added the function \"generate\" in the file toPfx.ml in the expr/basic directory. It defines\nhow an expr ast should be translated into pfx ast and it is used to transform whole expr programs by the \ncompiler (at the expr/ ).\n\nExercice 6.1 : we modified the lexer of the pfx and added the necessary rules to tokenize commands\n\nExercice 7   : we added the location of the error to the error message printed by the lexer using the \nlocation utility. \n\nExercice 8.1 : we added the pfx parser in the pfx/basic directory. The starting type is the \"program\"\nthat can contain the number of arguments followed by the second defined type \"command\" which is a sequence\nof the tird defined type \"operator\" that represents the pfx commands. \n\nExercice 8.2 : we coded the necessary functions to print the sequence of commands. They are located \nat the pfx/basic/ast.ml file. We used it on the pfx virtual machine to print the ast. (you can add\nthe following line of code \n        print_endline (BasicPfx.Ast.string_of_program pfx_prog);\nafter line 16 to apply the printing \n)\n\nExercice 9.3 : For this exercice, we introduced a new AST where the 'Push' command can contain either \nan integer or a list of commands, we defined both of them in a new type called 'element'. We added a \nsmall patch to the string of functions so that we can print these new constructs as well. \n\nwe extended the lexer and the parser of pfx in the fun directory. We added the new regexp\n'command' to tokenize the nested sequences. We intended first to detect expressions of type \n'Push [ ... Push [...] ... ]' in order to have a nested sequence inside another one. We fisrt tried \n\n'let command = \"Swap\"|\"Sub\"|\"Div\"|\"Mul\"|\"Rem\"|\"Add\"|\"Pop\" | \"Push\" blank+ \"[\" blank+ (command blank+)+ blank+ \"]\" '\n\nBut we found at the end that a nested regexp like this was not correct (i believe it was the lexer who gave us an error log that\nfinally located the problem )\n\nWe also added the function 'tokenize_command_str' that we intended to tokenize the sequence of commands (the \nargument of the Push for nested sequences ). However there was a problem in referencing the lexer, and we \nthink the implementation needs revising (in this version we returned the list of tokens, we should have returned the \nlist of commands types )\n\nWe added the detection of 'Exec' and 'Get' as well, we do not think there is a problem with that.\n\nFor the parser, we did not apply major changes, we just added the new operators and changed the constructor\nof the 'PUSH' token. \n\nExercice 10.3 : For this one, we coded a new function \"generate\" in a copy of the 'toPfx.ml' in the fun directory\nof expr. This new function uses an environment (called env) to store the binding between variables and \ntheir depths. We did not test the function yet, however we think there is an error in the way we matched\nthe const pattern (the idea was that whenever we use a constant, we need to push it and therefore \nwe need somehow to modify the env of the whole program: there are too issues with this approach we think\n, first, this fact is not totally accurate, morelikely the env should change with the binary operations\n,second, even if the method is correct, the way we update the environment seems wrong as the update\nis not transmitted to the rest of the ast recursively ), Nontheless, the rest seems to be correct.\n\nFutur: \n------\n\nSince the project was beautiful we look forward to complete it. It gaves us a comprehensive understanding of the functional paradigm and provided us with practical knowledge of compilers. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohcineproject%2Fexpr-compiler-and-pfx-virtual-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohcineproject%2Fexpr-compiler-and-pfx-virtual-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohcineproject%2Fexpr-compiler-and-pfx-virtual-machine/lists"}