This project implements a simple interpreter for the LanGU programming language. The interpreter is built in Python and is divided into several modules:
- Lexer: Tokenizes the source code and the
Tokenizer()is used to get print the grammar. - Parser & AST Tree: Parses tokens according to the language grammar and builds an Abstract Syntax Tree (AST).
- Semantic Analysis: Checks the AST for semantic correctness.
- Interpreter: Evaluates the AST and produces the program's output, with support for both direct interpretation and step-by-step execution.
Below is the EBNF grammar for the LanGU programming language:
<program> -> "program" <statements> "end_program"
<statements> -> { <statement> }
<statement> -> <assignment> | <if_statement> | <loop_statement> | <print_statement>
<assignment> -> <var> "=" <expr> ";"
<if_statement> -> "if" "(" <logic_expr> ")" <statements> "end_if"
<loop_statement> -> "loop" "(" <var> "=" (<INT_LIT> | <var>) ":" (<INT_LIT> | <var>) ")" <statements> "end_loop"
<print_statement> -> "print" "(" <expr> ")" ";"
<logic_expr> -> <rel_expr> { ("&&" | "||") <rel_expr> } | "(" <logic_expr> ")"
<rel_expr> -> <expr> <rel_op> <expr>
<rel_op> -> "==" | "!=" | ">" | "<" | ">=" | "<="
<expr> -> <term> { ("+" | "-") <term> }
<term> -> <factor> { ("*" | "/" | "%") <factor> }
<factor> -> "-" <factor> | <var> | <INT_LIT> | <STRING_LIT> | "(" <expr> ")"
<var> -> <IDENT>
- lexer.py: Contains token definitions and a lexical analyzer that converts source code into tokens.
- parser.py: Implements the grammar rules and builds the AST.
- AST_Tree.py: Defines the AST node structure and provides utilities for printing the AST.
- semantics.py: Performs semantic analysis on the AST, checking for issues such as undeclared or unused variables.
- Interpreter.py: Evaluates the AST to execute the program. It also supports step-by-step interpretation.
- LanGU.py: Provides the GUI.
- program1.txt / program2.txt: Sample programs for testing.
Requirements
- Python
- Download the EXE file
-
Organize Files:
Save all the following files into a single folder:AST_Tree.pyInterpreter.pyLanGU.pyparser.pyprogram1.txtprogram2.txtsemantics.py
-
Open the Project:
Open theLanGU.pyfile in your preferred IDE (e.g., VSCode) or run it directly from the command line. -
Use the GUI:
Once the GUI launches, you can:- Enter your own code manually.
- Click the Program 1 or Program 2 buttons to load sample programs.
- Use the other buttons to step through or run the interpretation process.
Requirements
- Pyinstaller: Download with <pip install pyinstaller> in the terminal
-
Organize Files:
Save all the following files into a single folder:AST_Tree.pyInterpreter.pyLanGU.pyparser.pyprogram1.txtprogram2.txtsemantics.py
-
Compile the EXE
- Windows --> open a terminal in the folder, run
<pyinstaller --onefile --windowed --add-data "program1.txt;." --add-data "program2.txt;." LanGU.py>
- Linux/MacOS --> open a terminal in the folder, run
<pyinstaller --onefile --windowed --add-data "program1.txt:." --add-data "program2.txt:." LanGU.py>- Note: The --windowed flag may hide the console on MacOS
- Windows --> open a terminal in the folder, run
-
Run the File
- Navigate to the dist folder
- Run LanGU
This project is licensed under the MIT License.
#Copyright (c) [2025] [Crenta] [All rights reserved].
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY [Crenta] “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL [Crenta] BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-Crenta