Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Cpp-Subset-Parser

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.

Features

  • 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, cin with stream operators (<<, >>)
    • Comments (single-line // and multi-line /* */)
    • Preprocessor directives (#include)
  • 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

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/cpp-subset-parser.git
cd cpp-subset-parser
  1. Install dependencies:
pip install ply

Usage

Run the parser on a C++ source file:

python main.py input.cpp

Or run interactively:

python main.py
# Enter filename when prompted

Example Input

#include <iostream>

int main() {
    int x = 5;
    int y = 10;
    
    if (x < y) {
        cout << "x is less than y" << "\n";
    }
    
    return 0;
}

Example Output

✓ 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!

Project Structure

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

Language Subset

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: cout with << and cin with >>
  • Comments: C++ style (//) and C style (/* */)

Technical Details

  • 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

Limitations

  • 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

License

MIT License

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests for bug fixes, improvements, or additional features.

Acknowledgments

Built as a demonstration project for compiler design concepts using PLY.

About

A Python lexer and parser for C++ subset syntax validation using PLY (Python Lex-Yacc)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages