please can you help me to translate this code from c to c++
/***************************************************************
Program describtion :
=====================
This program is for creating a Lexical Analyzer in c

Created by :
=============
CoMPuTeRQ8

kuwait university
Major : COMPuTeR-SCIeNCe


*****************************************************************/


/****************************************************************
Necessary Header files used in program.
*****************************************************************/

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>

/****************************************************************
Functions prototype.
*****************************************************************/

void   Open_File();
void   Demage_Lexeme();
int    Search(char[256],int);
void   analyze();
void   Skip_Comment();
void   Read_String();
void   Is_Keyword_Or_Not();
void   Is_Identifier_Or_Not();
void   Is_Operator_Or_Not();
void   Read_Number();
void   Is_Special_Or_Not();
void   Is_Comparison_Or_Not();
void   Add_To_Lexical (char[256],int,char[256]);
void   Print_ST();
void   Print_TOKEN();
void   Token_Attribute();

/****************************************************************
Data structure used in program.
*****************************************************************/

struct lexical
{
    char    data[256];          //Value of token.
    int     line[256];          //Line # which token appear in input file.
    int     times;              //# of times that token appear in input file.
    char    type[256];           //Type of each token.
    struct  lexical *next;
};

typedef struct lexical Lex;
typedef Lex *lex;

/****************************************************************
File pointer for accessing the file.
*****************************************************************/

FILE *fp;
FILE *st;
FILE *token;
char lexeme[256],ch;
int f,flag,line=1,i=1;
lex head=NULL,tail=NULL;

/****************************************************************
Array holding all keywords for checking.
*****************************************************************/

char *keywords[]={"procedure","is","begin","end","var","cin","cout","if",
		  "then","else","and","or","not","loop","exit","when",
		  "while","until"};

/****************************************************************
Array holding all arithmetic operations for checking.
*****************************************************************/

char arithmetic_operator[]={'+','-','*','/'};

/****************************************************************
Array holding all comparison operations for checking.
*****************************************************************/

char *comparison_operator[]={"<",">","=","<=","<>",">="};

/****************************************************************
Array holding all special for checking.
*****************************************************************/

char special[]={'%','!','@','~','$'};

/****************************************************************

			**************
			*MAIN PROGRAM*
			**************

*****************************************************************/

void main()
{
  Open_File();
  analyze();
  fclose(fp);
  Print_ST();
  Print_TOKEN();
}

/****************************************************************
This function open input sourse file.
*****************************************************************/

void Open_File()
{

  fp=fopen("source.txt","r");   //provide path for source.txt here
  if(fp==NULL)
  {
	printf("!!!Can't open input file - source.txt!!!");
	getch();
	exit(0);
  }
}

/****************************************************************
Function to add item to structure of array to store data and
information of lexical items.
*****************************************************************/

void Add_To_Lexical (char value[256],int line,char type[256])
{
	lex new_lex;

	if (!Search(value,line))    //When return 1 the token not found.
	{

	  new_lex=malloc(sizeof(Lex));

	  if (new_lex!=NULL)
	  {
		strcpy(new_lex->data,value);
		new_lex->line[0]=line;
		new_lex->times=1;
		strcpy(new_lex->type,type);
		new_lex->next=NULL;

		if (head==NULL)
		   head=new_lex;
		else
		   tail->next=new_lex;

		tail=new_lex;
	  }
	}
}

/****************************************************************
Function to search token.
*****************************************************************/

int Search (char value[256],int line)
{
  lex x=head;
  int flag=0;

  while (x->next!=NULL && !flag)
  {
    if (strcmp(x->data,value)==0)
    {
      x->line[x->times]=line;
      x->times++;
      flag=1;
    }
    x=x->next;
  }
  return flag;
}

/****************************************************************
Function to print the ST.TXT .
*****************************************************************/

