473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with a struct{} table[]

TMS
119 New Member
I have an assignment to write a recursive descent parser. I have a struct that I've defined that has a table after it. I have NEVER done it this way before, and I'm hoping someone can help me access the table. I put the struct inside of main because that is where I've seen it done (surfing the web). If that is wrong, please say so. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. typedef enum{ 
  2.     repeat, 
  3.     begin, 
  4.     stmt, 
  5.     end, 
  6.     until, 
  7.     cond, 
  8.     semi_colon,
  9. }token_t;
  10.  
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.     struct parseMe
  15.     {
  16.     token_t tok;
  17.     char *word;
  18.     } table[] = {
  19.         {repeat, "repeat"}, {begin, "begin"}, {stmt, "stmt"},
  20.         {end, "end"}, {until, "until"}, {cond, "cond"}, {semi_colon, ";"}
  21.         };
  22.  
  23.  
  24.     ifstream infile, newFile;
  25.     char parser[] = "repeat begin stmt end until cond ;";
  26.  
  27.     char delims[] = " ";
  28.     char *result = NULL;
  29.     result = strtok(parser, delims);
  30.     while(result != NULL)
  31.     {
  32.         cout << "Token=" << result << endl;
  33.         result = strtok(NULL, delims);
  34.  
  35.     }
  36.     if(strcmp(word,"repeat")==0) tok = repeat;  //THIS IS WHERE MY PROBLEM IS!!!
  37.     cout << tok;
  38.     return 0;
  39. }
  40.  
I sort of marked where I'm having a problem. I want to compare my parser array to the table, and I'm having a real hard time figuring out how to do that. I thought my code was solid, but it seems not. I get an error saying that word is an undeclared identifier. So, I tried to make an object using the struct, but, alas, I've not really used structs that much (maybe once!!!). But this is how I was told to do it, and I'm learning, just need another bit of help.

Thank you
Feb 5 '07 #1
1 2482
Banfa
9,065 Recognized Expert Moderator Expert
if(strcmp(word,"repeat")==0) tok = repeat;

You do not have a variable named word, word is a member of your struct called parse me. You have a variable of this type called table so this could be written as

if(strcmp(table[0].word,"repeat")==0) tok = repeat;

however that would compare "repeat" (in the table) with "repeat", i.e. it will always be true.

I think what you need to do is as you tokenise your string compare the token, pointer to by result, with each entry in the table to find out which word this token is.
Feb 5 '07 #2

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

Similar topics

1
1503
by: progII | last post by:
Hello, i am struggling the whole night cos of my c++ homework. deadline is very soon. (april 4th) i have to implement a calender. i store every calender entry in a list. a calender entry is...
17
1952
by: VM | last post by:
In my Windows app, I'm running a batch process that's composed of a FOR loop that'll run 15,000 times (datatable row count), copy cthe data of each row -3 fields- to a struct, and send the strct to...
3
2126
by: monkeydragon | last post by:
hi, i am making a struct within a struct like this.. struct upper { struct lower{ BYTE* row; UINT width_row; } }
9
2031
by: Dadio | last post by:
Hi! I have to take some strings from a file and put them in a record... The various strings in the file are written on this way: string1|string2|string3|string4|string5| This is the program...
5
1569
by: pkirk25 | last post by:
It looks like the Visuakl Studio Intellisense can see the various functions but its compiler cannot. I am well out of my depth with this error message. report.obj : error LNK2019: unresolved...
5
3604
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
0
3154
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
0
7223
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
7114
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
7321
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,...
1
7034
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
7488
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.