473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

compare content of file text with letters in array

23
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.
Apr 25 '09 #1
13 6699
weaknessforcats
9,208 Expert Mod 8TB
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?
Apr 25 '09 #2
kinzy
23
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,while,......)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,sum2,......).
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 .
Apr 25 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 26 '09 #4
kinzy
23
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.
Apr 26 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 27 '09 #6
kinzy
23
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
Apr 27 '09 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 28 '09 #8
donbock
2,426 Expert 2GB
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.
Apr 28 '09 #9
kinzy
23
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:
Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char*argv[])
  2. {
  3.    char string[100],temp[100][32],let[26];
  4.    char *tokenPtr,ch,s;
  5.    int i=0,b,dig[10];
  6.    FILE *fp;
  7.  ///  cout<<"program\n";
  8.    if(argc!=2)
  9.     {
  10.       cout<<"wrong formet,format is c>compile_sh filename";
  11.         getche();
  12.         exit(1);
  13.     }
  14.  
  15.     fp= fopen(argv[1],"r");
  16.     if(fp==NULL)
  17.    {
  18.         cout<<"cannot open file";
  19.         getche();
  20.         exit(2);
  21.     }
  22. cout<<"\nprogram\n";
  23.    while(fgets(string,100,fp)!=NULL)
  24.     {
  25.         cout<<string;
  26.  
  27.  
  28.        tokenPtr =strtok(string," ");//pointer to first token
  29.        do
  30.        {
  31.             strcpy(temp[i],tokenPtr);//save the token to array
  32.  
  33.  
  34.           char word1[30],word2[30],word3[30],word4[30];
  35.  ifstream dictionary1("reserved word.txt");
  36.               ifstream dictionary2("operator.txt");
  37.            ifstream dictionary3("logic operator.txt");
  38.              ifstream dictionary4("relation operator.txt");
  39.              while (dictionary1>>word1)
  40.               if (strcmp(word1,temp[i])==0)
  41.                     cout<<temp[i]<<"\t is Reserved Word \n";
  42.  
  43.  
  44.            while (dictionary2>>word2)
  45.             if (strcmp(word1,temp[i])==0)
  46.                  cout<<temp[i]<<"\t is operator \n";
  47.  
  48.              while (dictionary3>>word3)
  49.             if (strcmp(word1,temp[i])==0)
  50.                   cout<<temp[i]<<"\t is logic operator \n";
  51.  
  52.            while (dictionary4>>word4)
  53.             if (strcmp(word1,temp[i])==0)
  54.                   cout<<temp[i]<<"\t is relation operator \n";
  55.      tokenPtr=strtok(NULL," ");
  56.  
  57.  
  58.           if((temp[0]>='A' && temp[0]<='Z')||(temp[0]>='a'&& temp[0]<='z'))----->
  59. there is an error here
  60.              {
  61.              if(temp[0]== word1)
  62.              cout<<"it is reserved word" ;
  63.  
  64.              if (temp[0]==word2 )
  65.              cout<<"it is operator";
  66.              if(temp[0]==word3)
  67.              cout<<"logic operator";
  68.               if(temp[0]==word4)
  69.               cout<<"relation operation";
  70.          if((temp[0]!=word1)&&(temp[0]!=word2)&&(temp[0]!=word3)&&(temp[0]!=word4))
  71.              cout<<"identifier";
  72.              }
  73.  
  74.           else
  75.           cout<<"error";
  76.  
  77.      } while (tokenPtr != NULL);
  78.           }
  79.             getche();
  80.                fclose(fp);
  81.  
  82.     }
  83.  
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.
Apr 29 '09 #10
newb16
687 512MB
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.
Apr 29 '09 #11
kinzy
23
the compiler refuse this statement
Expand|Select|Wrap|Line Numbers
  1. (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
Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 30 '09 #12
newb16
687 512MB
Expand|Select|Wrap|Line Numbers
  1. if((temp[0]!=word1)&&(temp[0]!=word2)&&(temp[0]!=word3)&&(temp[0]!=word4)) 
  2.              cout<<"identifier"; 
  3.  
If you're trying to compare string with string, you're doing it wrong here.
Apr 30 '09 #13
kinzy
23
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:
Expand|Select|Wrap|Line Numbers
  1. if((temp[0][0]>='A' && temp[0][0]<='Z')||(temp[0][0]>='a'&& temp[0][0]<='z'))
  2.                    { /*if((temp[i]!=word1)&&(temp[i]!=word2)&&(temp[i]!=word3)&&(temp[i]!=word4))*/
  3.                           cout<<temp[0]<<"\t identifier \t"<<"\n";
  4.  
  5.                    }
  6.                    else  {
  7.                        if((temp[0][0]=='[')||(temp[0][0]==']')||(temp[0][0]=='(')||(temp[0][0]==')')||(temp[0][0]==';')||(temp[0][0]=='}')||(temp[0][0]=='{'))
  8.                           cout<<temp[0][0]<<"\t symbol \t"<<"\n";
  9.                           }
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
Apr 30 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
31
by: Royal Denning | last post by:
I am designing a table with 2 columns and 20 rows. I want to insert small images (each with a link) and a text title in each cell of the table. On refresh of the page, I would like to have the...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
6
by: RSH | last post by:
Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk...
6
by: Justin Fancy | last post by:
Hi Everyone, I'm lookin for a very confusing loop (to me), to compare two files. Here it is. I have two arrays with paths stored in both. example: /en/aviation/you.htm. I need to search array...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
5
by: Davo1977 | last post by:
Analysing text files to obtain statistics on their content You are to write a Perl program that analyses text files to obtain statistics on their content. The program should operate as follows: ...
3
by: madankarmukta | last post by:
Hi, Objectives is to write the DataTable content to the text file in the format Col1 | Col2 | Col3 ------|-------|--------- Val11 | Val21 | Val31 Val12 | Val22 | Val32 Val13 | Val23 |...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.