This project is a basic Java Syntax Analyzer designed to read Java code from an input file, detect syntax errors, and output a cleaned and analyzed version of the code along with descriptive error messages.
- Reads code from an
input.txtfile. - Detects and reports:
- Missing semicolons (
;) - Unmatched or missing curly braces (
{or}) - Invalid or malformed
ifandwhileconditions - Invalid identifiers (only lowercase single-character names like
a,b,x, etc. are valid)
- Missing semicolons (
- Splits the input into individual statements and classifies them as:
- Variable Declarations → e.g.,
int a; - Assignments → e.g.,
a = 3 + 2; - Return Statements → e.g.,
return x; - Conditional Statements →
if,while
- Variable Declarations → e.g.,
- Writes the output (including corrections and error messages) to
output.txt.
| Statement Type | Validation Criteria |
|---|---|
| Variable Declaration | Must follow type identifier;, e.g., int x; |
| Assignment | Must follow identifier = expression;, identifiers must be single lowercase letters |
| Return Statement | Must be return identifier; or return expression; |
| If / While Condition | Must have a valid comparison like a < b, and be enclosed in parentheses ( ) |
The program detects and auto-corrects common syntax issues:
-
Missing Semicolons
Adds a semicolon and logs an error message. -
Missing or Unmatched Braces
Tracks curly braces using a stack and inserts missing braces when necessary. -
Malformed Conditions
Flags conditions that don’t match the expected pattern (e.g.,a + b < c * d). -
Invalid Identifiers
Only allows lowercase letters likea,b,x— rejects anything else as invalid.
Each error is logged in the output.txt with a corresponding fix note.