delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/17/08:52:17

From: eklaus AT erols DOT com (Programmed to death)
Newsgroups: comp.os.msdos.djgpp
Subject: HELP: Bison, Flex & C++ class support...
Date: Thu, 17 Apr 1997 06:27:02 GMT
Organization: Erol's Internet Services
Lines: 103
Message-ID: <3355bec7.44451742@news.erols.com>
NNTP-Posting-Host: spg-as4s30.erols.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I currently have simple bison and flex files working( listed below )

I made them just to test one thing.

I want to be able use c++ classes in both the lexer and the parser,
and possibly pass objects of a particular class back and forth between
the two.

When i overide the .c extensions for lexyy.c and y_tab.c and
compile everything as c++ code,  errors pop up that were not
present when compiling the files as c-code.

I used the following batch file:

	bison -y -d -t -v cplus.y
	flex -I cplus.l
	gxx -o cplus.exe -x c++ y_tab.c lexyy.c -lfl -lgpp -lm -lpc 

and got the following errors:

	bison.simple: In function `int yyparse()':
	bison.simple:377: warning: implicit declaration of function
`int yylex(...)'
	cplus.lex: In function `int yylex()':
	cplus.lex:21: warning: implicit declaration of function `int
yyerror(...)'
	d:/langs/djgpp/tmp\ccdaaaaa(.text+0x4b5):y_tab.cc: undefined
reference to `yylex'
	d:/langs/djgpp/tmp\cceaaaaa(.text+0x247):lexyy.cc: undefined
reference to `yyerror'

The LEXER( cplus.l ) is listed below:

%{
#include	"y_tab.h"
%}

eol	\n
delim	[ \t\n]
ws	{delim}+
letter	[A-Za-z]
letters	{letter}+
digit	[0-9]
digits	{digit}+

%%

{eol}			{ return( EOL );}

({letters}|{digits})*	{ sprintf( yylval.str, "%s", yytext ); return(
STRING ); }

.			{ yyerror( yytext );}

%%

The PARSER( cplus.y ) is listed below:

%{
#include	<stdio.h>
#include	<math.h>
#include	<string.h>
//#include	<iostream.h>

int yyerror( char str[50] );
int main();
%}

%union
{
	char	str[50];
}

%token	STRING
%token	EOL

%type	<str> STRING string line

%%
input:					{ printf("\n\n  ** START **
\n"); }
	|	input line		{ printf("\n\n  ** FINISHED:
%s", $2 ); YYABORT; }
	;

line:		EOL			{ strcpy( $$, "\n");	}
	|	string EOL		{ strcat( $$, "\n");	}
	;

string:		STRING			{ strcpy( $$, $1);	}
	|	STRING string		{ strcat( $$, $2 );	}
	;
%%

int yyerror( char *str )
{
	printf("\nERROR: %s\n", str );
}

int main()
{
	yyparse();
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019