Stefan Mijatovic
Stefan Mijatovic

Reputation: 65

Yacc file parsing

i'm making a c to assembly compiler, and i want my program to start by giving him 2 arguments : - The source file containing the code (.c) - The destination file which will contain the assembly code generated by the compiler. Example of launch : ./compiler sum_test.c result.asm

This actually works : echo "int main(){int a; int b; int c; a = 2; b=3; c=a+b;}" | ./compiler

But i really want to read the code from a file.

I am having trouble parsing all the content off sum_test.c to STDIN, my program get's stuck and wait's for an input..

Here is my main function and what i tried :

   int main(int argc, char** argv)
{

   extern FILE * yyin;
   yyin = fopen( argv[1], "r");
   yyin = stdin;

  labelVrai = labelFaux = labelFin = labelDebut = labelSuite = labelWhileDebut = labelWhileFin = n_labels = labelN_else = 0;
  dico_global = dictionnaire_create();
  dico_local = creer_piledico();

  if ( yyparse() != 0 )
    { fprintf(stderr,"Syntaxe incorrecte\n"); return 1; }

  dictionnaire_destroy(dico_global);
  destroy_piledico(dico_local);
  return 0;
}

If you could help me it would be great , thanks.

Upvotes: 1

Views: 196

Answers (1)

happydave
happydave

Reputation: 7187

I'm not sure if there's any other issue, but you need to remove the line yyin = stdin;. It's over-writing the file pointer for the file that you've opened.

Upvotes: 4

Related Questions