A Java-based interpreter that parses and executes a subset of Python's tuple syntax, built with ANTLR 3.5.2.
This project implements a complete parsing and execution pipeline for a simplified Python-like language focused on tuple operations. It uses ANTLR to generate a lexer and parser from a formal grammar, with embedded semantic actions that execute statements at parse time.
- Tuple creation:
X = ('hello', 'world', 42) - Tuple slicing:
Y = X[:2]andZ = X[2:] - Tuple concatenation:
A = Y + Z - Tuple unpacking:
(A, B, C) = X - Element access/update:
X[0] = 'new value' - String and integer variables:
A = 'text'orA = 100 - Print statements:
print(X) - Comments:
// single-line comments
- Grammar.g defines the lexical and syntactic rules using ANTLR grammar notation
- ANTLR generates
GrammarLexer.javaandGrammarParser.javafrom the grammar - ActionRoutines.java provides the runtime, storing tuples and variables in HashMaps and executing operations like creation, slicing, concatenation, and unpacking
- TestT.java reads
input.txtand drives the lexer/parser to interpret the program
- Java JDK
- ANTLR 3.5.2 (included as
antlr-3.5.2-complete.jar)
On Windows, run the included batch script:
run.batOr manually:
java -cp ".;antlr-3.5.2-complete.jar" org.antlr.Tool Grammar.g
javac -cp ".;antlr-3.5.2-complete.jar" *.java
java -cp ".;antlr-3.5.2-complete.jar" TestTOn Linux/macOS, replace ; with : in the classpath separator.
Given input.txt:
X = ('This is', 'the first', 'EECE', 334, 'Assignment')
print(X)
Y = X[:2]
print(Y)
Z = X[2:]
print(Z)
A = Y + Z
print(A)
The interpreter outputs each tuple's contents as it encounters each print statement.