Name Analysis and structs
Relevant CSX Grammar Rules
decl ::= varDecl
| fnDecl
| structDecl // struct defs only at top level
;
varDeclList ::= varDeclList varDecl
| /* epsilon */
;
varDecl ::= type id SEMICOLON
| STRUCT id id SEMICOLON
;
structDecl ::= STRUCT id LCURLY structBody RCURLY SEMICOLON
;
structBody ::= structBody varDecl
| varDecl
;
type ::= INT
| BOOL
| VOID
;
loc ::= id
| loc DOT id
id ::= ID
;
Definition of a struct type
struct Point {
int x;
int y;
};
struct Color {
int r;
int g;
int b;
};
struct ColorPoint {
struct Color color;
struct Point point;
};
Declaring a variable of type struct
struct Point pt;
struct Color red;
struct ColorPoint cpt;
Accessing fields of a struct
pt.x = 7;
pt.y = 8;
pt.z = 10;
red.r = 255;
red.g = 0;
red.b = 0;
cpt.point.x = pt.x;
cpt.color.r = red.r;
cpt.color.g = 34;