Skip to content

pri-2711/ErrorDetector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compiler Design Project

What this project does

This project is a simple Python syntax error detector and grammar parser. It processes source code in four major phases:

  1. 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.
  2. 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.
  3. 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 if headers, print(...), and a for ... 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.
  4. 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.

Files in this folder

  • 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.

How the code works in simple language

  • 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.

How to run it

From the cdProject folder, run:

python -u main.py

This will execute two built-in tests:

  • TEST 1: Correct Program
  • TEST 2: Program with Errors

Limitations

  • The grammar parser is not a full Python parser.
  • It only supports a few transformed forms, including:
    • id = expr
    • if 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.

Notes

  • 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.

About

Syntax analyzer which detects errors in python scripts using compiler design concepts and parser - LR(1), LALR

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages