LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-08-2012, 01:49 PM   #1
carlnet
LQ Newbie
 
Registered: Apr 2012
Location: In front of my computer
Posts: 1

Rep: Reputation: Disabled
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?
 
Old 04-08-2012, 02:25 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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>

int main( int argc, char *argv[] )
{ return 0; }
To your question...

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__
#define __COMMON_H__

typedef struct {
  int myInteger;
  double myDouble;
} myStructure;

#endif
file1.c
Code:
#include "common.h"

myStructure recordedData;

/* Code to populate recordedData */
file2.c
Code:
#include "common.h"

extern myStructure recordedData;

/* Code that accesses the information in recordedData */
The lex and yacc specifications include sections where you can insert C code that will be passed verbatim to the final generated code. You would place the #include and the recordedData declaration in that section.

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?

Last edited by Dark_Helmet; 04-08-2012 at 02:44 PM.
 
Old 04-11-2012, 03:31 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
man yacc has a simple example about the usage. what is the problem with that?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing calculator program in lex and yacc but getting syntax error Avanti Linux - Newbie 1 10-27-2011 10:27 AM
lex & yacc installation sharathg786 Ubuntu 3 10-12-2005 01:21 AM
how to work on lex & yacc? sharathg786 Ubuntu 2 09-13-2005 04:52 AM
Lex & YACC coolfrog Programming 3 09-25-2004 07:00 AM
Lex & yacc programming in windows. hemanexp Linux - Software 1 01-09-2004 05:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration