将bison flex代码从linux移植到visual studio [英] porting bison flex code from linux to visual studio

查看:68
本文介绍了将bison flex代码从linux移植到visual studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个使用bison和flex的解析器来读取ASAP2文件以进行CAN

通信。整个开发都是在unix环境下完成的,现在代码已经准备好集成到我之前在windows环境中编写的现有CAN通信

软件了(我们得到了)司机

这些CAN卡只适用于Windows)。


当我开始将代码从linux移到
$时我的问题就出现了b $ b视觉工作室。经过多次编译和链接错误后,代码

编译成功。我必须提一下,无论我在代码中做出什么变化,首先我在linux中编写它,编译它,运行它来检查

代码是否有效。然后我在视觉

工作室环境中提交这些更改。


现在我的代码成功编译并链接到linux和windows

环境(包括unistd.h问题除外)。


当我运行代码时,程序崩溃并出现错误Unhandled

异常,错误读取0xc00005。访问冲突>

当我调试代码时它从lex中恢复的值,这是
yylval.ptr(这是char *)应该有一个值为null 。当

i尝试处理该指针时我得到了上述错误。


我对这个问题的看法是在lex文件中我声明

YYSTYPE yylval;


,它在野牛语法中是extern。

在linux环境中,即使我没有声明yylval,我从未听过

任何投诉。但是关于yylval的视觉工作室投诉直到我

声明如下。


请帮帮我


jc

解决方案

[交叉发布删除]


jc写道:
< blockquote class =post_quotes>
我编写了一个使用bison和flex的解析器来读取ASAP2文件以进行CAN

通信。整个开发都是在unix环境下完成的,现在代码已经准备好集成到我之前在windows环境中编写的现有CAN通信

软件了(我们得到了)司机

这些CAN卡只适用于Windows)。


当我开始将代码从linux移到
$时我的问题就出现了b $ b视觉工作室。经过多次编译和链接错误后,代码

编译成功。我必须提一下,无论我在代码中做出什么变化,首先我在linux中编写它,编译它,运行它来检查

代码是否有效。然后我在视觉

工作室环境中提交这些更改。


现在我的代码成功编译并链接到linux和windows

环境(包括unistd.h问题除外)。


当我运行代码时,程序崩溃并出现错误Unhandled

异常,错误读取0xc00005。访问冲突>

当我调试代码时它从lex中恢复的值,这是
yylval.ptr(这是char *)应该有一个值为null 。当

i尝试处理该指针时我得到了上述错误。


我对这个问题的看法是在lex文件中我声明

YYSTYPE yylval;


,它在野牛语法中是extern。

在linux环境中,即使我没有声明yylval,我从未听过

任何投诉。但视觉工作室对yylval的抱怨直到我

声明如下。


请帮助我



首先,你的代码应该更健壮并且在解除引用之前检查空指针

,即使它通常没有得到null

指针。


其次,请发布一个最小但完整的样本(即,我们可以剪切一个
并粘贴*不变*到我们的编辑器中)以查看相同的错误。请参阅

关于发布无效代码的常见问题解答:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8


干杯! --M




mlimber写道:


[交叉发布已删除]


jc写道:


我编写了一个使用bison和flex的解析器来读取ASAP2文件CAN

通讯。整个开发都是在unix环境下完成的,现在代码已经准备好集成到我之前在windows环境中编写的现有CAN通信

软件了(我们得到了)司机

这些CAN卡只适用于Windows)。


当我开始将代码从linux移到
$时我的问题就出现了b $ b视觉工作室。经过多次编译和链接错误后,代码

编译成功。我必须提一下,无论我在代码中做出什么变化,首先我在linux中编写它,编译它,运行它来检查

代码是否有效。然后我在视觉

工作室环境中提交这些更改。


现在我的代码成功编译并链接到linux和windows

环境(包括unistd.h问题除外)。


当我运行代码时,程序崩溃并出现错误Unhandled

异常,错误读取0xc00005。访问冲突>

当我调试代码时它从lex中恢复的值,这是
yylval.ptr(这是char *)应该有一个值为null 。当

i尝试处理该指针时我得到了上述错误。


我对这个问题的看法是在lex文件中我声明

YYSTYPE yylval;


,它在野牛语法中是extern。

在linux环境中,即使我没有声明yylval,我从未听过

任何投诉。但视觉工作室对yylval的抱怨直到我

声明如下。


请帮助我



首先,你的代码应该更健壮并且在解除引用之前检查空指针

,即使它通常没有得到null

指针。


其次,请发布一个最小但完整的样本(即,我们可以剪切一个
并粘贴*不变*到我们的编辑器中)以查看相同的错误。请参阅

关于发布无效代码的常见问题解答:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8


干杯! --M



谢谢,现在我有简单的代码仍然无法工作

lex文件


D [0-9]

L [a-zA-Z_\。]

%{

#include< stdio.h>

#include" gram.h"

int yywrap();

YYSTYPE yylval;

%}


%%

{L}({L} | {D})* {

yylval。 ptr = strdup(yytext);

返回(IDENTIFIER);

}

.. {

}

%%


int yywrap(无效){

返回1;

}


野牛文件


%{

#include< stdio.h>

# include< string.h>

#include< malloc.h>

int yyerror(char * s);

extern" C" {

int yylex(无效);

}

%}

%名称样本

%union {

char * ptr;

}


%token< ptridENTIFIER

%type< ptridentifier


%%


标识符

:IDENTIFIER

{

printf("标识符是%s \ n",


1);

}


%%

externC FILE * yyin;

externC 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 \\\
");

printf(" enter文件名:");

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();

返回0;

}

int yyerror(char * s){

printf(" error%s\ n,s);

yyparse();

返回0;

}


简单输出可以是sample.txt

含内容


你好


t他的程序在linux中编译,输出将是

标识符是你好


但是在windows中它编译并且输出是


标识符是< null>


再次感谢


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

解决方案

[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



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


这篇关于将bison flex代码从linux移植到visual studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