-
The main aim of this project is to create an interpreter for a custom programming language from scratch.
-
It will involve defining the syntax of the language, implementing a lexer, developing a parser to construct an AST, and building a tree-walk interpreter to execute the code written in the custom language.
-
This Repo is inspired from Crafting Interpreters and all codes have been implemented chapterwise as given in the book.
- An interpreter is a program that directly executes the instructions in a high-level language, without converting it into machine code.
- It does not generate any intermediate object code. Hence it is memory efficient.
-
A Compiler converts a high-level language to a low-level language whereas, an Interpreter translates code written in a high-level programming language into machine code line-by-line as the code runs.
-
Interpreters usually take less amount of time to analyze the source code. However, the overall execution time is comparatively slower than compilers.
Creating a custom Programming Language.
It involves the following stages :
- Lexical Analysis (Lexing)
- Parsing (Syntax Analysis)
- Tree Walk Interpreter
-
Also known as tokenization, it converts a sequence of characters (string) to lexemes.
-
These lexemes pass through the lexer and it gives us tokens.
-
These tokens are then sent forward to use in parsing.
-
These tokens are defined in TokenType.h.
The lexical analyzer is responsible for breaking these syntaxes into a series of tokens, by removing whitespace in the source code. If the lexical analyzer gets any invalid token, it generates an error. The stream of character is read by it and it seeks the legal tokens, and then the data is passed to the syntax analyzer, when it is asked for.
-
The tokens we get after lexing, are passed through the Parser.
-
On receiving the tokens, it forms a Parse Tree using it.
-
This parse tree is simplified, removing all the extra, syntactic stuff, to give the AST (Abstract Syntax tree).
-
Thus, we can say, AST is a compact version of the parse tree.
-
In parse tree, after getting rid of the extra comment or other syntactic stuff is present it is passed through AST.
So the conversion of the tokens to AST is called Parsing.
- The Parser is defined in Parser.h.
-
After this step, we do semantic parsing whose result is an abstract syntax tree.
-
AST is a tree data structure that stores various tokens as its nodes, such that it can abstractly represent the code in memory.
AST is what represents our language in memory, the program for generating an AST is given in GenerateAst.cpp
- The next step is to execute this syntax tree by recursively transversing it.
Here we implement the evaluation logic for each kind of expression that we can parse - Interpreter.h
Refer to this language reference - CyPy
Here we will use MakeFile to run and compile our programs more efficiently. It defines set of tasks to be executed resulting in a final executable binary - Makefile
git clone https://github.com/siddhip2004/Lang-craft_eklavya23.git
cd Chapter9
-
Run make or make cypy to compile the program.
-
Run make generate_ast to compile GenerateAst.cpp.
-
Run make ast_printer to compile AstPrinterDriver.cpp.
-
To enter interactive mode run ./cypy
-
Individual Tests (test-X.cypy) can be tested with make test-X and check the output with test-X.cypy.expected
-
If you want to run all tests - make test-all
- Thanks to SRA-VJTI for providing this wonderful opportunity in Eklavya 2023.
- Special thanks to our mentor Khushi Balia, Lakshaya Singhal and Prit Kanadiya for their guidance throughout this project.