when i debug this code it is giving few errors whats wrong in this code can anyone please help out :S

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#define T_SEMICOLON';'
#define T_LPAREN'('
#define T_RPAREN')'
#define T_ASSIGN'='
#define T_DIVIDE'/'

#define T_WHILE 257
#define T_IF 258
#define T_RETURN 259

#define T_IDENTIFIER 268
#define T_INTEGER 269
#define T_DOUBLE 270
#define T_STRING 271

#define T_END 349
#define T_UNKNOWN 350



using namespace System;

struct token_t {
    int type;
    union{
        char stringValue [256];
        int intValue;
        double doubleValue;
    } val;
};
//array<System::String ^> ^args
int main(int argc,char *argv[])
{
    Console.WriteLine("enter any string:");

    struct token_t token; 
    InitScanner();
    while (ScanOneToken(stdin, &token) !=T_END);
    return 0;

}
static void InitScanner()
{
create_reserved_table();
{
insert_reserved("WHILE",T_WHILE)
insert_reserved("IF",T_IF)
insert_reserved("RETURN",T_RETURN)
}
static int ScanOneToken(FILE *fp,struct token_t *token)
{
    int i,ch,nextch;
    ch =getc(fp);
    while (isspace(ch))
        ch = getc(fp);
    switch(ch)
    {
    case'/':
        nextch=getc(fp);
        if(nextch=='/'|| nextch =='*');
        else
            ungetc(nextch,fp););
    case';':case',':case'=':
        token->type =ch;
        return ch;

    case 'A':case'B':case'C':case'D':case'E':case'F':case'G':case'H':case 'I':case'J':case'K':case'L':case'M':case'N':case'O':case'P':
    case 'Q':case'R':case'S':case'T':case'U':case'V':case'W':case'X':case'Y':case'Z':
        token->val.stringValue[0]=ch;
        for (i=1;isupper(ch =getc(fp));i++)
            token->type =lookup_reserved(token->val.stringValue);
        return token->type;
    case 'a':case'b':case'c':case'd':case'e':case'f':case'g':case'h':case 'i':case'j':case'k':case'l':case'm':case'n':case'o':case'p':
    case 'q':case'r':case's':case't':case'u':case'v':case'w':case'x':case'y':case'z':
            token->type = T_IDENTIFIER;
            token->val.stringValue[0] = ch;
            for (i=1; islower(ch= getc(fp));i++)
                token->val.stringValue[i]=ch;
            ungetc(ch,fp);
            token->val.stringValue[i] ='\0';
            if (lookup_symtab(token->val.stringValue)==NULL)
                add_symtab(token->val.stringValue);
            return T_IDENTIFIER;
            case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case '8':case'9':
                token->type=T_INTEGER;
                token->val.intValue = ch -'0';
                while (isdigit(ch = getc(fp)))
                    token->val.intValue = token->val.intValue*10+ch-'0';
                ungetc(ch,fp);
                return  T_INTEGER;

            case EOF:
                return T_END;

            default:
                token->val.intValue = ch;
                token->type = T_UNKNOWN;
                return T_UNKNOWN;
    }
}

Recommended Answers

All 13 Replies

Well, what are the errors you receive in the compiler output?

i get these errors
652bd58cc2d7b7dc82940f5a1cd861e0

As the error indicates those names are not declared at the point where you use them. In C++, the declaration of any identifier generally must come before its use. So if you want to use InitScanner and ScanOneToken on lines 43 and 44, you need to declare them before then.

The usual process is to put declarations for all functions that you define in your file at the top of your file or into a header file that you include.

i dont get you i have alredy defined them

You're defining them on lines 46 and 54 respectively. You're calling them on lines 41 and 42 (according to the line numbers in the code you've posted here - the error message says 43 and 4). 46 comes after 41 and 54 comes after 42. And as I said, "in C++, the declaration of any identifier generally must come before its use".

So either move your definitions to the top or use forward declarations.

If i am moving the code on top it is resulting in more errors :S

Then you should fix those, too.

It's not uncommon that fixing one error will produce multiple other errors that were previously unreported. That does not mean that you're going in the wrong direction (even if intuitively one might think that few error messages are better than many error messages).

Yeah, there are a couple of mistakes in there. In addition to leaving out includes (which I assume was intentional), leaving out the forward declarations (which might have been intentional as well - after all it's not a C tutorial and forward declarations don't help you understand lexing), they also forgot semicolons in a couple of place. And now I see that you copied that mistake, too, so that's one thing that will be causing errors in your code.

so should i be using forward declaraton of ts InitScanner() function ?? and where did they miss out semicolons :S

As I said already, you should either forward-declare both functions or move their defintions before main. It doesn't really matter which one though forward-declarations are more common. As for the missing semicolons, I was talking about the calls to insert_reserved. But if that's defined as a macro, the missing semicolons might in fact be intentional. Though in that case, I'd have to say it's a rather badly defined macro - macros should generally be defined in such a way that they can be followed by semicolons in the same contexts as functions are (and they should be named in ALL_CAPS).

Anyway insert_reserved doesn't seem to be defined anywhere, so that's another error.

yup i am getting errors as shown in the image

Why did you ask where the semicolons are missing if the error messages you're getting specifically say "missing ;" with a line number?

Anyway, the code uses a lot of functions (or macros) that aren't defined anywhere, so clearly it's incomplete. I don't know what to tell you beyond that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.