--- a/c2iec.y +++ b/c2iec.y @@ -18,14 +18,15 @@ %{ #include <string.h> #include <stdio.h> +#include <stdlib.h> // get tail of a string #define TAIL(str) (&str[strlen(str)]) /* fifo to store expression strings */ -#define STMT_SIZE 128 -#define FIFO_SIZE 20 +#define STMT_SIZE 1024 +#define FIFO_SIZE 200 typedef enum { @@ -73,10 +74,10 @@ char *strIdentifier(char *szStr); // add symbol name to symbol table -#define SYM_SIZE 64 -#define SYM_COUNT 64 +#define SYM_SIZE 128 +#define SYM_COUNT 1024 int s_iSymTabCount = 0; -static char s_szSymTab[SYM_COUNT][SYM_SIZE]; +char s_szSymTab[SYM_COUNT][SYM_SIZE]; void addSym(char *szSym); int isSym(char *szSym); @@ -268,11 +269,17 @@ char *p; while( (p=fifo_pop(decl_all)) != NULL) /* noop */; - - printf("TYPE "); - printf($<str>3, $<str>2); - addSym(strIdentifier($<str>3)); - printf("END_TYPE\n\n"); + if (strlen($<str>3) > 0) + { + printf("TYPE "); + printf($<str>3, $<str>2); + addSym(strIdentifier($<str>3)); + printf("END_TYPE\n\n"); + } + else + { + addSym($<str>4); + } strcpy($<str>$, ""); } | declaration_specifiers init_declarator_list ';' @@ -324,6 +331,7 @@ : TYPEDEF | EXTERN | STATIC + { strcpy($<str>$, ""); } | AUTO | REGISTER ; @@ -510,18 +518,21 @@ { sprintf($<str>$, "FUNCTION %s:",$<str>1); fifo_push(decl_all, $<str>$); + strcpy($<str>$, $<str>1); strcpy($<str>$, ""); } | direct_declarator '(' identifier_list ')' + { + sprintf($<str>$, "FUNCTION %s:",$<str>1); + fifo_push(decl_all, $<str>$); + strcpy($<str>$, $<str>1); + strcpy($<str>$, ""); + } + | direct_declarator '(' ')' { sprintf($<str>$, "FUNCTION %s:",$<str>1); fifo_push(decl_all, $<str>$); strcpy($<str>$, ""); - } - | direct_declarator '(' ')' - { - sprintf($<str>$, "FUNCTION %s:",$<str>1); - fifo_push(decl_all, $<str>$); strcpy($<str>$, ""); } ; @@ -673,11 +684,15 @@ jump_statement : GOTO IDENTIFIER ';' + { sprintf($<str>$, " %s %s;\n",$<str>1, $<str>2); } | CONTINUE ';' + { sprintf($<str>$, " %s;\n",$<str>1); } | BREAK ';' { sprintf($<str>$, "EXIT;\n"); } | RETURN ';' + { sprintf($<str>$, " %s;\n",$<str>1); } | RETURN expression ';' + { sprintf($<str>$, " %s %s;\n",$<str>1, $<str>2); } ; translation_unit @@ -878,7 +893,21 @@ void addSym(char *szSym) { int i; + if (s_iSymTabCount >= SYM_COUNT) + { + printf("error: symbol table size exceeded!\n"); + exit(-1); + } + if (strlen(szSym) >= SYM_SIZE) + { + printf("error: symbol name too long '%s'!\n", szSym); + exit(-1); + } + i = s_iSymTabCount++; + +// printf("symbol: adding '%s' (%i)\n", szSym, i); + strcpy(s_szSymTab[i], szSym); } @@ -887,11 +916,14 @@ int i; for (i=0; i < s_iSymTabCount; i++) { +// printf("symbol: check %d: %s\n", i, s_szSymTab[i]); if (!strcmp(s_szSymTab[i], szSym)) { +// printf("symbol: found '%s'\n", szSym); return 1; } } +// printf("symbol: missed '%s'\n", szSym); return 0; }