void Print_ST()
{
  lex x=head;
  int j;

  if ((st=fopen("ST.TXT","w"))==NULL)
      printf("The file ST.TXT cat not open. \n");

  else

  {
    fprintf(st,"\t %s \t    %s \t %s \n","Line#","Lexeme","Type");
    fprintf(st,"\t ---- \t    ------ \t ---- \n");

    while (x!=NULL)
    {
      if ((strcmp(x->type,"num")==0)         ||
	 (strcmp(x->type,"keyword")==0)      ||
	 (strcmp(x->type,"identifier")==0))
      {
	 fprintf(st,"\t ");

	 for (j=0;j<x->times;j++)
	 {
	   fprintf(st,"%d",x->line[j]);
		if (j!=x->times-1)      //This condition to prevent the comma
	   fprintf(st,",",x->line[j]);  //"," to not print after last line #.
	 }

	fprintf(st,"\t    %-6s   \t%-6s \n",x->data,x->type);
      }
      x=x->next;
    }

    fclose(st);
  }
}

/****************************************************************
Function to print the TOKENS.TXT .
*****************************************************************/

void Print_TOKEN()
{
  int flag=0;

  fp=fopen("source.txt","r");

    if(fp==NULL)
    {
       printf("!!!Can't open input file - source.txt!!!");
       getch();
       exit(0);
    }

  else

    {
	if ((token=fopen("TOKENS.TXT","w"))==NULL)
	  printf("The file ST.TXT cat not open. \n");

      else

      {
	ch=fgetc(fp);

	while (!(feof(fp)))
	{

	  if (ch==' ' && !flag)
	  {
	    do
	      ch=fgetc(fp);
	    while (ch==' ');

	    fseek(fp,-2,1);
	    ch=fgetc(fp);
	    flag=1;
	  }

	  if (ch!='\n' && ch!='\t')
	    fprintf(token,"%c",ch);

	  if (ch=='\n')
	  {
	    fprintf(token,"\n");
	    Token_Attribute();
	    i++;
	    flag=0;
	  }

	  ch=fgetc(fp);
	}
      }
    }
    fclose(fp);
    fclose(token);
}

/****************************************************************
Function to put the token and atrribute in TOKENS.TXT .
*****************************************************************/

void Token_Attribute()
{
  lex x=head;
  int j;

  while (x!=NULL)
  {
    if (x->line[0]==i)
    {
      fprintf(token,"token : %-4s\t",x->type);

      if ((strcmp(x->type,"num")==0)         ||
	 (strcmp(x->type,"keyword")==0)      ||
	 (strcmp(x->type,"identifier")==0))

      {
	  fprintf(token,"attribute : line#=%-4d \n",i);
      }

      else

      {
	  fprintf(token,"attribute : %-4s \n",x->data);
      }

    }
    x=x->next;
  }
  fprintf(token,"\n");
}

/****************************************************************
Function to create lexical analysis.
*****************************************************************/

void analyze()
{

  ch=fgetc(fp);                      //Read character.

  while(!feof(fp))                   //While the file is not end.
  {

      if(ch=='\n')                   //Compute # of lines in source.txt .
	  {
	    line++;
	    ch=fgetc(fp);
	  }

      if(isspace(ch) && ch=='\n' )
      {
	  line++;
	  ch=fgetc(fp);
      }
      if(isspace(ch) && ch!='\n' )          //The character is space.
	  ch=fgetc(fp);


      if(ch=='/' || ch=='"')    //Function for skipping comments in the file
	  Skip_Comment();	//and '"' with display statements.


      if(isalpha(ch))              //The character is leter.
	{
	    Read_String();
	    Is_Keyword_Or_Not();
	    Is_Operator_Or_Not();
	    Is_Identifier_Or_Not();
	}


      if(isdigit(ch))             //The character is digit.
	 Read_Number();


      if (ch==';')                //The character is semicolon.
	Add_To_Lexical(";",line,"semicolon");


      if (ch==':')                //The character is colon.
	Add_To_Lexical(":",line,"colon");


      if (ch==',')                //The character is comma.
	Add_To_Lexical(",",line,"comma");


      if (ch=='(')                //The character is parenthesis.
	Add_To_Lexical("(",line,"parenthesis");


      if (ch==')')                //The character is parenthesis.
	Add_To_Lexical(")",line,"parenthesis");

				 //The character is comparison_operator
      if (ch=='<' || ch=='=' || ch=='>')
	Is_Comparison_Or_Not();


      Is_Special_Or_Not();       //After failed scaning in before cases
				 //check the character is special or not.
      Demage_Lexeme();

      if(isspace(ch) && ch=='\n' )
      {
	  line++;
	  ch=fgetc(fp);
      }
      else
      ch=fgetc(fp);
  }
}

