Hello again good people,
i have done some coding on lexical analyzer phase of compiler but it is giving me some error that i am unable to understand..
Fair Warning: Code is a little longer.

Here is the code:

 # include <iostream>
 # include <fstream>
 # include <string.h>
 # include <stdlib.h>
 # include <stdio.h>
 # include <conio.h>
 # include <ctype.h>

using namespace std;


 const int Rows = 34;
 const int Cols = 15;

 int iTT[Rows][Cols] = {0};


 void loadTransitionTable( )
 {
    fstream File("tt.txt");

    if (!File)
    {
       cout << "\n Cannot open the input file." << endl;
       cout << "\n Press any key to exit.";

       getch( );
       exit(0);
    }

    char sInput[100]= {NULL};

    for (int i = 0; i < Rows; i ++)
    {
       strset(sInput, NULL);

       File.getline(sInput, 80);

       char *sPtr=NULL;

       sPtr = strtok(sInput, " ");

       iTT[i][0] = atoi(sPtr);

       for(int j = 1; j < Cols; j ++)
       {
      sPtr=strtok(NULL, " ");

      iTT[i][j] = atoi(sPtr);
       }
    }

    File.close( );
 }


 int getNextState(int iState, char cChar)
 {
    if (isalpha(cChar))
       return iTT[iState][1];

    else if (isdigit(cChar))
       return iTT[iState][2];

    else if (cChar == '.')
       return iTT[iState][3];

    else if (cChar == '"')
       return iTT[iState][4];

    else if (cChar == '_')
       return iTT[iState][6];

    else if (cChar == '+')
       return iTT[iState][7];

    else if (cChar == '=')
       return iTT[iState][8];

    else if (cChar == '-')
       return iTT[iState][9];

    else if (cChar == '%')
       return iTT[iState][10];

    else if (cChar == '!')
       return iTT[iState][11];

    else if (cChar == '>')
       return iTT[iState][12];

    else if (cChar == '<')
       return iTT[iState][13];

    else if (cChar == '/')
       return iTT[iState][14];

    return iTT[iState][0];
 }


 int isKeyword(char* sToken)
 {
    if (strlen(sToken) > 16 || strlen(sToken) == 0)
       return 0;

    char sKeywords[64][20] = {
                   "asm","auto","bool","break","case","catch",
                   "char","class","const","const_cast",
                   "continue","default","delete","do","double",
                   "dynamic_cast","else","enum","explicit",
                   "export","extern","false","float","for",
                   "friend","goto","if","inline","int","long",
                   "main","mutable","namespace","new",
                   "operator","private","protected","public",
                   "register","reinterpret_cast","return",
                   "short","signed","sizeof","static",
                   "static_cast","struct","switch","template",
                   "this","throw","true","try","typedef",
                   "typeid","typename","union","unsigned","using",
                   "virtual","void","volatile","wchar_t","while"
                 };

    for(int iCount = 0; iCount < 64; iCount ++)
    {
       if (strcmpi(sKeywords[iCount], sToken) == 0)
      return 1;
    }

    return 0;
 }

 int main( )
 {
    char input; 
     int token; 
    int len; 
    loadTransitionTable( );


    fstream File("input.txt");
    cout<<"Please enter the expression to be tokenized: \n";
        File>>input;
        len=strlen(input); token=0;

    if (!File)
    {
       cout<<"\n Unable to open the input file."<<endl;
       cout<<"\n Press any key to exit.";

       getch( );
       exit(0);
    }

    char sToken[255] = {NULL};
    int iTokenIndex = 0;

    char cChar = NULL;
    int iState = 0;
    int iFlag = 0;

    char cTemp = File.get( );

    do
    {
       Start:

       if (iFlag == 0)
       {
      cChar = cTemp;
      cTemp = File.get( );
       }

       else
      iFlag = 0;

       if (cChar == '/' && cTemp == '/')
       {
      while(File.get( ) != '\n')
      {
         if (File.eof( ))
        goto End;
      }

      cout<<'\r';

      cTemp = File.get( );

      goto Start;
       }

       if (cChar == '/' && cTemp == '*')
       {
      cTemp = File.get( );

      do
      {
         cChar = cTemp;
         cTemp = File.get( );

         if (File.eof( ))
        goto End;
      }
      while(cChar != '*' && cTemp != '/');

      cout<<'\r';

      cTemp = File.get( );

      goto Start;
       }

       iState = getNextState(iState, cChar);

       switch (iState)
       {
      case  0 :  cout << cChar;

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      //case  1 :
      //case  3 :
      //case  5 :
      //case  7 :
      //case 10 :
      //case 14 :
      //case 18 :
      //case 25 :
      case 26 :  sToken[iTokenIndex] = cChar;
             iTokenIndex ++;

             break;

      case  2 :  if (isKeyword(sToken))
            cout << sToken;

             else
            cout << "<ID>";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  4 :  cout << "<INT>";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  6 :  cout << "<FLOAT>";

             iState = 0;
             iTokenIndex = 0;
             iFlag = 1;

             strset(sToken, NULL);

             break;

      case  8 :  cout << "<STR>";

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      //case  9 :
      //case 11 :
      //case 12 :
      //case 13 :
      //case 15 :
      //case 16 :
      //case 17 :
      //case 19 :
      //case 20 :
      //case 21 :
      //case 22 :
      //case 23 :
      //case 24 :
      //case 27 :
      case 28 :  cout << "<OPR>";

             if (cChar != '+' && cChar != '-' && cChar != '/'
                && cChar != '>' && cChar != '<' && cChar != '=')
            iFlag = 1;

             iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;

      //case 30 :
      case 33 :  iState = 0;
             iTokenIndex = 0;

             strset(sToken, NULL);

             break;
       }
    }
    while(!File.eof( ));

    End:

    getch( );
    return 0;
 }

