--- /dev/null
+#include <stdlib.h>
+#include <string.h>
+#include "grammar.h"
+
+struct type_qualifier *type_qualifier_new(struct token *tok)
+{
+ struct type_qualifier *tq;
+
+ tq = malloc(sizeof(*tq));
+
+ if(tq) {
+ memset(tq, 0, sizeof(*tq));
+ tq->token = tok;
+ }
+
+ return(tq);
+}
+
+struct storage_class_specifier *storage_class_specifier_new(struct token *tok)
+{
+ struct storage_class_specifier *sp;
+
+ sp = malloc(sizeof(*sp));
+
+ if(sp) {
+ memset(sp, 0, sizeof(*sp));
+ sp->token = tok;
+ }
+
+ return(sp);
+}
+
+struct type_specifier *type_specifier_new(void)
+{
+ struct type_specifier *ts;
+
+ ts = malloc(sizeof(*ts));
+
+ if(ts) {
+ memset(ts, 0, sizeof(*ts));
+ }
+
+ return(ts);
+}
+
+struct translation_unit *translation_unit_new(struct external_declaration *edecl)
+{
+ struct translation_unit *tu;
+
+ tu = malloc(sizeof(*tu));
+
+ if(tu) {
+ memset(tu, 0, sizeof(*tu));
+ tu->decl = edecl;
+ }
+
+ return(tu);
+}
+
+struct external_declaration *external_declaration_new(void)
+{
+ struct external_declaration *ed;
+
+ ed = malloc(sizeof(*ed));
+
+ if(ed) {
+ memset(ed, 0, sizeof(*ed));
+ }
+
+ return(ed);
+}
--- /dev/null
+#ifndef GRAMMAR_H
+#define GRAMMAR_H
+
+struct token;
+
+struct type_qualifier {
+ struct token *token;
+};
+
+struct storage_class_specifier {
+ struct token *token;
+};
+
+enum type_specifier_type {
+ TYPE_SPECIFIER_INVALID,
+ TYPE_SPECIFIER_PRIMITIVE,
+ TYPE_SPECIFIER_STRUCT,
+ TYPE_SPECIFIER_UNION,
+ TYPE_SPECIFIER_ENUM,
+ TYPE_SPECIFIER_TYPEDEF
+};
+
+struct struct_specifier {
+ struct token *token;
+};
+
+struct union_specifier {
+ struct token *token;
+};
+
+struct enum_specifier {
+ struct token *token;
+};
+
+struct typedef_name {
+ /* add a reference to the typedef? */
+ struct token *token;
+};
+
+struct type_specifier {
+ enum type_specifier_type type;
+ struct token *token;
+
+ struct struct_specifier *ss;
+ struct union_specifier *us;
+ struct enum_specifier *es;
+ struct typedef_name *tn;
+};
+
+struct function_definition {
+
+};
+
+struct declaration {
+
+};
+
+struct external_declaration {
+ struct function_definition *fdef;
+ struct declaration *decl;
+};
+
+struct translation_unit {
+ struct translation_unit *unit;
+ struct external_declaration *decl;
+};
+
+struct type_qualifier *type_qualifier_new(struct token*);
+struct storage_class_specifier *storage_class_specifier_new(struct token*);
+struct type_specifier *type_specifier_new(void);
+struct translation_unit *translation_unit_new(struct external_declaration*);
+struct external_declaration *external_declaration_new(void);
+
+#endif /* GRAMMAR_H */
#include <stdio.h>
#include "token.h"
#include "lex.h"
-
-#define sizeof_a(_x) (sizeof(_x) / sizeof((_x)[0]))
-
-struct type_qualifier {
- struct token *token;
-};
-
-struct storage_class_specifier {
- struct token *token;
-};
-
-enum type_specifier_type {
- TYPE_SPECIFIER_INVALID,
- TYPE_SPECIFIER_PRIMITIVE,
- TYPE_SPECIFIER_STRUCT,
- TYPE_SPECIFIER_UNION,
- TYPE_SPECIFIER_ENUM,
- TYPE_SPECIFIER_TYPEDEF
-};
-
-struct struct_specifier {
- struct token *token;
-};
-
-struct union_specifier {
- struct token *token;
-};
-
-struct enum_specifier {
- struct token *token;
-};
-
-struct typedef_name {
- /* add a reference to the typedef? */
- struct token *token;
-};
-
-struct type_specifier {
- enum type_specifier_type type;
- struct token *token;
-
- struct struct_specifier *ss;
- struct union_specifier *us;
- struct enum_specifier *es;
- struct typedef_name *tn;
-};
-
-struct function_definition {
-
-};
-
-struct declaration {
-
-};
-
-struct external_declaration {
- struct function_definition *fdef;
- struct declaration *decl;
-};
-
-struct translation_unit {
- struct translation_unit *unit;
- struct external_declaration *decl;
-};
-
-struct type_qualifier *type_qualifier_new(struct token *tok)
-{
- struct type_qualifier *tq;
-
- tq = malloc(sizeof(*tq));
-
- if(tq) {
- memset(tq, 0, sizeof(*tq));
- tq->token = tok;
- }
-
- return(tq);
-}
-
-struct storage_class_specifier *storage_class_specifier_new(struct token *tok)
-{
- struct storage_class_specifier *sp;
-
- sp = malloc(sizeof(*sp));
-
- if(sp) {
- memset(sp, 0, sizeof(*sp));
- sp->token = tok;
- }
-
- return(sp);
-}
+#include "grammar.h"
struct type_qualifier *parse_type_qualifier(void)
{
return(ret_val);
}
-struct type_specifier *type_specifier_new(void)
-{
- struct type_specifier *ts;
-
- ts = malloc(sizeof(*ts));
-
- if(ts) {
- memset(ts, 0, sizeof(*ts));
- }
-
- return(ts);
-}
-
-struct translation_unit *translation_unit_new(struct external_declaration *edecl)
-{
- struct translation_unit *tu;
-
- tu = malloc(sizeof(*tu));
-
- if(tu) {
- memset(tu, 0, sizeof(*tu));
- tu->decl = edecl;
- }
-
- return(tu);
-}
-
struct struct_specifier *parse_struct_specifier(void)
{
return(NULL);
case TYPE_SPECIFIER_STRUCT:
if(!ret_val->ss) {
/* syntax error */
- fprintf(stderr, "%s:%d:%d [E] Expected struct-specifier\n",
+ fprintf(stderr,
+ "%s:%d:%d [E] Expected struct-specifier\n",
tok->file, tok->line, tok->column);
ret_val->type = TYPE_SPECIFIER_INVALID;
}
case TYPE_SPECIFIER_UNION:
if(!ret_val->us) {
/* syntax error */
- fprintf(stderr, "%s:%d:%d [E] Expected union-specifier\n",
+ fprintf(stderr,
+ "%s:%d:%d [E] Expected union-specifier\n",
tok->file, tok->line, tok->column);
ret_val->type = TYPE_SPECIFIER_INVALID;
}
case TYPE_SPECIFIER_ENUM:
if(!ret_val->es) {
/* syntax error */
- fprintf(stderr, "%s:%d:%d [E] Expected enum-specifier\n",
+ fprintf(stderr,
+ "%s:%d:%d [E] Expected enum-specifier\n",
tok->file, tok->line, tok->column);
ret_val->type = TYPE_SPECIFIER_INVALID;
}
case TYPE_SPECIFIER_TYPEDEF:
if(!ret_val->tn) {
/* syntax error */
- fprintf(stderr, "%s:%d:%d [E] Expected typedef-name\n",
+ fprintf(stderr,
+ "%s:%d:%d [E] Expected typedef-name\n",
tok->file, tok->line, tok->column);
ret_val->type = TYPE_SPECIFIER_INVALID;
}
default:
if(ret_val->type != TYPE_SPECIFIER_PRIMITIVE) {
/* syntax error */
- fprintf(stderr, "%s:%d:%d [E] Expected type-specifier\n",
+ fprintf(stderr,
+ "%s:%d:%d [E] Expected type-specifier\n",
tok->file, tok->line, tok->column);
ret_val->type = TYPE_SPECIFIER_INVALID;
}
return(ret_val);
}
-struct external_declaration *external_declaration_new(void)
-{
- struct external_declaration *ed;
-
- ed = malloc(sizeof(*ed));
-
- if(ed) {
- memset(ed, 0, sizeof(*ed));
- }
-
- return(ed);
-}
-
struct function_definition *parse_function_definition(void)
{
return(NULL);