Here, you will find the code and slides for the parser we will be discussing during my presentation.
-
Open a terminal (or a git bash on Windows).
-
Use
cd
to navigate to a directory where you would like to create theLogik_Parser
folder. -
Run
git clone https://github.com/Louis-Le-Grand/Logik_Parser.git
. -
Run
cd Logik_Parser
-
Open the Code in your favorite editor (I recommend VSCode).
The Code is written in OCaml. To run the code, you will need to install OCaml and the OCaml Package Manager (opam). You can find the instructions here.
The code is writen in Version 5.1.0~alpha1 of OCaml. You can check your version by running ocaml -version
in your terminal. If you have any difiiculties running the code, you may want to install this version.
opam update
And then
eval $(opam env --switch=5.1.0~alpha1)
In Toplevel language, you can define types. For example, you can define types Term
and Formula
as shown in slides\00.ml
.
For this presentation, we will use Algebraic Data Types, which consist of Variballes, Constants and Function Symbols (Addition and Multiplication). Defining this type in OCaml is done as follows:
type expression =
| Var of string
| Const of string
| Add of expression * expression
| Mul of expression * expression
The first step of parsing is lexing. Lexing is the process of transforming a string into a list of tokens. For example, the string x + y
would be transformed into the list [alphanumeric "x"; space " "; symbolic "+"; space " "; alphanumeric "y"]
.
After you have split your string into tokens, you can parse it. Parsing is the process of transforming a list of tokens into a tree. For example, the list [alphanumeric "x"; space " "; symbolic "+"; space " "; alphanumeric "y"]
would be transformed into the tree Add (Var "x", Var "y")
.
If you want to return readable output, you can use prettyprinting.
Prettyprinting is the process of transforming a tree into a string. For example, the tree Add (Var "x", Var "y")
would be transformed into the string x + y
.