Lexicon Analyzer using LEX

4

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).

    
asked by anonymous 13.07.2016 / 16:56

1 answer

0

Do you need to write at the end of the file? or can you overwrite it? if you are using linux and can override the cpp.cpp file do the following.

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

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

    yylex();
    fclose(yyin);   
    return 0;
}

And when you run open the terminal with ctrl + alt + t and do ./executable < cpp.cpp

    
20.07.2016 / 01:00