469,636 Members | 1,970 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

porting bison flex code from linux to visual studio

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

Dec 13 '06 #1
13 4006
[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.

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/ho...t.html#faq-5.8

Cheers! --M

Dec 13 '06 #2
jc

mlimber wrote:
[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.

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/ho...t.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 <ptrIDENTIFIER
%type <ptridentifier

%%

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

Dec 13 '06 #3
jc wrote:
mlimber wrote:
[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.
>
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/ho...t.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 <ptrIDENTIFIER
%type <ptridentifier

%%

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-lit....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

Dec 14 '06 #4
jc wrote:
mlimber wrote:
>>[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
Dec 14 '06 #5
jc

Stuart Redmann wrote:
jc wrote:
mlimber wrote:
>[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

Dec 14 '06 #6
jc wrote:
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?

Stuart
Dec 14 '06 #7
jc

Stuart Redmann wrote:
jc wrote:
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?

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

Dec 14 '06 #8
jc wrote:
Stuart Redmann wrote:
>>jc wrote:
>>>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
Dec 14 '06 #9
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!

Dec 14 '06 #10
jc

TvN wrote:
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

Dec 14 '06 #11
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!

Dec 15 '06 #12
jc

TvN wrote:
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!
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

Dec 15 '06 #13
jc

TvN wrote:
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!
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

Dec 15 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard Giuly | last post: by
reply views Thread by =?Utf-8?B?S2luZXRpYyBKdW1wIEFwcGxpZmUgZm9yIC5ORVQg | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.