porting bison flex code from linux to visual studio

J

jc

I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

please help me

jc
 
M

mlimber

[cross-posting deleted]
I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

please help me

First, your code should be more robust and check for null pointers
before dereferencing them even if it usually doesn't get a null
pointer.

Second, please post a minimal but complete sample (i.e., one we can cut
and pasted *unchanged* into our editors) to see the same error. See the
FAQ on posting code that doesn't work:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Cheers! --M
 
J

jc

mlimber said:
[cross-posting deleted]
I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

please help me

First, your code should be more robust and check for null pointers
before dereferencing them even if it usually doesn't get a null
pointer.

Second, please post a minimal but complete sample (i.e., one we can cut
and pasted *unchanged* into our editors) to see the same error. See the
FAQ on posting code that doesn't work:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Cheers! --M
thanks, now i have simple code that still does not work
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;
%}

%%
{L}({L}|{D})* {
yylval.ptr = strdup(yytext);
return(IDENTIFIER);
}
.. {
}


%%

int yywrap(void){
return 1;
}

bison file

%{
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int yyerror(char *s);
extern "C" {
int yylex(void);
}
%}
%name sample
%union {
char *ptr;
}

%token <ptr> IDENTIFIER
%type <ptr> identifier

%%

identifier
:IDENTIFIER
{
printf("identifier is %s\n", $1);
}

%%
extern "C" FILE *yyin;
extern "C" char *yytext;
extern "C" int yy_flex_debug;
int main(int argc, char *argv[]){
char *fname = NULL;
yy_flex_debug = 0;
if (argc < 2){
printf("no input filename \n");
printf("enter the file name :");
fname = new char[20];
scanf("%s", fname);
yyin = fopen(fname,"r");
}
else{
fname = new char[strlen(argv[1]) + 1];
strcpy(fname, argv[1]);
yyin = fopen(fname, "r");
}
if(yyin){
while(!feof(yyin)){
yyparse();
}
}
getchar();
getchar();
return 0;
}
int yyerror(char *s){
printf("error %s\n", s);
yyparse();
return 0;
}

the simple output can be sample.txt
with content

hello

this program compiles in linux and the output will be
identifier is hello

but in windows it compiles and the output is

identifier is <null>

thanks again
 
M

mlimber

jc said:
mlimber said:
[cross-posting deleted]
I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

please help me

First, your code should be more robust and check for null pointers
before dereferencing them even if it usually doesn't get a null
pointer.

Second, please post a minimal but complete sample (i.e., one we can cut
and pasted *unchanged* into our editors) to see the same error. See the
FAQ on posting code that doesn't work:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Cheers! --M
thanks, now i have simple code that still does not work
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;
%}

%%
{L}({L}|{D})* {
yylval.ptr = strdup(yytext);
return(IDENTIFIER);
}
. {
}


%%

int yywrap(void){
return 1;
}

bison file

%{
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int yyerror(char *s);
extern "C" {
int yylex(void);
}
%}
%name sample
%union {
char *ptr;
}

%token <ptr> IDENTIFIER
%type <ptr> identifier

%%

identifier
:IDENTIFIER
{
printf("identifier is %s\n", $1);
}

%%
extern "C" FILE *yyin;
extern "C" char *yytext;
extern "C" int yy_flex_debug;
int main(int argc, char *argv[]){
char *fname = NULL;
yy_flex_debug = 0;
if (argc < 2){
printf("no input filename \n");
printf("enter the file name :");
fname = new char[20];
scanf("%s", fname);
yyin = fopen(fname,"r");
}
else{
fname = new char[strlen(argv[1]) + 1];
strcpy(fname, argv[1]);
yyin = fopen(fname, "r");
}
if(yyin){
while(!feof(yyin)){
yyparse();
}
}
getchar();
getchar();
return 0;
}
int yyerror(char *s){
printf("error %s\n", s);
yyparse();
return 0;
}

the simple output can be sample.txt
with content

hello

this program compiles in linux and the output will be
identifier is hello

but in windows it compiles and the output is

identifier is <null>

