Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
#define KMC_C90_COMPILER_AST_H

enum AstTag {
AST_IDENTIFIER
AST_IDENTIFIER,
AST_INTEGER_CONSTANT,
AST_TYPE_VOID,
AST_TYPE_INT,
AST_SPEC_AUTO,
AST_SPEC_REGISTER,
AST_SPEC_STATIC,
AST_SPEC_EXTERN,
AST_SPEC_TYPEDEF
};

#endif /* KMC_C90_COMPILER_AST_H */
81 changes: 62 additions & 19 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,41 @@ void set_yyin_string(const char *code);
%%

identifier
: IDENTIFIER
: IDENTIFIER {
$$ = cons(sexpr_make_ast(AST_IDENTIFIER), $[IDENTIFIER]);
}
;

expression.opt
: %empty
| expression
: %empty {
$$ = NULL;
}
| expression {
$$ = $[expression];
}
;

expression
: identifier
| INTEGER_CONSTANT
: identifier {
$$ = $[identifier];
}
| integer-constant {
$$ = $[integer-constant];
}
;

integer-constant
: INTEGER_CONSTANT {
$$ = cons(sexpr_make_ast(AST_INTEGER_CONSTANT), $[INTEGER_CONSTANT]);
}

fundamental-specifier
: VOID
| INT
: VOID {
$$ = sexpr_make_ast(AST_TYPE_VOID);
}
| INT {
$$ = sexpr_make_ast(AST_TYPE_INT);
}
/* | CHAR */
/* | SIGNED CHAR */
/* | UNSIGNED CHAR */
Expand All @@ -119,39 +138,63 @@ fundamental-specifier
;

storage-class-specifier.opt
: %empty
| storage-class-specifier
: %empty {
$$ = NULL;
}
| storage-class-specifier {
$$ = $[storage-class-specifier];
}
;

storage-class-specifier
: AUTO
| REGISTER
| STATIC
: AUTO {
$$ = sexpr_make_ast(AST_SPEC_AUTO);
}
| REGISTER {
$$ = sexpr_make_ast(AST_SPEC_REGISTER);
}
| STATIC {
$$ = sexpr_make_ast(AST_SPEC_STATIC);
}
;

linkage-specifier.opt
: %empty
| linkage-specifier
: %empty {
$$ = NULL;
}
| linkage-specifier {
$$ = $[linkage-specifier];
}
;

linkage-specifier
: EXTERN
| STATIC
: EXTERN {
$$ = sexpr_make_ast(AST_SPEC_EXTERN);
}
| STATIC {
$$ = sexpr_make_ast(AST_SPEC_STATIC);
}
;

typedef-specifier
: TYPEDEF
: TYPEDEF {
$$ = sexpr_make_ast(AST_SPEC_TYPEDEF);
}
;

type-specifier
: fundamental-specifier
: fundamental-specifier {
$$ = cons($[fundamental-specifier], NULL);
}
/* | struct-or-union-specifier */
/* | enum-specifier */
/* | typedef-name */
;

declaration-specifiers
: type-specifier
: type-specifier {
$$ = $[type-specifier];
}
/* : type-specifier type-qualifier-list.opt */
/* | type-qualifier declaration-specifiers */
;
Expand Down