LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2006, 10:49 PM   #1
Millenniumman
Member
 
Registered: Apr 2006
Posts: 81

Rep: Reputation: 15
Using yacc parser in C files


I have made a yacc/lex parser. It becomes "y.tab.c" and "y.tab.h" files and then compiles to an executable. How do I best access the parser from within a C++ file in the same project? I have tried including "y.tab.h", but none of the functions from "y.tab.c" can be found. Is there anywhere with a good guide for this? I understand how to make yacc and lex grammar files, but not how to access them from C++ progrmas.
 
Old 09-28-2006, 03:40 PM   #2
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Have you wrapped the header with an extern "C"...?

My (admittedly limited) understanding of C++ is that it mangles all function names, even those that are not overloaded. So you need to explicitly tell it that the code uses C linkage (non-mangled). I.e. put a

Code:
#ifdef __cplusplus
extern "C" {
#endif
at the top of the header, and a corresponding

Code:
#ifdef __cplusplus
}
#endif
at the bottom.

Of course, you'll want to put this in the .y file really, but I'm not familiar with yacc syntax, can you specify code that goes in the header? If not, just write a script that puts it in after running yacc.

Last edited by soggycornflake; 09-28-2006 at 03:43 PM.
 
Old 09-28-2006, 04:38 PM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
If memory serves, you can leave the yacc part alone, and include from C++ like this,
Code:
extern "C" {
#include "y.tab.h"
}
This reduces preprocessor macros and, IMO, is a bit cleaner.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
LEX and YACC with C gr33ndata Programming 4 11-18-2007 05:12 PM
Where to get Yacc? kaydknight Linux - Software 2 12-08-2005 02:59 AM
generic parser to read/write linux config files amitchandel Linux - Software 1 06-14-2004 04:00 PM
yacc, Bison Config Linux - General 6 02-21-2002 02:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

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