thanks again

Well, I don't know what the problem is, and I'm afraid you're likely
beyond the bounds of this group
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9). If
you can boil it down to a standard C++ language question, perhaps we
can be more help. Otherwise, I think you'll have to rely on some other
newsgroup that deals with platform dependencies or lex/yacc.

Cheers! --M
 
S

Stuart Redmann

jc said:
mlimber said:
[cross-posting deleted]
I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

[snipped mlimbers reply where he asks for a compilable example]
thanks, now i have simple code that still does not work
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;

Just a thought: Shouldn't yylval be defined by the header that is
generated by bison (called yytab.h or something like it)? In this case
it may be an error to define yylval a second time inside this
compilation unit. Googling yields some examples where the yylval
variable is declared as extern in the lex file:
extern YYSTYPE yylval;

[snipped rest of code]

Regards,
Stuart
 
J

jc

Stuart said:
jc said:
mlimber said:
[cross-posting deleted]

jc wrote:

I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing CAN communication
software that i wrote earlier in windows environment(as we get drivers
for these CAN cards only for the windows).

my problem started when i started to move the code from linux to the
visual studio. after many compilation and link error now the code
compiles successfully. i have to mention that whatever changes i make
in the code first i make it in the linux, compile it, run it to check
whether the code works. then i commit those changes in the visual
studio environment.

now my code successfully compiles and links in both linux and windows
environment(except the include unistd.h problem).

when i run the code it, the program crashes with the error "Unhandled
exception, error reading 0xc00005. access violation".
when i debug the code it the value retured from the lex, which
yylval.ptr (which is char *) supposed to have a value is null. and when
i tried to process that pointer i get the above error.

my thoughts about this problem is in the lex file i declare
YYSTYPE yylval;

and it is extern in the bison grammar.
in the linux environment even if i don't declare yylval, i never heard
any complaints. but visual studio complaints about yylval till i
declare it as follows.

[snipped mlimbers reply where he asks for a compilable example]
thanks, now i have simple code that still does not work
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;

Just a thought: Shouldn't yylval be defined by the header that is
generated by bison (called yytab.h or something like it)? In this case
it may be an error to define yylval a second time inside this
compilation unit. Googling yields some examples where the yylval
variable is declared as extern in the lex file:
extern YYSTYPE yylval;

[snipped rest of code]

Regards,
Stuart

yes you are right. in linux environment i don't need

YYSTYPE yylval;
declaration at all. even if i declare it works normally.
but in the visual studio env, if i declare extern it complains, if i
don't declare it complains, it stops only if i declare it this way. i
know this is the root cause of the problem, but how do i solve it.

thanks

jc
 
S

Stuart Redmann

jc said:
Stuart said:
Just a thought: Shouldn't yylval be defined by the header that is
generated by bison (called yytab.h or something like it)? In this case
it may be an error to define yylval a second time inside this
compilation unit. Googling yields some examples where the yylval
variable is declared as extern in the lex file:
extern YYSTYPE yylval;

[snipped rest of code]
yes you are right. in linux environment i don't need

YYSTYPE yylval;
declaration at all. even if i declare it works normally.
but in the visual studio env, if i declare extern it complains,

What is the error message exactly? Is this a compiler or a linker error?

Stuart
 
J

jc

Stuart said:
jc said:
Stuart said:
Just a thought: Shouldn't yylval be defined by the header that is
generated by bison (called yytab.h or something like it)? In this case
it may be an error to define yylval a second time inside this
compilation unit. Googling yields some examples where the yylval
variable is declared as extern in the lex file:
extern YYSTYPE yylval;

[snipped rest of code]
yes you are right. in linux environment i don't need

YYSTYPE yylval;
declaration at all. even if i declare it works normally.
but in the visual studio env, if i declare extern it complains,

What is the error message exactly? Is this a compiler or a linker error?

Stuart

it compiles and links fine in both linux and windows environment. but
the output is different.
the output for the above code should be

identifier is hello

but when i compile it in visual studio and run it, i get

