473,569 Members | 2,634 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 2488
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
1510
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 a struct with entry, date, time, type. the problem is: if i use the change-function to add a (optional) time to an
17
1961
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 an external method (using DLLImport). The problem is that the processing speed is very inconsistent- it may run very FAST during certain points...
3
2129
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
2039
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 that i have just made...what's wrong? #include<stdio.h> #include<conio.h> #include<string.h>
5
1574
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 external symbol "private: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char,class...
5
3611
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 anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the...
0
3159
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 fine. Now we decided to move from mainframe IMS-DB2 to Windows 2003 server-DB2 UDB for LUW 9.5.
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.