Skip to content

shafiamanzoor762/compiler-construction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Compiler Construction Project (C# Windows Forms)

๐Ÿท๏ธ Badges

C# Windows Forms Compiler Design Lexical Analyzer Parser Syntax Tree Error Handling Visual Studio

๐Ÿ“Œ Overview

This project is a custom-built compiler simulation implemented using C# Windows Forms. It parses and evaluates a custom-designed scripting language that supports variables, conditional logic, loops, arrays, and I/O operations.

The compiler scans input code, validates it using Regular Expressions (Regex), and simulates the compilation process with basic syntax analysis, semantic checks, symbol table generation, and intermediate interpretation.


๐Ÿ› ๏ธ Features

  • ๐Ÿงพ Lexical Analysis using Regex.
  • ๐Ÿ“„ Syntax Analysis with grammar-based validation.
  • ๐Ÿ“š Symbol Table management with scope support.
  • ๐Ÿ’ก Conditional Logic: is, elis, el constructs (similar to if-else).
  • ๐Ÿ” Looping using a loop(...) construct.
  • ๐Ÿ“ฆ Array Declaration with basic initialization.
  • ๐Ÿ–จ๏ธ Display Statements (string, variable, expressions).
  • โŒจ๏ธ Input Statements.
  • โž• Arithmetic Expressions: supports +, -, *, / operators.
  • ๐Ÿงฎ Expression Evaluation.
  • ๐Ÿงช Switch-Case-like structure using option(), opt, def.

๐Ÿงช Sample Code Inputs

Here are some supported code patterns:

num $a = 10, $y;
display < $a;
{
    num $x = 7;
    display < $x;
    $y = 50;
}
display < $y;

num $a = 2;
num $b = 5;
is($a > $b){
    display < 'a is greater';
}
elis($a < $b){
    display < 'a is less';
}
el{
    display < 'a is equal to b';
}

๐Ÿงฐ Technology Stack

Component Technology
Language C#
UI Framework Windows Forms
Logic Handling Regex, C# Classes
IDE Visual Studio

๐Ÿงฉ Supported Syntax

โœ… Declarations

num $x;
flo $y;
chr $z;
str $name;

โœ… Initialization

$x = 5;
$name = "John";

โœ… Declaration + Initialization

num $a = 10;
flo $rate = 3.14;
chr $grade = 'A';

โœ… Multiple Declarations / Initializations

num $x = 5, $y, $z;
flo $a, $b;

โœ… Arithmetic Expressions

$a = 5 + 3;
$x = $y * 2 - 1;

โœ… Display Statements

display < 'Hello World';
display < $x;
display < 'Value: ' + $x;

โœ… Input Statements

in > $name;

โœ… Conditional Statements

is($x > $y){
    ...
}
elis($x == $y){
    ...
}
el{
    ...
}

โœ… Looping

loop(num $i = 0; $i < 10; $i + 1){
    display < $i;
}

โœ… Arrays

num $arr = new num[1, 2, 3, 4];

โœ… Option Block (Switch-Case)

option($choice){
    opt 1:
        display < 'One';
    opt 2:
        display < 'Two';
    def:
        display < 'Default';
}

๐Ÿ—‚๏ธ Project Structure

CompilerProject/
โ”œโ”€โ”€ MainForm.cs        # Main UI logic with Regex-based parsing
โ”œโ”€โ”€ SymbolTable.cs     # Class to store variable name, type, and value
โ”œโ”€โ”€ ScopeSymbolTable.cs# For nested block-level scoping
โ”œโ”€โ”€ Program.cs         # App entry point
โ”œโ”€โ”€ CC_project1.sln
โ””โ”€โ”€ README.md

๐Ÿงช Symbol Table Design

Name Type Value Scope
$a num 10 global
$x num 7 local

๐Ÿ’ก Implementation Highlights

  • Modular Regex Rules:

    • Different patterns for declaration, initialization, arithmetic, loops, and conditionals.
  • Queue-based Execution:

    • Instructions are parsed and added to a queue for simulation.
  • Block Management:

    • Scope-sensitive parsing for nested code blocks.
  • Extensible Design:

    • Easily add support for new types and language constructs.

๐Ÿ”„ How to Run

  1. Clone or download the project.
  2. Open in Visual Studio.
  3. Restore NuGet packages if required.
  4. Press Start (F5) to run the Windows Form app.
  5. Paste example code in the txtInput textbox.
  6. Press the Compile or Run button (ensure you have that button wired to parsing logic).

๐Ÿ“ˆ Future Enhancements

  • โœ… Add error highlighting and messages.
  • โœ… GUI improvements with syntax highlighting.
  • โณ AST (Abstract Syntax Tree) generation.
  • โณ Intermediate Code Generation (ICG).
  • โณ Basic optimization strategies.
  • โณ Function support and recursion.
  • โณ Export symbol table to file.

๐Ÿ‘จโ€๐Ÿซ Educational Value

This project was created as part of a Compiler Construction course to simulate the fundamental concepts of:

  • Lexical & Syntactic analysis
  • Context-free grammar validation
  • Symbol tables and scoping
  • Expression evaluation and control flow

๐Ÿ“„ License

This project is for educational and academic use only. All rights reserved ยฉ 2025.


๐Ÿ™‹ Contact

For questions or suggestions, feel free to contact the developer:

Name: Shafia Manzoor LinkedIn: linkedin.com/in/yourprofile

Releases

No releases published

Packages

No packages published

Languages