back to projects

ADV - Automata Animation DSL Compiler

Compilers & Programming LanguagesANTLR4CompilerDSLPythonOpenCVcompleted2023

This project defines and implements ADV (Animation Description Language), a domain-specific language for declaratively constructing and animating finite automata, developed for a university Compilers course in a 6-person team. A user writes .adv source files describing an automaton's alphabet, states, and transitions; visual layouts (state placement, coordinate arithmetic, custom transition paths, grid overlays); and animation sequences (viewports, step-by-step state highlighting, pausing, and interactive word tracing). The Java compiler translates these files into self-contained Python scripts that render animated automata diagrams using OpenCV.

The compilation pipeline runs as four sequential ANTLR4 visitor passes over the parse tree, proceeding only if the source contains no syntax errors. The first pass, the semantic analyzer, performs full type checking and scope validation before any code is generated. It implements a 12-type system (int, float, coord, coordName, state, string, bool, color, line, automata, grid, animation, read) with scope-aware variable tracking across nested for-loop scopes, and an arithmetic compatibility matrix governing valid operations between coordinates, states, and numeric types (for example, coord plus coord yields coord, while coord times a scalar yields a scaled coord). It also enforces language-specific rules: transition letters must belong to the declared alphabet, NFAs must have at least one non-deterministic transition, labels accept at most two alignment keywords, colors and line styles must come from a fixed whitelist, and set literals must be type-homogeneous. Any violation reports the offending line number and halts compilation cleanly.

The second pass builds an in-memory automaton graph and verifies it against its declared type, checking DFA, complete DFA, and NFA properties (for example, a complete DFA must have a transition from every state for every alphabet symbol). The third pass resolves view and animation properties. The fourth and most complex pass, the code generator, is a 700+ line ANTLR visitor where every visit method returns a StringTemplate instance; the full parse tree traversal produces one nested template that renders to the complete Python output file. This pass tracks which automaton, view, or animation declaration is currently active, and makes context-sensitive code generation decisions, such as emitting a coordinate object with operator overloading versus a plain tuple depending on context, or treating an identifier as a Python loop variable versus a quoted string literal depending on whether it appears inside a for-loop over states.

The StringTemplate (StringTemplate4) grammar serves as the complete interface between the Java compiler and the Python runtime: it defines not only the syntax of each generated code fragment but also embeds a full Python runtime library directly into the output, including an immutable Point class with operator overloading and polar-to-Cartesian conversion, OpenCV-based figure classes for rendering automaton states (normal, initial, accepting, highlighted) and transitions (straight, looping, polyline with custom paths and labels), and Automaton, View, Viewport, and Animation classes that manage the automaton's runtime state, coordinate layout, rendering canvas, and step-by-step animation logic, including computing the automaton's transition function to advance the visualization as a user types an input word.

  • ·Built a semantic analyzer with a 12-type system, scope-aware variable tracking, and a full arithmetic compatibility matrix for coordinates, states, and scalars
  • ·Built a 700+ line ANTLR4 code generator with context-sensitive translation of every language construct into StringTemplate calls
  • ·Designed the StringTemplate grammar defining a self-contained Python/OpenCV runtime, including operator-overloaded coordinate types and animated automaton rendering
  • ·Delivered all minimum-level requirements and most desirable-level requirements, including bonus automaton animation and interactive string-tracing features

Core

JavaANTLR4StringTemplate4

Generated Runtime

PythonOpenCV

Concepts

Type SystemsSemantic AnalysisCode GenerationAutomata Theory
Sample automaton animation
Sample automaton animation