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.
13 6699
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?
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 .
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.
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.
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.
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
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.
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.
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: - 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.
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.
the compiler refuse this statement - (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 - 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.
-
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.
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: - 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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: ...
|
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 |...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |