Redirect Flex input to a string

Asked

Viewed 57 times

1

Good,

I have a flex file and a C routine to check the writing of texts.

I’ve been testing at 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 stdin).

How can I add that?

2 answers

0

You can pass the file by argument.

./inputfile test

That’s what the argc and argv of the main function are for. int main(int argc, char **argv)

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

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

  • Yes, my problem is that this later is to integrate into a C# program as a DLL, so going through argument does not suit me. (Or serve and I’m not seeing how?)

  • I’m sorry @Rodrigoguiotti, this might help, but I need to know: how could I later say that inputfile was the new input for lex?

  • I don’t know if I understand your problem so... let’s see. Is the program in C and C# running? If they are, you will need to decide how the communication between the two processes will be. May be by file or local network interface. Depending on the operating system, you may have a better way.

  • The C# program is working perfectly. Then I did a C routine that also works well. Now what I wanted was to add to the program in c# a dll to access the functions in c (but this is not the purpose of this question) E then send the string as parameter to the function in C and then run the lexical check. (I could tell?)

  • In this case, it seems that the program in C# will call your program in C. When C# decides the string, it can make a system call, like: System.Diagnostics.Process.Start(@"C: Windows test.exe inputfile"); No?

  • I would prefer not to do so. There is no way to change the lex file or my c routine to read directly from a string? (and not from a file)

  • For both programs to use the same string, only using shared memory.

  • It is possible to convert the C code to dll if it is managed. Use dllexport to expose the functions you want to access in C#. If the code is simple, it might be better to copy and paste in C# and fix what is not supported.

Show 3 more comments

0

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

By default, this variable is associated with input STDIN.

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

Here is an example 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

  • So this helps me ;) I’ll see how I can change this to be a string and not a file (even if I have to create a file and then destroy it. Thanks

  • @J.Pinto: I removed the comment, because the functions I indicated are not compatible with the Windows environment

  • Yes I saw @Gomiero, because I’m trying to connect everything, because I have to add this to a C#program. As soon as I have managed to do so I put an answer here with what I have done and attribute the solution either to you or to Rodrigo, whichever has helped me the most

  • One more tip that can help (in English): Multiple Input Buffers, and in Soen, there’s an example: how to use yy_scan_string in lex or String input to flex lexer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.