/****************************************************************
This function read all character of strings.
*****************************************************************/

void Read_String()
{
  int j=0;

  do
  {
    lexeme[j++]=ch;
    ch=fgetc(fp);
  } while(isalpha(ch));

    fseek(fp,-1,1);
    lexeme[j]='\0';
}

/****************************************************************
This function demage the data stored in the lexeme to store
next word without any previous character of previous word .
*****************************************************************/

void Demage_Lexeme()
{
  int j=0;

  while (lexeme[j]!='\0')
  {
    lexeme[j]='\0';
    j++;
  }
}

/****************************************************************
This function check the string is keyword or not.
*****************************************************************/

void Is_Keyword_Or_Not()
{
  int j=0;

  flag=0;

  for (j=0;j<18;j++)         //search for keyword & # of keywords = 18.
  {
    if (strcmp(lexeme,keywords[j])==0)
	{
		Add_To_Lexical(lexeme,line,"keyword");
		flag=1;
		break;
	}
  }
}

/****************************************************************
This function check the string is indentifier or not.
*****************************************************************/

void Is_Identifier_Or_Not()
{
    if(flag==0)   //identifier
       Add_To_Lexical(lexeme,line,"identifier");
}

/*****************************************************************
This function check if the character is operator or not.
******************************************************************/

void Is_Operator_Or_Not()
{
  int j;

      for (j=0;j<4;j++)               //The # of arithmetic_operator is 4.
	if (ch==arithmetic_operator[j])
	{
	  lexeme[0]=ch;
	  lexeme[1]='\0';
	  Add_To_Lexical(lexeme,line,"operator");
	  break;
	}
}

/*****************************************************************
This function check if the character is special symbol or not.
******************************************************************/

void Is_Special_Or_Not()
{
  int j;

  for (j=0;j<5;j++)               //The # of special symbol is 5.
    if (ch==special[j] )
      {
	lexeme[0]=ch;
	lexeme[1]='\0';
	Add_To_Lexical(lexeme,line,"special");
	break;
      }

}

/*****************************************************************
This function check if the character is special symbol or not.
******************************************************************/

void Is_Comparison_Or_Not()
{
    int j,flag=0;
    char k=ch;

    ch=fgetc(fp);

    if (isalpha(ch) && k=='<')         //Skip words between #include<>.
    {
      do
	ch=fgetc(fp);
      while (ch!='>');
      flag=1;
    }

    else

    if (ch=='<' && k=='<')            //Skip cout <<.
      flag=1;

    else

    if (ch=='>' && k=='>')            //Skip cin >>.
      flag=1;

    if (!flag)
    {

      lexeme[0]=ch;          //We read here to check if the comparision
      ch=fgetc(fp);          //operator is single which is only (> or < or =)
			     //or like ( >= or == or <= ).

      if (ch=='<' || ch=='=' || ch=='>')
      {
	lexeme[1]=ch;
	lexeme[2]='\0';
      }
      else
      {
	lexeme[1]='\0';
	fseek(fp,-1,1);
      }

      for (j=0;j<6;j++)      //The # of comparison_operator is 6.
	if (strcmp(lexeme,comparison_operator[j])==0)
	  {
	    Add_To_Lexical(lexeme,line,"comparision");
	    break;
	  }
   }
}


