A fully-featured Pascal interpreter written in Python that supports variables, procedures, and arithmetic operations.
- Lexical Analysis: Tokenizes Pascal source code
- Parsing: Builds an Abstract Syntax Tree (AST) from tokens
- Semantic Analysis: Validates variable declarations and scoping rules
- Interpretation: Executes the AST with call stack management
- Variable declarations (INTEGER and REAL types)
- Procedure definitions and calls
- Formal and actual parameters
- Nested scopes
- Arithmetic operations:
+,-,*,/,DIV - Unary operations:
+,- - Comments:
{ comment text }
main.py- Entry point and command-line interfacelexer.py- Tokenization moduletokens.py- Token type definitionsparser.py- Parser implementationast_nodes.py- AST node classessemantic_analyzer.py- Semantic analysis and symbol table managementinterpreter.py- AST interpreter and execution engineerrors.py- Error definitionsdemo.pas- Sample Pascal program
python main.py <pascal_file>--scope- Print scope information during execution--stack- Print call stack during execution
python main.py demo.pasPROGRAM Demo;
VAR
x, y: INTEGER;
a: REAL;
PROCEDURE Add(num1, num2: INTEGER);
BEGIN
END;
BEGIN
x := 10;
y := 20;
a := 5.5;
Add(x, y)
END.