compare content of file text with letters in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kinzy
    New Member
    • Apr 2009
    • 23

    compare content of file text with letters in array

    i made array that had all letter from(a-z) ,i want to compare the content of text file with this array ,i want to make that because i need to distinguish between identifier and reserved word.
    i already write code for reserved word but i can't for identifier.
    i need any idea please.
    thx.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    OK, I see an array of some type but you don't say an array of what.

    What is the nature of your comparison? All you say is "compare" without saying what kind of comparison.

    What do you mean by "reserved word" and "identifier "? And how is that related to your comparison?

    Comment

    • kinzy
      New Member
      • Apr 2009
      • 23

      #3
      i want to make program in text file this file contains program by c++,the program may be written by another language.
      reserved word like(for,if,whi le,......)i.e these word i can't write it as variable.
      identifier any word but this word must not start with digit but may contain digit in the middle like(x1,count,s um2,......).
      i want to write program in borland c++ to tell me which word will be identifier and which will be reserved word ,this will happen when i read text file .
      in borland c++ i made array contain all letter from (a-z) to compare these letter with every word in the text file ,when the first word in txt match with any letter in array ,this word will be identifier .
      i made code to tell me where is the reserved word but i don't know writing right code for this task.
      i hope my problem declared now.
      thx .

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I am not sure your approach will work.

        I suggest you write a text file that is a dictionary. Put all of your reserved words and identifers in that file. Add a code that indicates whether the word is a reserved word or a keyword.

        When you start your program, read the dictionary file into an array.

        Then, read your data file and look up the words in the dictionary array.

        If you want to get a little fancier, read your dictionary file into a map<> container and do the lookups in the container rather than goiing throug the array sequentially.

        Comment

        • kinzy
          New Member
          • Apr 2009
          • 23

          #5
          hi,
          thx for your reply.
          i want to make small compiler ,i need this compiler making check on any code to tell me which word is identifier and which isn't and when i write any word start by number,program tell me this isn't identifier and refuse it.
          i can't write identifier in text file because i don't know what the user write in text file or which program will write and which identifier will use.
          i wrote the code in borland to divid the text file into word and put it in array,i need to compare every word in array with the array of character to know if the first char is letter (i.e one char from a-z) ,i will consider this word as identifier .
          i need any idea please.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I am not at all clear on what you are trying to do.

            If your idenfiers a entered by the user and they have to be valid identifiers, then just check the first character to be and upper or lower case character or an underscore. You don't need an array of letters.

            Then, when you think it's an identifer, check the word against the dictionary of reserved words. Reserved words cannot be used as identifiers.

            Comment

            • kinzy
              New Member
              • Apr 2009
              • 23

              #7
              i want to write the letter in array because i need to make sure that the first char in word is letter ,if it is letter tell me identifier,if the user confuse and write number or symbol in the first char of word, i need output tell me, there was an error in your code

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I think all you have to do is get your word entered by user into a char array.

                Then check array[0] to be an upper or lower case letter or an underscore:

                if (
                (word[0] >= 'A' && word[0] <= 'Z')
                ||
                (word[0] >= 'a' && word[0] <= 'z')
                ||
                (word[0] == '_')

                )
                {
                /* the word MAY be an identifier */
                /* check the dictionary of reserved words here */
                /* if the word is in the dictionary,
                then the word cannot be used as an identifier */
                }
                else
                {
                /* error */
                }

                I still dp not see where your array of letters is required.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  It sounds like you want to recognize identifier definitions/declarations and react by (a) searching a dynamic symbol table to verify the identifier isn't already defined and (b) if it hasn't already been defined then add an entry for it to the symbol table.

                  If that's what you want then I perceive the following programming tasks:
                  1. Initialize symbol table to "empty".
                  2. Parse input text to recognize definitions/declarations and extract the identifier name from such an input line.
                  3. Search symbol table for matching identifier.
                  4. Add new entry to symbol table.

                  Comment

                  • kinzy
                    New Member
                    • Apr 2009
                    • 23

                    #10
                    i wrote your code inside my code but made an error(cannot convert 'char' to 'char *') i don't know why.
                    i will write my code if i have any error tell me.
                    my code:
                    Code:
                    int main (int argc, char*argv[])
                    {
                       char string[100],temp[100][32],let[26];
                       char *tokenPtr,ch,s;
                       int i=0,b,dig[10];
                       FILE *fp;
                     ///  cout<<"program\n";
                       if(argc!=2)
                        {
                          cout<<"wrong formet,format is c>compile_sh filename";
                    		getche();
                    		exit(1);
                        }
                    
                    	fp= fopen(argv[1],"r");
                    	if(fp==NULL)
                       {
                    		cout<<"cannot open file";
                    		getche();
                    		exit(2);
                    	}
                    cout<<"\nprogram\n";
                       while(fgets(string,100,fp)!=NULL)
                        {
                    		cout<<string;
                    
                    
                       	tokenPtr =strtok(string," ");//pointer to first token
                       	do
                       	{
                            	strcpy(temp[i],tokenPtr);//save the token to array
                    
                    
                              char word1[30],word2[30],word3[30],word4[30];
                     ifstream dictionary1("reserved word.txt");
                         		 ifstream dictionary2("operator.txt");
                          	 ifstream dictionary3("logic operator.txt");
                             	ifstream dictionary4("relation operator.txt");
                         		while (dictionary1>>word1)
                              	if (strcmp(word1,temp[i])==0)
                       				 cout<<temp[i]<<"\t is Reserved Word \n";
                    
                    
                        	   while (dictionary2>>word2)
                                if (strcmp(word1,temp[i])==0)
                                 	cout<<temp[i]<<"\t is operator \n";
                    
                         		while (dictionary3>>word3)
                                if (strcmp(word1,temp[i])==0)
                                  	cout<<temp[i]<<"\t is logic operator \n";
                    
                        	   while (dictionary4>>word4)
                                if (strcmp(word1,temp[i])==0)
                                  	cout<<temp[i]<<"\t is relation operator \n";
                    	 tokenPtr=strtok(NULL," ");
                    
                    
                              if((temp[0]>='A' && temp[0]<='Z')||(temp[0]>='a'&& temp[0]<='z'))----->
                    there is an error here
                                 {
                                 if(temp[0]== word1)
                                 cout<<"it is reserved word" ;
                    
                                 if (temp[0]==word2 )
                                 cout<<"it is operator";
                                 if(temp[0]==word3)
                                 cout<<"logic operator";
                                  if(temp[0]==word4)
                                  cout<<"relation operation";
                             if((temp[0]!=word1)&&(temp[0]!=word2)&&(temp[0]!=word3)&&(temp[0]!=word4))
                                 cout<<"identifier";
                                 }
                    
                              else
                              cout<<"error";
                                
                    	 } while (tokenPtr != NULL);
                     		 }
                    			getche();
                      			 fclose(fp);
                    
                    	}
                    you will find some char never used,don't care about it ,i used it in another thing ,i made this thing comment for now only.

                    Comment

                    • newb16
                      Contributor
                      • Jul 2008
                      • 687

                      #11
                      You don't know why, but at least you know where ( compiler prints line number along with error message). Please tell us what exact line the compiler doesn't like.
                      ps . don't compare c string with '==', use strcmp.

                      Comment

                      • kinzy
                        New Member
                        • Apr 2009
                        • 23

                        #12
                        the compiler refuse this statement
                        Code:
                        (if((temp[0]>='A' && temp[0]<='Z')||(temp[0]>='a'&& temp[0]<='z')))
                        where i compare c string with'==',there is no string called c in my code.
                        i modified this statement which contain error
                        Code:
                        if((temp[0][0]>='A' && temp[0][0]<='Z')||(temp[0][0]>='a'&& temp[0][0]<='z'))
                        the error removed but the output tell(if is reserved word and if is identifier)
                        the program didn't execute the part of compression with txt.

                        Comment

                        • newb16
                          Contributor
                          • Jul 2008
                          • 687

                          #13
                          Code:
                          if((temp[0]!=word1)&&(temp[0]!=word2)&&(temp[0]!=word3)&&(temp[0]!=word4)) 
                                       cout<<"identifier";
                          If you're trying to compare string with string, you're doing it wrong here.

                          Comment

                          • kinzy
                            New Member
                            • Apr 2009
                            • 23

                            #14
                            i modified this part from code ,i check this part in above part so i think there is no needing for check it again.
                            so i wrote this:
                            Code:
                            if((temp[0][0]>='A' && temp[0][0]<='Z')||(temp[0][0]>='a'&& temp[0][0]<='z'))
                                               { /*if((temp[i]!=word1)&&(temp[i]!=word2)&&(temp[i]!=word3)&&(temp[i]!=word4))*/
                                                      cout<<temp[0]<<"\t identifier \t"<<"\n";
                            
                                               }
                                               else  {
                                                   if((temp[0][0]=='[')||(temp[0][0]==']')||(temp[0][0]=='(')||(temp[0][0]==')')||(temp[0][0]==';')||(temp[0][0]=='}')||(temp[0][0]=='{'))
                                                      cout<<temp[0][0]<<"\t symbol \t"<<"\n";
                                                      }
                            the only thing i need it now ,don't write if is reserved word and identifier at the same time.
                            tell me how can i do that?.
                            thanks for you

                            Comment

                            Working...