0

I am trying to make a compiler and if the compiler is given the token INTEGER, REAL or CHARACTER, i would like it to generate code to make int, float or char instead. The language I am writing in is C and I am using flex and bison.

My lexer METHODS are:

    SYMTABNODEPTR newSymTabNode()
{
    return ((SYMTABNODEPTR)malloc(sizeof(SYMTABNODE)));
}

int installId(char *id) 
{
    extern SYMTABNODEPTR symTab[SYMTABSIZE]; 
    extern int currentSymTabSize;
    int index;

    index = lookup(id);
    if (index >= 0)
    {
        return (index);
    }
    else 
    {
       symTab[currentSymTabSize] = newSymTabNode();
       strcpy(symTab[currentSymTabSize]->identifier,id);
       //symTab[currentSymTabSize]->type=0;
       return(currentSymTabSize++);
    }
}

int lookup(char *s)
{
    extern SYMTABNODEPTR symTab[SYMTABSIZE];
    extern int currentSymTabSize;
    int i;

    for(i=0; i<currentSymTabSize; i++)
    {
        if(strcmp(s,symTab[i]->identifier) == 0)
        {
            return (i);
        }
    }
    return (-1);    
}

and in my parser I have:

struct symTabNode {
    char identifier [IDLENGTH];
};

typedef struct symTabNode SYMTABNODE;
typedef SYMTABNODE *SYMTABNODEPTR;

SYMTABNODEPTR symTab[SYMTABSIZE];

%token<iVal> IDENTIFIER NUMBER CHARACTER_CONSTANT EQUAL GTLT LT GT LTEQ GTEQ PLUS MINUS TIMES DIVIDE ASSIGN CHARACTER INTEGER REALCONST

type                    :   CHARACTER
                            {
                                $$ = create_node($1, TYPE_NODE, NULL, NULL, NULL);
                            }
                        |   INTEGER
                            {
                                $$ = create_node($1, TYPE_NODE, NULL, NULL, NULL);
                            }
                        |   REALCONST
                            {
                                $$ = create_node($1, TYPE_NODE, NULL, NULL, NULL);
                            }

void GenerateCode(TERNARY_TREE t)
    {

        if (t == NULL) return;
            switch(t->nodeIdentifier)
            {
                case(TYPE_NODE):
                        printf("%s ",symTab[t->item]);

                    break;
                default:
                    GenerateCode(t->first);
                    GenerateCode(t->second);
                    GenerateCode(t->third);
}

My compiler is currently outputting:

INTEGER a, b, c;
REAL d, e;
CHARACTER f;

it would be great to have

int a, b, c;
float d, e;
char f;

instead I hope thats enough information (there is alot more to the compiler of course) Thank you in advance

1
  • 1
    I removed the Flex tag; as I'm pretty sure this has nothing to do w/ Adobe Flex and I added the gnu-flex tag which relates to the lexical analyzer. Dec 4, 2011 at 13:06

1 Answer 1

1

I recommend creating a very simple test case (much like you put in your question), and then running your compiler in the debugger. As you step through line by line, the problem should become apparent to you. At least it usually does for me. You could also modify your program to print out extra information at key points in its execution so you can see what it is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.