Skip to content

This repo is about the project named Lang-Craft as a part of SRA_eklavya mentorship programme. This project aims at making an interpreter for our custom language.

Notifications You must be signed in to change notification settings

siddhip2004/Lang-craft_eklavya23

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangCraft

  • 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.

What is an INTERPRETER ?

  • 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.

What is the difference between an Interpreter and a Compiler ?

  • 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.

Table of contents


About the Project

Creating a custom Programming Language.

It involves the following stages :

  • Lexical Analysis (Lexing)
  • Parsing (Syntax Analysis)
  • Tree Walk Interpreter

Implementation details of an Interpreter

Lexical Analysis

  • 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.

LEXER

Significance of Lexical Analysis

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.

Parsing

  • 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.

PARSER

Tree Walk Interpreter

  • 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

INTERPRETER

Language Reference

Refer to this language reference - CyPy

MakeFile

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


Build

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

Contributors

Acknowledgements and Resources

  • 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.

About

This repo is about the project named Lang-Craft as a part of SRA_eklavya mentorship programme. This project aims at making an interpreter for our custom language.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published