2

So, the thing is I'm trying to do a lexical analyzer and I call it from a syntactical analyzer.

I have this:

int lexico(char linea[MAXLINEA])
{
   if (linea[0] == '1') return 1;
   else if (linea[0] == '2') return 2;
   else if (linea[0] == '3') return 3;
   else if (linea[0] == '4') return 4;
   else return -1;
   return 0;                
}

int main(int argc, char *argv[])
{
   ...
   FILE *fichero;
   fich = fopen("test", "r");
   ...
   int l;
   char buffer[MAXLINEA];
   while(fgets(buffer,MAXLINEA,fich) != NULL){
      l = lexico(buffer);
      ...
   }
}

This is just a poor example.

So imagine I have something in linea[1]and, after the first return to main, I want to return to lexico and continue analyzing linea[1]. Is it possible?

Thank you in advance.

PS: I know I can do this, for example, using Struct and saving several int values but I don't want to use it.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Zariweya
  • 295
  • 3
  • 13
  • well, you can use the function at your will. only, you need to stop calling that on reaching certain condition. – Natasha Dutta Dec 04 '14 at 11:42
  • By the way the `return 0;` in your fucntion is unneccessary, as you have a full if/else where in each case you return, so the end of the function body CANT be reached ;) – dhein Dec 04 '14 at 11:43
  • @NatashaDutta thank you for the answer but I did not understand what you mean. – Zariweya Dec 04 '14 at 11:44
  • Are threads a option for that what you try to achive? so you could just don't return and when ever something is achived what you want to inform the main about, do put it in some shared memory which the main is checking. – dhein Dec 04 '14 at 11:44
  • @Zariweya So what is about the idea of using multiple threads for that "analysis"? Sounds to me as this is what you are looking for ;) – dhein Dec 04 '14 at 11:48
  • 1
    @DavidHeffernan I would love to get an explanation why you think so. I mean he wants to return a fucntion without leaving its scope, so returning some information but keep proccessing the fucntion it self. Thats exactly what a thread does. So instead of just throwing in rude comments about other people ideas, at least state why it is "appalling" And maybe suggest a better idea that is fitting the OP's requirments. – dhein Dec 04 '14 at 11:58
  • 2
    @Zaibis That's not what a thread does. Threads are distinct execution contexts. If he wants to remain in a function that he can just do that. A callback can be used to pass information out as it is processed. For instance. Or the function can return the current position in the string to allow the caller to call it back to continue where it left off. There are plenty of good ways to go. I don't see any reason to believe that lexing and parsing is a good for threads. Do you have examples of widely accepted lexers/parsers that do so? – David Heffernan Dec 04 '14 at 12:05
  • @DavidHeffernan But what use would you get from using a callback if there is nothing handling outside the returned values? I was thinkign of that first too. But to me it sounded as he wants the function be running on whilest the main is checking some results. so as I understood he want two units that do 2 different things at same time. maybe I got him wrong. but if not, I don't understand how a callback could do that. – dhein Dec 04 '14 at 12:26
  • @Zaibis That's a completely different reading of the question than mine. – David Heffernan Dec 04 '14 at 12:29
  • @Zariweya that means the if-else branches have covered all possible cases of the input and your program will return before reaching `return 0;`. Turn on all warnings on the compiler and you'll probably get a warning about that (deadcode) too – phuclv Dec 04 '14 at 12:43

2 Answers2

2

If you want a simpler solution, you could add an another argument(with pass by reference method) to lexico function call and use a while loop for calling from main fucntion.

int lexico(char linea[MAXLINEA], int *rccnt)
{
   (*rccnt)++;
   ......
   return 1;
}


int main(int argc, char *argv[])
{
   ...
   FILE *fichero;
   fich = fopen("test", "r");
   ...
   int l;
   char buffer[MAXLINEA];
   int chrcnt;
   while(fgets(buffer,MAXLINEA,fich) != NULL){
      chrcnt = 0; //set to zero for every buffer fetch
      while(chrcnt < strlen(buffer))
          l = lexico(buffer, &chrcnt);

      ...
   }
}

Hope this answer solves your problem.

UserM
  • 190
  • 2
  • 19
0

As a supply for previous answer, you can call lexico with lexico(&buffer[index]), and increase index in you code, for int lexico(char linea[MAXLINEA]) is actual the same as int lexico(char *linea) in C. Check this if you want to know more detail about this Difference between passing array and array pointer into function in C

Community
  • 1
  • 1
D3Hunter
  • 1,329
  • 10
  • 21