How does a c program see the variable declared in a lex & yacc file
Hi guys, I wrote a simple parser in lex and yacc and I have declared my data structure in there (namely, an array). When I am parsing a file, I store the data contained within the file in the array. Then I would like to print them out using a .c file. However, there is no header in lex and yacc for me the c file to see the array declared in the lex and yacc files. So how is it possible for me to use any of the variables declared in lex and yacc?
|
For these kinds of questions, you will almost always get better responses if you include the code you're trying to use.
To include code in one of your messages, use code tags. For instance, typing this in your message: [code] #include <stdio.h> int main( int argc, char *argv[] ) { return 0; } [/code] Will produce: Code:
#include <stdio.h> If you want one C file to access a data structure instantiated in another C file, the simplest method is to: 1. declare the variable type using a typedef in a header file 2. #include the header with the definition in the file that wants to access the data and in the file that will populate the data structure with data 3. create an instance of the variable at a global scope (i.e. not inside a function) in the file that populates the data structure 4. create an instance of the variable by using the same name, but add "extern" before the type in the file that wants to access the data Something like: common.h Code:
#ifndef __COMMON_H__ Code:
#include "common.h" Code:
#include "common.h" Note: You should have only once C file that declares recordedData without the extern keyword. This means that if your data structure is being populated by both your lex specification and your yacc specification, one of them need to use the "extern" keyword. Make sense? |
man yacc has a simple example about the usage. what is the problem with that?
|
All times are GMT -5. The time now is 11:51 AM. |