An Advanced BNF Syntax Analyzer with Recursive Backtracking, Static DFS Guard, Semantic Interpretation, and Visual Topology Generation.
Syntax Engine is a high-performance, library-free syntax analysis framework designed to process dynamic Backus-Naur Form (BNF) grammars with integrated semantic interpretation capabilities. Built entirely from foundational principles, it features a custom recursive descent parser, automated left-recursion detection, an AST-based interpreter, and graphical parse tree visualization.
- Zero-Dependency Parsing: A custom top-down Recursive Descent kernel built without external parser generator libraries (like NLTK or Lark).
- Static Left-Recursion Guard: Implements a pre-execution Depth-First Search (DFS) graph traversal algorithm to detect cyclic dependencies (e.g., A β B β A) and prevent infinite loops/stack overflow.
- Intelligent Backtracking: Accurate memory state management and token index restoration during failed non-deterministic derivation branches.
- Semantic Interpretation Engine: Built-in AST (Abstract Syntax Tree) evaluator for executing domain-specific commands such as NPC spawning and attribute assignment in game grammar contexts.
- Dual Lexical Analysis Modes: Flexible tokenization supporting both character-level and word-level lexical analysis with comprehensive error reporting.
- Visual Topology Integration: Automated generation of high-resolution parse tree topologies (PDF/PNG) using the Graphviz rendering engine.
- Semantic Anomaly Diagnostics: Goes beyond standard "Syntax Error" exceptions by providing highly contextual WHERE, WHAT, and WHY reports for derivation failures.
- Clean JSON Serialization: Actively strips linguistic formatting tags (
< >) to produce pristine, dictionary-based JSON derivation structures. - Dual Interfaces: Offers both a batch-testing Command Line Interface (
main_display.py) and a feature-rich multi-threaded Graphical User Interface (main_GUI.py) with real-time execution feedback.
Tokenizes input source code into a structured token stream.
Features:
- Token classification (NUMBER, STRING, IDENTIFIER, OPERATOR, PUNCTUATION, COMMENT, KEYWORD)
- Line and column tracking for precise error diagnostics
- Support for multi-line input with proper newline handling
- Automatic comment and whitespace filtering
Token Structure:
Token(type: str, value: str, line: int, column: int)Implements a top-down recursive descent parser with backtracking capabilities.
Key Methods:
parsing_rules(symbol)- Performs recursive descent parsing for a given non-terminal symboldetect_left_recursion()- Executes DFS-based cycle detection to prevent infinite recursionerror_check(start_symbol)- Generates contextual error diagnostics
Algorithm Details:
- Maintains parse state: current token index, maximum index reached, expected symbols at failure point
- Attempts production rules in order of decreasing length (longest-match-first strategy)
- Restores parser state on failed branches (intelligent backtracking)
Parses grammar files and sentence input with robust error handling.
Input Format:
- Grammar File: BNF format with
::=rule separator and|for alternatives - Sentences File: Line-separated inputs, supporting both space-delimited tokens and epsilon (Ξ΅) symbol
Evaluates parse trees to execute semantic actions and domain-specific operations.
Supported Operations:
- Spawn Action:
spawn <npc> at <zone>β Creates NPC instance at specified zone - State Action:
set <attribute> = <value>β Assigns values to NPC attributes with memory persistence
Output: Structured system messages and memory-based state tracking
Converts parse trees into both textual and graphical representations.
Output Formats:
- Plain-text indented tree structure
- PDF/PNG graphical topologies via Graphviz
- Entry-numbered outputs for batch processing
- Python: 3.8 or higher
- System Software: Graphviz Executable
β οΈ CRITICAL: During Graphviz installation, you MUST select "Add Graphviz to the system PATH for all users"
Install required packages using pip:
pip install customtkinter graphviz PillowPackage Descriptions:
customtkinter- Modern dark-themed GUI frameworkgraphviz- Python bindings for Graphviz rendering enginePillow- Image processing and display
git clone https://github.com/DioBey7/Syntax-Engine-CFG-PARSER-ANALYZER.git
cd Syntax-Engine-CFG-PARSER-ANALYZERpip install -r requirements.txt(Or manually: pip install customtkinter graphviz Pillow)
Download and install from the link above, ensuring PATH configuration is enabled.
import graphviz
print(graphviz.version()) # Should output Graphviz version without errorsThe primary recommended interface for interactive testing and visualization.
python main_GUI.pyFeatures:
-
Sidebar Controls:
- LOAD GRAMMAR - Select BNF grammar file
- LOAD SENTENCES - Select sentence input file
- EXECUTE ENGINE - Run full analysis pipeline
- DEVELOPER REPOSITORY - Quick link to GitHub profile
-
Multi-Tab Output:
- PARSE TREE - Display parse trees with optional graphical topology view
- JSON STRUCTURE - Validated derivation structures in JSON format
- ANOMALY LOGS - Detailed error diagnostics for invalid inputs
-
Kernel Console:
- Real-time execution logs
- Timestamp-prefixed messages
- System status indicators
-
Interactive Features:
- Animated progress bar during analysis
- Sound effects (success, error, recursion alerts)
- Graphical tree topology toggle
- Multi-threaded background processing
For batch processing and automated testing workflows.
python main_display.pyFeatures:
- Sequential sentence analysis
- Console-based tree visualization
- JSON output to terminal
- Graphical PDF/PNG generation with automatic viewing
- Summary statistics (valid/invalid counts)
<command> ::= <spawn_action> | <state_action>
<spawn_action> ::= spawn <npc> at <zone>
<state_action> ::= set <attribute> = <value>
<npc> ::= hunter | golem | boss
<zone> ::= dungeon | arena | sanctuary
<attribute> ::= aggro | health | speed
<value> ::= 100 | 80 | 0
spawn hunter at arena
set aggro = 100
spawn boss at dungeon
set health = 80
spawn golem at tavern
Valid Sentence:
Input: spawn hunter at arena
Valid
Parse tree:
<command>
βββ <spawn_action>
β βββ spawn
β βββ <npc> β hunter
β βββ at
β βββ <zone> β arena
AST ENGINE OUTPUT: SYSTEM: [HUNTER] named entity loaded at the [ARENA] region.
JSON:
{
"command": {
"spawn_action": {
"npc": "hunter",
"zone": "arena"
}
}
}
Invalid Sentence:
Input: spawn at hunter arena
Invalid
Error Diagnostics:
β’ WHERE: SatΔ±r 1, SΓΌtun 2 (at token 'at')
β’ WHAT: "hunter", "golem", or "boss" was expected.
β’ WHY: after 'spawn', the grammar requires a valid NPC identifier to continue, but found 'at'
The engine automatically detects and prevents left-recursive grammars before parsing begins:
recursive_cycles = parser.detect_left_recursion()
if recursive_cycles:
print("FATAL ERROR: Left Recursion detected")
# E.g., A β B β A or A β A xComprehensive error reporting with three contextual layers:
- WHERE: Exact token position (line, column)
- WHAT: Expected terminal/non-terminal symbols
- WHY: Explanation of derivation failure in natural language
The built-in interpreter executes domain-specific actions extracted from parse trees:
engine = interpreter.Interpreter()
result = engine.evaluate(parse_tree)
# Executes spawn/set commands and maintains NPC stateAutomated visual generation with Graphviz:
# Generated as: gui_1.pdf, gui_2.pdf, etc.
# Viewable in the GUI's PARSE TREE tab with topology toggleSyntax-Engine-CFG-PARSER-ANALYZER/
βββ lexer.py # Tokenization engine
βββ my_parser.py # Recursive descent parser
βββ my_reader.py # Grammar and sentence file reader
βββ interpreter.py # AST evaluator
βββ parse_tree_generator.py # Tree visualization (text & graphical)
βββ main_display.py # CLI execution interface
βββ main_GUI.py # GUI execution interface
βββ grammar_game.txt # Example game grammar (BNF)
βββ sentences_game.txt # Example game sentences
βββ execution_and_dependency_notes.txt # Installation guide
βββ README.md # This file
βββ requirements.txt # Python dependencies
The CLI mode provides automated batch testing:
python main_display.pyConfigured to test against:
grammar1.txtwithsentences.txtgrammar2.txtwithsentences2.txtgrammar3.txtwithsentences3.txt
-
Define Grammar: Create
my_grammar.txtwith BNF rules -
Create Sentence File: Create
my_sentences.txtwith test inputs -
Run Parser:
- GUI: Load both files and click EXECUTE ENGINE
- CLI: Modify
main_display.pyto reference your files
Solution: Graphviz is not installed or not in PATH
- Re-run Graphviz installer
- Ensure "Add Graphviz to the system PATH" is checked
- Restart Python/IDE after installation
Solution: Graphviz rendering failure
- Check
PARSE TREEβANOMALY LOGStab for error details - Verify Graphviz installation with:
dot -V
Solution: Left recursion detected
- Check grammar for circular dependencies
- Run
detect_left_recursion()in isolation - Refactor grammar to eliminate cycles
Solution: Check grammar or tokenization
- Verify BNF syntax (use
::=and|correctly) - Review lexer token specifications in
lexer.py - Add test sentences one-by-one for isolation
Located in main_GUI.py:
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("blue")Options: "Dark", "Light", "System" for mode
Colors: "blue", "green", "red", "purple", etc.
Control in play_sound() method:
if sound_type == "success":
for freq in [600, 800, 1200]:
winsound.Beep(freq, 150)- Python: 65% (Core engine and interfaces)
- TeX/Documentation: 35% (Mathematical notation and formal specifications)
This project is provided as-is for educational and research purposes.
DioBey7
GitHub: @DioBey7
Contributions, bug reports, and feature requests are welcome. Feel free to:
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature) - Commit changes (
git commit -m 'Add YourFeature') - Push to branch (
git push origin feature/YourFeature) - Open a Pull Request
For issues, questions, or feature requests:
- Open an issue on GitHub Issues
- Check existing documentation in
execution_and_dependency_notes.txt
- LL(1) grammar analysis and conversion
- SLR parser implementation as alternative to recursive descent
- Extended interpreter with variable scoping and functions
- Interactive grammar debugging mode
- Export parse trees in AST JSON format
- Multi-language semantic action definitions
Version: Academic Build 443728
Last Updated: June 2026
Status: Active Development