Redirect Flex input to a string

1

Good,

I have a flex file and a routine in C to check text writing.

I've been testing on the command-prompt using test < inputFile What I wanted now was to be able to indicate in my C file a parameter (a string) that it then checked (instead of being stdin).

How can I add this?

    
asked by anonymous 19.02.2016 / 17:36

2 answers

0

You can pass the file by argument.

./ test inputFile

That's what argc and argv's main function is for. int main (int argc, char ** argv)

argc is the number of parameters received. In the example above, argc would be 1.

argv contains the arguments themselves. In the above example, argv [0] must access the inputFile.

    
19.02.2016 / 17:42
0

Flex reads data from the yyin variable, which is a file or a file type.

By default, this variable is associated with the STDIN entry.

You can associate this variable with another file or file (eg in a parameter) with the command yyin = fopen("nome_do_arquivo.zzz", "r"); .

Here's an example, taken from the Flex manual:

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

   if ( argc > 0 ) 
       yyin = fopen( argv[0], "r" );
   else
       yyin = stdin;

   yylex();
}

If the function is in a DLL , you can send the file name as a function parameter. Example:

__attribute__ ((dllexport)) int funcao_da_dll(const char *nome_arquivo)
{
    yyin = fopen(nome_arquivo, "r" );
    yylex();
}

For more references (in the item THE GENERATED SCANNER ):

FLEX

    
19.02.2016 / 18:04