This project is a custom compiler written in Python for a unique programming language. It performs two critical phases of compilation: lexical analysis and syntax analysis. These phases are designed to break down and validate source code, ensuring it adheres to predefined rules and structure.
The project is an educational tool, showcasing how compilers analyze and validate source code.
- Breaks down the input source code into tokens.
- Supports keywords, custom operators, constants, and identifiers.
- Detects and reports unrecognized tokens with detailed error messages, including line and word position.
- Validates the program's structure according to specified grammar rules.
- Ensures correct relationships and sequences between tokens.
- Reports syntax errors with contextual information to aid debugging.
- Recognizes user-defined operators (e.g.,
<=>,<+>,<->). - Supports identifiers enclosed in hyphens (e.g.,
-myVar-). - Handles numeric constants, including fractions (e.g.,
123,45/67).
Version 1:
START -> StmtList | id Expression | $
StmtList -> IfStmt | WhileStmt | ForStmt | FuncStmt | CallFunc
IfStmt -> CON | Condition | THEN: { START }; X
X -> ElseStmt | START | λ
ElseStmt -> NOTOK { START }; Y
Y -> START | λ
WhileStmt -> GO | UNTIL Condition | { START }; Y
ForStmt -> GOCON | WITH Condition UNTIL Condition NEXT Condition | { START }; Y
FuncStmt -> SUB(id, PARAMETER) { START }; Y
CallFunc -> SAYSUBTO(id, PARAMETER) Y
Condition -> TERM ASSIGN TERM
Expression -> ∈ TERM OP TERM
TERM -> id | integer | float
PARAMETER -> id Q
Q -> , PARAMETER | λ
ASSIGN -> <=> | <<> | <>> | <≠>
OP -> <∨> | <-> | <∧> | <∺>
First/Follow Analysis:
-
START:
- Firsts:
<CON_TK>,<GO_TK>,<GOCON_TK>,<SUB_TK>,<SAYSUBTO_TK>,<id,number> - Follows:
},$
- Firsts:
-
StmtList:
- Firsts:
<CON_TK>,<GO_TK>,<GOCON_TK>,<SUB_TK>,<SAYSUBTO_TK> - Follows:
},$
- Firsts:
-
IfStmt:
- Firsts:
<CON_TK> - Follows:
},$
- Firsts:
-
WhileStmt:
- Firsts:
<GO_TK> - Follows:
},$
- Firsts:
-
ForStmt:
- Firsts:
<GOCON_TK> - Follows:
},$
- Firsts:
-
FuncStmt:
- Firsts:
<SUB_TK> - Follows:
},$
- Firsts:
-
CallFunc:
- Firsts:
<SAYSUBTO_TK> - Follows:
},$
- Firsts:
-
Condition:
- Firsts:
<id,number> - Follows:
UNTIL,NEXT
- Firsts:
-
Expression:
- Firsts:
∈ - Follows:
},$
- Firsts:
-
TERM:
- Firsts:
<id,number> - Follows:
},$
- Firsts:
-
PARAMETER:
- Firsts:
<id,number> - Follows:
)
- Firsts:
-
ASSIGN:
- Firsts:
<=>,<<>,<>>,<≠> - Follows:
<id,number>
- Firsts:
-
OP:
- Firsts:
<∨>,<->,<∧>,<∺> - Follows:
<id,number>
- Firsts:
An example of the source code processed by the compiler:
GO CON -identifier-
123 <+> 456
invalid-token SAYSUBTO -anotherIdentifier-
Tokens:
<GO>
<CON>
<id,1>
<123>
<<+>>
<456>
<SAYSUBTO>
<id,2>
Errors:
Line 1, Word 4: 'invalid-token' - Unrecognized token
Input:
GO 123 <->
CON -variable- SAYSUBTO
Output:
Syntax Errors:
Line 1: Incomplete expression after '123'.
Line 2: Unexpected command 'SAYSUBTO' following an identifier.
- The source code is read from a file (
input.txt).
- The code is split into words and matched against predefined rules for:
- Keywords (e.g.,
GO,CON,SAYSUBTO). - Operators (e.g.,
<+>,<->). - Identifiers (
-identifier-format). - Numbers and fractions (
123,45/67).
- Keywords (e.g.,
- The tokens are organized into a parse tree based on grammar rules.
- Relationships between tokens are validated for correctness.
- Syntax errors are logged with line numbers and contextual details.
- Unrecognized tokens or invalid constructs are logged with their line number and position in the line.
- Tokens are written to an output file (
output.txt). - Syntax and lexical errors are detailed in the same file or printed to the console for debugging.
-
Write your source code into
input.txt:GO <=> 123/45 -variable- SUB xyz -
Run the compiler:
python COM.py
-
Check the output in
output.txt:<GO> <=> <123/45> <id,1> <SUB> Errors: Line 2, Word 3: 'xyz' - Unrecognized token Line 2: Syntax error: Missing operator or invalid sequence after 'SUB'.
- Clone the repository.
- Install Python 3.7 or later.
- Place your source code in
input.txt. - Run the compiler with:
python COM.py
- Check
output.txtfor tokens and errors.
Contributions are welcome! Feel free to create issues or submit pull requests to enhance the compiler. Examples include adding new token rules, improving syntax analysis, or optimizing performance.