Skip to content

dtaing11/Compiler-Inspired

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C Parser Readme

Overview

This parser is designed to handle a subset of the C programming language. It builds an Abstract Syntax Tree (AST) using JavaCup and Java-based AST classes. The parser supports various C constructs, including variable declarations, expressions, loops, function definitions, and complex types.


✅ Features Supported

1️⃣ Variable Declarations

  • Basic types: int, char, float, double, long, long long
    int x;
    char c;
    float f;
  • Storage class specifiers: extern, static
    extern int globalVar;
    static int localVar;
  • Pointers: int *p;, int ******p;
    int *p;
    int ******ptr;
  • Arrays: int[] x = {1,2,3,4};
    int x[] = {1, 2, 3, 4};
  • Custom types: typedef unsigned int uint;
    typedef unsigned int uint;
    uint num;
  • Structs & Unions: struct Point { int x, y; };
    struct Point {
        int x;
        int y;
    };
  • Enums: enum Color { RED, GREEN, BLUE };
    enum Color { RED, GREEN, BLUE };

2️⃣ Expressions & Assignments

  • Arithmetic: x + y, a * b, x += 1
    x = y + 5;
    x += 1;
  • Relational: x < y, a >= b
    if (x < y) {...}
  • Logical: x && y, a || b
    if (x && y) {...}
  • Bitwise: x & y, a ^ b, x << 2
    int result = x & y;
  • Pointer Dereferencing: *p = 10;, *******p = 30;
    *ptr = 10;
    *******ptr = 30;
  • Array Access: x[0] = 5;
    x[0] = 5;

3️⃣ Statements

  • Conditional: if, if-else
    if (x > 0) {
        y = 1;
    } else {
        y = 0;
    }
  • Loops: while, for, do-while
    while (x > 0) {
        x--;
    }
  • Function calls: printf("Hello");
    printf("Hello");
  • Return statements: return x;
    return x;
  • Block statements: { int x = 5; x++; }
    {
        int x = 5;
        x++;
    }
  • Struct field access: p.x = 10;
    struct Point p;
    p.x = 10;
  • Function pointers: int (*f)(int, int);
    int (*f)(int, int);

4️⃣ Functions

  • Function declarations: extern int foo();
    extern int foo();
  • Function definitions: int add(int a, int b) { return a + b; }
    int add(int a, int b) {
        return a + b;
    }
  • Static functions: static void helper() { ... }
    static void helper() {
        // Function body
    }

5️⃣ Type System

  • Basic types (int, float, char, etc.)
  • Pointer types (int ******p;)
  • Array types (int[])
  • Structs, Enums, and Unions
  • Typedefs (typedef struct Point Point;)
  • Function pointers (int (*f)(int, int);)

6️⃣ Storage Class Specifiers

  • extern
  • static
  • typedef

7️⃣ Symbol Table Support

  • Stores structs, enums
  • Handles type lookup for custom types

8️⃣ Additional Features

  • Bit-fields in structs: unsigned int x : 3;
    struct Example {
        unsigned int x : 3;
    };
  • Sizeof operator: sizeof(type), sizeof(expression)
    int size = sizeof(int);
  • Inline functions: extern inline int foo();
    extern inline int foo();

🚀 Future Enhancements (To be Implemented)

  • Error handling improvements
  • Support for more complex function pointer syntax

This parser is now capable of handling a comprehensive subset of C syntax! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages