straightline-ocaml

所属分类:人工智能/神经网络/深度学习
开发工具:OCaml
文件大小:0KB
下载次数:0
上传日期:2022-03-28 14:14:25
上 传 者sh-1993
说明:  《Moden编译器ML实现》一书中的玩具编程语言,
(Toy programming language from the book Moden Compiler Implementation in ML,)

文件列表:
LICENSE (1080, 2019-10-09)
prog1.sl (52, 2019-10-09)
shell.nix (475, 2019-10-09)
src/ (0, 2019-10-09)
src/absyn.ml (297, 2019-10-09)
src/absyntree.ml (1517, 2019-10-09)
src/box.ml (4960, 2019-10-09)
src/driver.ml (1528, 2019-10-09)
src/dune (378, 2019-10-09)
src/error.ml (754, 2019-10-09)
src/error.mli (437, 2019-10-09)
src/interpreter.ml (971, 2019-10-09)
src/lexer.mll (1626, 2019-10-09)
src/location.ml (2465, 2019-10-09)
src/maxargs.ml (523, 2019-10-09)
src/option.ml (1226, 2019-10-09)
src/option.mli (260, 2019-10-09)
src/parser.mly (1214, 2019-10-09)
src/scanner_adhoc.ml (2667, 2019-10-09)
src/test1.ml (714, 2019-10-09)
src/tree.ml (2664, 2019-10-09)
src/util.ml (102, 2019-10-09)

# The straightline programming language **Straightline** is a micro programming language used in the book series [Modern Compiler Implementation](http://www.cs.princeton.edu/~appel/modern/) by Andrew Appel. # Working with git repositories ## Initial setup to work with the straightline-ocaml project in BCC328 In order to develop the activities of the BCC328 (Compiler Construction 1) course you should: - Have a [github](https://github.com/) account. If you do not have one, visit the github site and [sign up](https://github.com/join). - [Log in](https://github.com/login) to github. - Visit the main [straightline-ocaml](https://github.com/romildo/straightline-ocaml) project page. - Fork the straightline-ocaml project. For that use the `Fork` buttom at the top right of the project page. ![forking](images/fork1.png) This will create your own copy of the main repository. It can be freely modified by you. But probably you do not have permission to modify the main repository. - In your computer clone your fork of the straightline-ocaml project. Notice that in the commands that follow any text written between angular brackets `<>` shold be replaced by more appropriate text, according to your setup. For instance ` $ git clone https://github.com//straightline-ocaml.git $ cd straightline-ocaml ``` - Set the the upstream remote repository for your clone. The upstream remote repository is the main repository of the project from which the fork was made. ``` $ git remote add upstream https://github.com/romildo/straightline-ocaml.git ``` All remote repositories can be listed with the following command: ``` $ git remote -v ``` Similar steps can also be followed when starting other github based projects on the course. The above commands were presented to you using the command line. There are other ways to accomplish them, though. For instance they may be integrated in development environments like [IntelliJ IDEA](https://www.jetbrains.com/idea), [Eclipse](https://www.eclipse.org), [Atom](https://atom.io/) and [Visual Studio SCode](https://code.visualstudio.com/). ## When testing a version of the straightline compiler - Change your working directory to the folder containing your clone. ``` $ cd /straightline-ocaml ``` - Select the master branch of the clone of your forked project. ``` $ git branch $ git checkout master ``` - Pull the latest changes from the remote repository. ``` $ git pull upstream master ``` - Create and select the appropriate branch for the activity. ``` $ git checkout -b ``` - Use the project code to do whatever is needed. ## To submit an activity - Select the master branch of the clone of your forked project. ``` $ cd /straightline-ocaml $ git checkout master ``` - Pull the latest changes from the remote repository. ``` $ git pull upstream master ``` - Create a new branch where you will develop the activity. ``` $ git checkout -b ``` - Develop the activity. - Check the status of your cloned repository: ``` git status ``` This command list new and modified files. - Add any new or modified file of interest to the revision history: ``` git add ``` - Commit the changes: ``` git commit -m ``` - Push your changes to your forked project. ``` git push origin ``` - Make a pull request (PR) from your forked project at github. # Compiling with OCaml ## Needed development tools In order to prepare your application for execution the following packages may be needed: - [OCaml compiler](http://www.ocaml.org), - findlib (`ocaml-findlib`), - [Camomile](https://github.com/yoriyuki/Camomile), a Unicode library for OCaml (`libcamomile-ocaml-dev`) - [Dune](https://dune.build), a composable build system for OCaml ## To remove the generated files With `dune`: ``` $ dune clean ``` ## To compile the project With `dune`: ``` $ dune build src/driver.exe ``` ## To run the executable With `dune`: ``` $ dune exec src/driver.exe ``` # Grammar - The syntax of the language is given by a context free grammar. - Only the production rules are explicitly given. - The sets of terminals and non-terminals are obtained from the rules. - The initial symbol is the non-terminal on the left side of the first production rule. Production rule | Internal representation ----------------------------------|------------------------- _Stm_ → _Stm_ `;` _Stm_ | CompoundStm _Stm_ → `id` `:=` _Exp_ | AssignStm _Stm_ → `print` `(` _ExpList_ `)` | PrintStm _Exp_ → `id` | IdExp _Exp_ → `num` | NumExp _Exp_ → _Exp_ _Binop_ _Exp_ | OpExp _Exp_ → `(` _Stm_ `,` _Exp_ `)` | EseqExp _ExpList_ → _Exp_ | LastExpList _ExpList_ → _Exp_ `,` _ExpList_ | PairExpList _Binop_ → `+` | Plus _Binop_ → `-` | Minus _Binop_ → `*` | Times _Binop_ → `/` | Div # Example ``` a := 5 + 3; b := ( print(a, a - 1), 10*a); print(b) ``` ![ast](images/ast.png) ## General guidelines for building the AST - There is an **algebraic data type** for each interesting non terminal (representing a kind of phrase in the source language). - There is a **data constructor** for each production rule for that non terminal (representing a particular form for the phrase). For instance, the _Stm_ non terminal represents statements. So there is a type, named `stm` to represent all forms of statements. As there are three forms of statements, for each one there is a data constructor of `stm`: - compound statements, represented by the `Compoundstm` constructor - assignment statements, represented by the `AssignStm` constructor - print statements, represented by the `PrintStm` constructor # Activities ## Building an abstract syntax tree Add a `Test` module to the project, with a `main` function that, when applied, creates the AST for the program given above, and prints it. As an example, below is the AST for the program: ``` x := 2 + 3 * 4; print(x) ``` ``` ocaml let prog1 = CompoundStm (AssignStm ("x", OpExp (NumExp 2, Plus, OpExp (NumExp 3, Times, NumExp 4))), PrintStm [IdExp "x"]) ``` ## Pretty printing the AST ## Calculating the maximum number of arguments in print statements Define a function `maxargs` to calculate the maximum number of arguments in print statements ocurring in a given straightline program. It should receive a statement and returs an integer. Test with the AST you have created above. ## Interpreting Add a method `interp` to run a program. It should have a `(string, int) Hashtbl.t` representing the memory, and a statement as arguments, and run the given program using the given memory. The memory is a hash table where the keys are variable names, and the associated values are the value of the variable. Test with the AST you have created above. ## Write test units for the project # Assignment (Trabalho)

近期下载者

相关文件


收藏者