A Python-based lexer and parser for a C++-like language subset, built using PLY (Python Lex-Yacc). This project demonstrates compiler frontend concepts including tokenization, grammar parsing, and abstract syntax tree (AST) generation.
-
Lexical Analysis: Tokenizes C++-like source code with support for:
- Keywords:
int,float,char,void,if,else,for,while,return - Operators: arithmetic (
+,-,*,/,%), comparison (==,!=,<,>,<=,>=), assignment (=) - I/O statements:
cout,cinwith stream operators (<<,>>) - Comments (single-line
//and multi-line/* */) - Preprocessor directives (
#include)
- Keywords:
-
Syntax Parsing: Validates code structure and builds an AST for:
- Function definitions with parameters
- Variable declarations with optional initialization
- Control flow:
if/else,for,while - Expressions: arithmetic, comparison, assignment
- I/O operations with chained streams
-
AST Visualization: Pretty-prints the abstract syntax tree for parsed programs
- Clone the repository:
git clone https://github.com/yourusername/cpp-subset-parser.git
cd cpp-subset-parser- Install dependencies:
pip install plyRun the parser on a C++ source file:
python main.py input.cppOr run interactively:
python main.py
# Enter filename when prompted#include <iostream>
int main() {
int x = 5;
int y = 10;
if (x < y) {
cout << "x is less than y" << "\n";
}
return 0;
}✓ Parsing successful!
--- Generated AST (Abstract Syntax Tree) ---
program
function_def (main)
int
compound
declaration (int)
x = 5
declaration (int)
y = 10
if
binop (<)
id: x
id: y
compound
cout
string: "x is less than y"
string: "\n"
return
number: 0
============================================================
✓ Code is syntactically valid!
cpp-subset-parser/
├── lexer.py # Tokenization rules and lexical analysis
├── parser.py # Grammar rules and AST construction
├── main.py # Entry point for running the parser
└── README.md # Project documentation
This parser supports a limited subset of C++ including:
- Data types:
int,float,char,void - Control structures:
if/else,for,while - Functions: definitions with typed parameters
- Operators: arithmetic, comparison, assignment
- I/O:
coutwith<<andcinwith>> - Comments: C++ style (
//) and C style (/* */)
- Built with PLY (Python Lex-Yacc) for lexing and parsing
- Implements a simple recursive descent parser
- Uses operator precedence rules to handle expression ambiguity
- Generates a tree-structured AST for syntax validation
- Does not perform semantic analysis or type checking
- No support for classes, templates, or advanced C++ features
- Basic string handling (no complex escape sequences)
- Limited error recovery during parsing
MIT License
Contributions are welcome! Feel free to open issues or submit pull requests for bug fixes, improvements, or additional features.
Built as a demonstration project for compiler design concepts using PLY.