This project is a simple Python syntax error detector and grammar parser. It processes source code in four major phases:
-
Lexical Analysis (
lexer.py)- Reads input lines and converts characters into tokens like identifiers, numbers, operators, parentheses, keywords, and punctuation.
- Builds a symbol table for identifiers.
- Converts each source line into a simplified "transformed statement" that the parser can use.
-
Syntax Checking (
syntax_checker.py)- Runs simple rule-based checks on the source code.
- Detects invalid variable names, bad operator sequences, indentation errors, unmatched brackets/quotes, incorrect
print()syntax, and some loop/condition warnings. - Reports errors and warnings before grammar parsing.
-
Grammar Parsing (
grammar_parser.py)- Implements an LR(1) and LALR(1) parser for a small grammar.
- Supports a limited set of statement forms: assignment, arithmetic expressions, simple
ifheaders,print(...), and afor ... in range(...) :header. - Builds parser tables automatically from the grammar, then uses them to accept or reject transformed lines.
- Also generates a parse tree for accepted lines.
-
Final Report (
main.py)- Prints the token list, symbol table, transformed statements, syntax errors, warnings, grammar parse results, and the original source input.
- Explains whether the source was accepted by the syntax checker and whether the grammar parser accepted all transformed lines.
main.py— main driver that runs lexing, syntax checking, and parsing.lexer.py— lexical analyzer that generates tokens and transforms statements.syntax_checker.py— rule-based syntax validator for simple Python-like code.grammar_parser.py— parser implementation with LR(1) and LALR(1) support.
- The code reads source text line by line.
- It turns human-readable code into machine-friendly tokens.
- It checks for obvious Python mistakes like bad variable names, wrong operators, indent problems, and missing brackets.
- It also tries to parse the simplified version of each statement using a small grammar.
- If the syntax checker sees no errors, the program prints
✓ Program accepted — no issues found. - If the grammar parser still rejects some lines, it prints a separate grammar verdict.
From the cdProject folder, run:
python -u main.pyThis will execute two built-in tests:
TEST 1: Correct ProgramTEST 2: Program with Errors
- The grammar parser is not a full Python parser.
- It only supports a few transformed forms, including:
id = exprif id > num :print(id)for id in range(num) :
- Other Python features such as nested blocks, function definitions, full expressions in conditions, or more general statements are not supported by the parser.
- The syntax checker and grammar parser are separate stages.
- A program may pass syntax checking but still fail grammar parsing if some lines are outside the small grammar handled by
grammar_parser.py. - The parser tables are built automatically from the grammar rules in
grammar_parser.py.