I'm using the LEX generator to do a lexical analysis of a simple code in C ++. The following code is from the generator:
%{
#include<stdio.h>
%}
extern FILE *yyin;
%%
"<" {printf("(Identifier, %s)\n",&yytext[0]);}
">" {printf("(Identifier, %s)\n",&yytext[0]);}
"+" {printf("operador de soma %s\n",&yytext[0]);}
"-" {printf("operador de subtracao %s\n",&yytext[0]);}
%%
int yywrap(){
return 1;
}
int main(){
yyin=fopen("cpp.cpp","r");
yylex();
fclose(yyin);
return;
}
As you can see, I used the 'yyin' variable to call the cpp.cpp file to do lexical analysis. I would rather than print, the text should be written to the cpp.cpp file. That is, the result for each lexeme was written in the analyzed code itself. Cpp file:
#include<iostream>
using namespace std;
int main(){
int a = 0;
int b = 2;
int c = a + b;
cout << c << endl;
return 0;
}
* I know you have lexemes that will not be found by the grammar, but it's just an example to explain that I want the lexemes found to be written inside the cpp.cpp file (as if instead of the function 'printf' it was a function fprintf - which writes to a file, using C, or a RETURN so I can write to the file inside the main method).