0

I'm doing a translation of acronyms. That is, if it finds 'OMS' print 'Organización Mundial del trabajo', but once I compile and run the program it runs infinitely.

I'm using Windows as a development environment. I have seen examples but I can't see where the error is. Here is the code:

%option noyywrap    
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
%}

%%
OMS {fprintf(yyout,"Organización Mundial del trabajo");}

%%

int main(int argc, char *argv[]) {
    FILE*yyin=fopen(argv[1],"r");
    FILE*yyout=fopen(argv[2],"w");
    yylex();
    fclose(yyin);
    fclose(yyout);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alfito
  • 11
  • 2
  • 2
    `FILE*yyin=fopen(argv[1],"r");` you are declaring and initializing a local unused variable, while global `yyin` remains unchanged. – n. m. could be an AI Oct 26 '14 at 04:45
  • @n.m.: Your diagnosis is basically correct — I suggest adding it as an answer so the question can be given closure. Strictly, the local variable `yyin` in `main()` _is_ used in the corresponding `fclose()` (for all that it is a vacuous usage), but you're absolutely correct that the lexical analyzer will be using the global variable `yyin` (which is hidden in `main()` by the local definition). Alfito: If you use GCC, you could use `-Wshadow` to get a warning about this problem. – Jonathan Leffler Oct 26 '14 at 13:44

1 Answers1

1
FILE*yyin=fopen(argv[1],"r");
FILE*yyout=fopen(argv[2],"w");

These lines declare and initialize two local variables named yyin and yyout. They are closed in the end of the function, but otherwise remain unused (that is, no one does any input/output with them). They are not available to the rest of the program. Meanwhile, global variables yyin and yyout, which are entirely separate from these local variables, remain untouched.

What you need to do is simply remove FILE* from both lines:

yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");

Now the names yyin and yyout refer to the global variables that are known to the rest of the program.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243