And the errors i am getting are :

H:\New folder\Analyzer.cpp In function 'void loadTransitionTable()':
33 28 H:\New folder\Analyzer.cpp [Warning] converting to non-pointer type 'char' from NULL [-Wconversion-null]
37 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
H:\New folder\Analyzer.cpp In function 'int main()':
146 19 H:\New folder\Analyzer.cpp [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
5 0 H:\New folder\Analyzer.cpp In file included from H:\New folder\Analyzer.cpp
49 40 d:\dev-cpp\mingw32\include\string.h [Error] initializing argument 1 of 'size_t strlen(const char*)' [-fpermissive]
157 29 H:\New folder\Analyzer.cpp [Warning] converting to non-pointer type 'char' from NULL [-Wconversion-null]
160 18 H:\New folder\Analyzer.cpp [Warning] converting to non-pointer type 'char' from NULL [-Wconversion-null]
224 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
251 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
261 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
271 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
280 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
307 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]
315 27 H:\New folder\Analyzer.cpp [Warning] passing NULL to non-pointer argument 2 of 'char* strset(char*, int)' [-Wconversion-null]

Ignoring the warning ofc. Any kind of help is deeply appreciated.
Thanks!

Recommended Answers

All 5 Replies

len=strlen(input);

input is a char. A single char. The function strlen takes a char* parameter. You cannot use a char instead of a char pointer.

Did you really mean to read just a single character from the input file?

From what I can see, you're trying to convert a char* to a const char* some where in your code, so check over you arguments and function calls from files they might only handle const char*. size_t is data so you need to assign it a varable name size_t i = strlen(string); and strlen has to have an argument which you have missed out or the converasion has stoped it from being passed in.

Here are afew tips.
Try avoid goto statements if you can, where you have said goto END: just return 0; in main function this will exit the program, also you can replace exit(0) with return 0;. For the goto start statment it might be a better if you make the goto start part of the program it's own function so when you need to run that part of the code you just need to call the function.

I hope this help you out :)

A few questions and comments, not directly relevant to your problem but possibly important:

  • What version of Dev-C++ are you using? The older Bloodshed edition is now ten years old, andif you are using that, you should switch to the newer Orwell fork, which is in current development and supports the newest version of GCC.

  • The C-style headers should all be changed to the modern C++ form, for example, <stdio.h> should be <cstdio>, <ctype.h> should be <cctype>, etc.

  • I see that you are using C=strings rather than C++ string objects. Is there a particular reason for this? I would expect C++ strings to be much easier to work with for these purposes.

  • The <conio.h> header and the console functions associated with it are not standard C++, and modern libraries generally don't support it. It is not recommended that it be used in any new programs.

  • It is best to avoid using the using namespace std; in large programs, as it leads to namespace conflicts. It is recommended that you either explicitly scope all references (e.g., std::cout), or if necessary, scope specific items (e.g., using std::cout;), rather than importing the entire namespace.

  • Using bare C-strings for tokens is inadvisable, as you will need to analyze the token for its classification repeatedly. The usual approach is to have a structure type or even a class for tokens, consisting of both the token string and a type tag.This makes checking the token type a simple matter of checking the tag, once the token has been processed.

    enum TokenType 
    {
        INVALID, 
        IDENTIFIER,         
        CHARLIT, INTLIT, FLOLIT, STRLIT.   // literals 
        IFTOK, WHILETOK, FORTOK,           // etc, ... keywords
        LPAREN, RPAREN, LBRACE, RBRACE,    // etc, ...                  
    };
    
    struct Token 
    {
        std::string token;
        TokenType type;
    };
    

    Filling out the rest of the TokenType enumeration is left as an exercise.

@moschops: i want to read the stuff in input file character by character.

@verterF3: thanks for your input :)

@Schol: i'll see what i can do what piece of info you have given me, thanks alot!

No problem, I hope you solve the problem with the help we have given you :)

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.