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.
- Basic types:
int,char,float,double,long,long longint x; char c; float f;
- Storage class specifiers:
extern,staticextern 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 };
- Arithmetic:
x + y,a * b,x += 1x = y + 5; x += 1;
- Relational:
x < y,a >= bif (x < y) {...}
- Logical:
x && y,a || bif (x && y) {...}
- Bitwise:
x & y,a ^ b,x << 2int result = x & y;
- Pointer Dereferencing:
*p = 10;,*******p = 30;*ptr = 10; *******ptr = 30;
- Array Access:
x[0] = 5;x[0] = 5;
- Conditional:
if,if-elseif (x > 0) { y = 1; } else { y = 0; }
- Loops:
while,for,do-whilewhile (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);
- 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 }
- 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);)
externstatictypedef
- Stores structs, enums
- Handles type lookup for custom types
- 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();
- Error handling improvements
- Support for more complex function pointer syntax
This parser is now capable of handling a comprehensive subset of C syntax! 🚀