https://github.com/shaina-gh/type-checking
A C++ program to implement type checking of variable assignments and expressions, simulating a part of the semantic analysis phase of a compiler.
https://github.com/shaina-gh/type-checking
compiler-design cpp semantic-analysis type-checking
Last synced: 12 months ago
JSON representation
A C++ program to implement type checking of variable assignments and expressions, simulating a part of the semantic analysis phase of a compiler.
- Host: GitHub
- URL: https://github.com/shaina-gh/type-checking
- Owner: shaina-gh
- Created: 2025-06-03T07:03:58.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-03T07:13:17.000Z (about 1 year ago)
- Last Synced: 2025-06-03T18:55:47.393Z (about 1 year ago)
- Topics: compiler-design, cpp, semantic-analysis, type-checking
- Language: C++
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 🧠 AIM
To implement **type checking** for variable declarations and arithmetic expressions using C++, ensuring compatibility of data types in assignments.
---
## 📘 EXPLANATION
In the semantic analysis phase of a compiler, type checking ensures that operations are semantically valid — particularly that data types match across expressions. This project replicates a simplified version of that:
- Users declare variables along with their types (`int`, `float`, `real`).
- An expression is provided in the form of an assignment.
- The program checks that the data type of the LHS variable is not of lower precision than any variables on the RHS.
It flags mismatches or confirms correctness accordingly.
---
## 🔍 LOGIC
1. **Input Stage**:
- Prompt for the number of variables.
- For each, get a variable name and its type (validated against `int`, `float`, `real`).
2. **Store Types**:
- Map variable names to an integer representing their type precedence (e.g., `int` = 0, `float` = 1, `real` = 2).
3. **Parse Expression**:
- Extract LHS and RHS of the expression.
- Tokenize RHS and check that no variable on RHS has higher precedence than LHS.
4. **Type Validation**:
- If an incompatibility is found, report the expected type.
- Otherwise, print "Correct".
---
## 💡 SAMPLE INPUT/OUTPUT
### ✅ Valid Case
**Input:**
```cpp
Enter the number of variables: 2
Enter variable name and type (e.g., a int): x float
Enter variable name and type (e.g., a int): y int
Enter the expression: x = y
```
**Output:**
```cpp
Correct
```
---
### ❌ Invalid Case
**Input:**
```cpp
Enter the number of variables: 3
Enter variable name and type (e.g., a int): a int
Enter variable name and type (e.g., a int): b float
Enter variable name and type (e.g., a int): c int
Enter the expression: a = b + c
```
**Output:**
```cpp
a should be of type float
```
---
## 🌍 REAL-WORLD APPLICATIONS
- **Compiler Design**: Helps detect semantic type errors at compile-time.
- **Integrated Development Environments (IDEs)**: Underpins type hints and warnings.
- **Data Transformation Pipelines**: Prevents operations on mismatched types in ETL systems.
- **Embedded Systems Development**: Ensures type safety to avoid low-level errors in constrained environments.
---