/*****************************************************************
This function check if the character is number or not.
******************************************************************/

void Read_Number()
{

   int j=0;

   while(isdigit(ch) || ch=='.')  //search for digits
   {
      lexeme[j]=ch;
      ch=fgetc(fp);
      j++;
   }

   lexeme[j]='\0';
   Add_To_Lexical(lexeme,line,"num");

}

/****************************************************************
Function to skip comment statements.
*****************************************************************/

void Skip_Comment()
{
    if (ch=='"')             //Skipping printf statements and
    {                        //other such display statements.
	if(ch=='"')
	while((ch=getc(fp))!='"');
    }

    else
		 //Skip comments.
    {
	ch=getc(fp);
	if(ch=='/')	//Checking single line comments
	{
		while((ch=getc(fp))!='\n');
	}
	else if(ch=='*')	//Checking multiple line comments.
	{
		while(f==0)
		{
			ch=getc(fp);
			if(ch=='*')
			{
				ch=getc(fp);
				if(ch=='/')
					f=1;
			}
		}
		f=0;
       }
    }
}

Why don't you do it yourself? It will take a lot of effort to change FILE to ifstream/ofstream, and character arrays to std::string. You would be better to do the conversion yourself instead of asking others to do it for you for free.

Of course you could always just use the code as it is.

It is much more reasonable to note which statements need to be converted, and then ask google how to convert them. If google doesn't know (!?) then we'll help you.

>You would be better to do the conversion yourself instead of asking others to do it for you for free.
Yes, 100% agreed :)
To the OP: It will only cost you effort.

To the OP:
One thing you should remember for always and for ever is that: you should never (and take this litterally), but never use void main() .
This is fully against the C++ standard.
According to the standard the main() function can only have int as it's return type, in other words: you must use int main() in your code, no way around.

And now practical (in case you didn't understand what I'm saying):
Change [B]void main()[/B] to [B]int main()[/B] :)

That's already a good starting point eh?

please can you help me to translate this code from c to c++
/***************************************************************
Program describtion :
=====================
This program is for creating a Lexical Analyzer in c

Created by :
=============
CoMPuTeRQ8

kuwait university
Major : COMPuTeR-SCIeNCe

Are you asking us to do your homework for you??!! :-O Bad boy. Hehehehehe.....

Looks like you'll have to:

1) Change the headers:

#include <string.h> to #include <string> for example

2) Update the way you open the file: fstream (ifstream/ofstream...)

3) Change printf to cout

4) Change char arrays to strings

Instead of trying to convert line-by-line, try converting function-by-function. First thing to do is create a c++ class then make all those functions class methods. Next, replace the linked list with a vector of structures. Avoid use of all those global variables -- pass them as parameters to the functions which use them.

1) Change the headers: #include <string.h> to #include <string> for example

You intended it the good way, but this is wrong.
string.h is not the same as string.
string.h is a header file which contains string functions for character strings (C-style strings), such as strcat or strcpy or ...(link) .
So to convert it the correct way: change #include <string.h> to #include <[B]c[/B]string> .

How many computer science students are studying in your university ?

Let me give you a hint , you need a class named Token , move all the data and functions related to token to that class.

commented: Wait, is this a "lightbulb" joke? ;) +36

You intended it the good way, but this is wrong.
string.h is not the same as string.
string.h is a header file which contains string functions for character strings (C-style strings), such as strcat or strcpy or ...(link) .
So to convert it the correct way: change #include <string.h> to #include <[B]c[/B]string> .

Thanks for the correction. You're absolutely correct. I'm new to C++ but I can recall reading about this a while back.... my bad. Innocent mistake. Sorry.

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.