identifier is <null>
that means the yylval.ptr is null instead of "hello"
somehow this value is not transfered between the lexer to bison code.

jc
 
S

Stuart Redmann

jc said:
Stuart said:
jc said:
Stuart Redmann wrote:
Just a thought: Shouldn't yylval be defined by the header that is
generated by bison (called yytab.h or something like it)? In this case
it may be an error to define yylval a second time inside this
compilation unit. Googling yields some examples where the yylval
variable is declared as extern in the lex file:
extern YYSTYPE yylval;

[snipped rest of code]
yes you are right. in linux environment i don't need

YYSTYPE yylval;
declaration at all. even if i declare it works normally.
but in the visual studio env, if i declare extern it complains,

What is the error message exactly? Is this a compiler or a linker error?
it compiles and links fine in both linux and windows environment. but
the output is different.
the output for the above code should be

identifier is hello

but when i compile it in visual studio and run it, i get

identifier is <null>
that means the yylval.ptr is null instead of "hello"
somehow this value is not transfered between the lexer to bison code.

We still need some clarification. Does your source compile fine with the
extern definition of yylval or with the non-extern definition?
Concluding from the behaviour you describe I'd guess that the non-extern
definition is used, so that bison and lex are working with two different
(locally defined) variables that happen to have the same names in their
respective compilation units. I should have emphasized that the
declaration of yylval _must not_ miss the storage specifier external!

Regards,
Stuart
 
T

TvN

Hi,
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;

Have you tried to declare it as extern? Like:

extern YYSTYPE yylval;

because it is declared inside of bison generated files.

I see that yywrap does nothing, what about next option in lex file:
%option noyywrap

And one more, maybe you need play with next option in lex file:
%option bison-bridge

Regards,
Volodymyr!
 
J

jc

TvN said:
Hi,
lex file

D [0-9]
L [a-zA-Z_\.]
%{
#include <stdio.h>
#include "gram.h"
int yywrap();
YYSTYPE yylval;

Have you tried to declare it as extern? Like:

extern YYSTYPE yylval;

because it is declared inside of bison generated files.

I see that yywrap does nothing, what about next option in lex file:
%option noyywrap

And one more, maybe you need play with next option in lex file:
%option bison-bridge

Regards,
Volodymyr!

yes i tried to declare it as extern. but i get the linker error
lex.yy.obj : undefined external symbol _yylval

i tried
%option bison-bridge
as per your suggestion, now surprisingly i get the same result in both
the linux and the visual studio. i.e., the value doesn't copy over from
lex to the bison code. it is still null.
i haven't had time to check that noyywrap and i'll try that later, and
post the results

thanks

jc
 
T

TvN

Hi,
yes i tried to declare it as extern. but i get the linker error
lex.yy.obj : undefined external symbol _yylval
You could declare it in your code ;) I'm not sure it is good idea, but
you could try;)
i tried
%option bison-bridge
as per your suggestion, now surprisingly i get the same result in both
the linux and the visual studio. i.e., the value doesn't copy over from
lex to the bison code. it is still null.
Interesting...
Try to play with %parse-param and %lex-param in your bison file.

Regards,
Volodymyr!
 
J

jc

TvN said:
Hi,
You could declare it in your code ;) I'm not sure it is good idea, but
you could try;)

Interesting...
Try to play with %parse-param and %lex-param in your bison file.

Regards,
Volodymyr!

Volodymyrl,

you are a genius. but i just didn't understand bison-bridge that well
when you first pointed it to me that's why it was not working for me
yesterday. now i get it working in both linux and visual studio
environment. i guess now i have incorporate this new knowledge in the
original project.

thanks again
jc
 
J

jc

TvN said:
Hi,
You could declare it in your code ;) I'm not sure it is good idea, but
you could try;)

Interesting...
Try to play with %parse-param and %lex-param in your bison file.

Regards,
Volodymyr!

Volodymyrl,

you are a genius. but i just didn't understand bison-bridge that well.
now i get it working in both linux and visual studio environment. i
guess now i have incorporate this new knowledge in the original
project.

thanks again
jc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top