• C

Lex and Yacc

How do I get Lex and Yacc to read files for input?
Who is Participating?
If you are using one input file, you can use redirection:
eg. prog < input_file.

If you want to change your program, you can modify the function 'main'. Inside 'main' and fopen is done.
A normal way of working is parse the option of the program (eg add a -f option) and then pass the file name to the fopen call.
Lex is a parser - you feed it a description of a grammar it is to read (such as C or C++) and it can parse that grammar.

Yacc (or Yet Another Compiler Compiler) uses Lex to parse a grammar which is fed back as a stream of tokens - yacc's purpose is to allow a 'compiler' to produce executables...

So you have to describe the grammar to lex, then use yacc (or yuour own program) to make use of the parsed input.

Start by having a really good read of the man pages for lex and yacc.

The GNU/Linux versions of these are Flex and Bison.


laeuchliAuthor Commented:
I know how to program lex/yacc, I want to know how to feed it text files. Up to now I have been typeing in the info by hand.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

laeuchliAuthor Commented:
I don't quite understand please explain.

When you are programming lex/yacc and you compile it, in some source there must be a main function. Normally inside this main function the input and output are opened.
If you can't find it , try doing a 'grep' in the source files.

So if you want to change the source files, first look for this function, then change it. When you are about to change it, a 'clean' solution is use argument parsing (eg. getopt) to parse (argc, argv).

need more info? explain what you don't understand, and we'll try to answer.
laeuchliAuthor Commented:
I found the main but the part where the files are opened is not there.What do I do?
can you do a grep for 'fopen' in the source files?


See which functions are called in the main functions and check the first few of them for an fopen call.

If no fopen can be found, check for the usage for a stdin filedescriptor.
laeuchliAuthor Commented:
ohh in the source files. I will look.Thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.