Home > Archive > Compilers > March 2007 > flex and bison in vc++6 or vc++8
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
flex and bison in vc++6 or vc++8
|
|
| eng.sharif@gmail.com 2007-03-14, 10:11 pm |
| hi groups
i want help in work in flex and bison with vc++6 or vc++8
there are many problem in linker.exe files
step by step plz
thnx
sharif
| |
| Bill Cox 2007-03-16, 4:46 am |
| > i want help in work in flex and bison with vc++6 or vc++8
>
> there are many problem in linker.exe files
>
> step by step plz
I recently had to do this quickly. I ran flex and yacc on a Solaris
machine and copied the generated C and header files to Windows. Then I
built the final parser with Visual Studio. There were 5 or 6 warnings,
but the generated parser worked on Windows identically to its operation
on Solaris.
I think you could do the same substituting Linux for Solaris.
Bill
| |
| George Neuner 2007-03-17, 7:14 pm |
| On 14 Mar 2007 14:34:20 -0400, "eng.sharif@gmail.com"
<eng.sharif@gmail.com> wrote:
>i want help in work in flex and bison with vc++6 or vc++8
>there are many problem in linker.exe files
What problems? We can't offer specific help unless we know what
you've tried and what error messages you are getting.
>step by step plz
In general you add the ".y" and ".l" files to your project and then
open the property page for each file and define a custom build for it.
The custom build dialog allows you to specify additional dependencies
for each file (for example you usually build the ".y" file before the
".l" file to generate token header files), what output files will be
produced and the command line required to run the foreign tool.
After you've defined the custom builds for your files, you should do
an individual compile on each file (in the appropriate order) and then
add each of the output files to your project.
Once all the files have been compiled one time and the output files
added to the project, VC will handle the dependencies properly.
The exact steps to define a custom build and compile files depend on
what version of VisualC++ or Visual Studio you are running.
George
| |
| eng.sharif@gmail.com 2007-03-21, 5:45 am |
| On Mar 17, 9:42 pm, George Neuner <gneun...@comcast.net> wrote:
> On 14 Mar 2007 14:34:20 -0400, "eng.sha...@gmail.com"
>
> <eng.sha...@gmail.com> wrote:
>
> What problems? We can't offer specific help unless we know what
> you've tried and what error messages you are getting.
>
>
> In general you add the ".y" and ".l" files to your project and then
> open the property page for each file and define a custom build for it.
> The custom build dialog allows you to specify additional dependencies
> for each file (for example you usually build the ".y" file before the
> ".l" file to generate token header files), what output files will be
> produced and the command line required to run the foreign tool.
>
> After you've defined the custom builds for your files, you should do
> an individual compile on each file (in the appropriate order) and then
> add each of the output files to your project.
>
> Once all the files have been compiled one time and the output files
> added to the project, VC will handle the dependencies properly.
>
> The exact steps to define a custom build and compile files depend on
> what version of VisualC++ or Visual Studio you are running.
>
> George
thax for all
my vesion of c is VisualC++ 8 in Visual Studio 2005
my file
example.l //////////////////////////////////////////////////////////////////
/* example.l */
%{
#include "example.parser.h"
%}
%option noyywrap
%%
[ \t]+ { /* ignore whitespace */ }
"(" { return LPAREN; }
")" { return RPAREN; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return STAR; }
\n { return NEWLINE; }
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
.. { printf("Invalid character '%s'", yytext); }
%%
and
example.y ////////////////////////////////////////////////////////////////////////
/* example.y */
%{
#define YYSTYPE int
#define int yylex()
%}
%token PLUS MINUS STAR LPAREN RPAREN NUMBER NEWLINE
%left PLUS MINUS
%left STAR
%%
line : /* empty */
| line expr NEWLINE { printf("%d\n", $2); }
expr : LPAREN expr RPAREN { $$ = $2; }
| expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr STAR expr { $$ = $1 * $3; }
| NUMBER { $$ = $1; }
;
%%
int yyerror (char const *msg) {
printf("Error: %s\n", msg);
}
int main() {
printf("%d\n", yyparse());
return 0;
}
/////////////////////////////////////
my files after compile
Header Files :
example.parser.h
stdafx.h
Source Files:
example.l
example.parser.c
example.y
lex.example.c
stdafx.cpp
but in build solution i have more errors
////////////////////////
------ Build started: Project: example, Configuration: Debug Win32
------
Compiling...
lex.example.c
...\example.l(16) : warning C4013: 'atoi' undefined; assuming extern
returning int
lex.example.c(911) : warning C4267: '=' : conversion from 'size_t' to
'int', possible loss of data
lex.example.c(1258) : warning C4996: 'fileno' was declared deprecated
d:\program files\microsoft visual studio 8\vc\include
\stdio.h(688) : see declaration of 'fileno'
Message: 'The POSIX name for this item is deprecated. Instead,
use the ISO C++ conformant name: _fileno. See online help for
details.'
lex.example.c(1449) : warning C4013: 'exit' undefined; assuming extern
returning int
lex.example.c(1496) : warning C4013: 'malloc' undefined; assuming
extern returning int
lex.example.c(1496) : warning C4312: 'type cast' : conversion from
'int' to 'void *' of greater size
lex.example.c(1514) : warning C4013: 'realloc' undefined; assuming
extern returning int
lex.example.c(1514) : warning C4312: 'type cast' : conversion from
'int' to 'void *' of greater size
lex.example.c(1524) : warning C4013: 'free' undefined; assuming extern
returning int
Linking...
example.parser.obj : error LNK2019: unresolved external symbol _yylex
referenced in function _yyparse
example.parser.obj : error LNK2019: unresolved external symbol _alloca
referenced in function _yyparse
C:\Documents and Settings\sharif.SYNRYU\My Documents\Visual Studio
2005\Projects\example\Debug\example.exe : fatal error LNK1120: 2
unresolved externals
Build log was saved at "file://c:\Documents and Settings\sharif.SYNRYU
\My Documents\Visual Studio 2005\Projects\example\example\Debug
\BuildLog.htm"
example - 3 error(s), 9 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
this is all
what is problem in my work ?
[Looks to me like you're using a Bison parser that expects to find
GCC extensions in the C compiler. -John]
| |
| George Neuner 2007-03-21, 5:45 am |
| On 20 Mar 2007 08:53:45 -0400, "eng.sharif@gmail.com"
<eng.sharif@gmail.com> wrote:
>my vesion of c is VisualC++ 8 in Visual Studio 2005
>
>my file
>example.l //////////////////////////////////////////////////////////////////
>
>/* example.l */
>%{
>#include "example.parser.h"
>%}
>%option noyywrap
>
>%%
>
>[ \t]+ { /* ignore whitespace */ }
>"(" { return LPAREN; }
>")" { return RPAREN; }
>"+" { return PLUS; }
>"-" { return MINUS; }
>"*" { return STAR; }
>\n { return NEWLINE; }
>[0-9]+ { yylval = atoi(yytext); return NUMBER; }
>. { printf("Invalid character '%s'", yytext); }
>
>%%
>
>and
>example.y ////////////////////////////////////////////////////////////////////////
>/* example.y */
>%{
>#define YYSTYPE int
>#define int yylex()
>%}
>%token PLUS MINUS STAR LPAREN RPAREN NUMBER NEWLINE
>%left PLUS MINUS
>%left STAR
>
>%%
>line : /* empty */
> | line expr NEWLINE { printf("%d\n", $2); }
>expr : LPAREN expr RPAREN { $$ = $2; }
> | expr PLUS expr { $$ = $1 + $3; }
> | expr MINUS expr { $$ = $1 - $3; }
> | expr STAR expr { $$ = $1 * $3; }
> | NUMBER { $$ = $1; }
> ;
>
>%%
>
>int yyerror (char const *msg) {
> printf("Error: %s\n", msg);
>}
>
>int main() {
> printf("%d\n", yyparse());
> return 0;
>}
>
>
>
>
>/////////////////////////////////////
>my files after compile
>
>Header Files :
>example.parser.h
>stdafx.h
>
>Source Files:
>example.l
>example.parser.c
>example.y
>lex.example.c
>stdafx.cpp
>
>
>but in build solution i have more errors
>
>////////////////////////
>
>------ Build started: Project: example, Configuration: Debug Win32
>------
>Compiling...
>lex.example.c
>..\example.l(16) : warning C4013: 'atoi' undefined; assuming extern
>returning int
>lex.example.c(911) : warning C4267: '=' : conversion from 'size_t' to
>'int', possible loss of data
>lex.example.c(1258) : warning C4996: 'fileno' was declared deprecated
> d:\program files\microsoft visual studio 8\vc\include
>\stdio.h(688) : see declaration of 'fileno'
> Message: 'The POSIX name for this item is deprecated. Instead,
>use the ISO C++ conformant name: _fileno. See online help for
>details.'
>lex.example.c(1449) : warning C4013: 'exit' undefined; assuming extern
>returning int
>lex.example.c(1496) : warning C4013: 'malloc' undefined; assuming
>extern returning int
>lex.example.c(1496) : warning C4312: 'type cast' : conversion from
>'int' to 'void *' of greater size
>lex.example.c(1514) : warning C4013: 'realloc' undefined; assuming
>extern returning int
>lex.example.c(1514) : warning C4312: 'type cast' : conversion from
>'int' to 'void *' of greater size
>lex.example.c(1524) : warning C4013: 'free' undefined; assuming extern
>returning int
>Linking...
>example.parser.obj : error LNK2019: unresolved external symbol _yylex
>referenced in function _yyparse
>example.parser.obj : error LNK2019: unresolved external symbol _alloca
>referenced in function _yyparse
>C:\Documents and Settings\sharif.SYNRYU\My Documents\Visual Studio
>2005\Projects\example\Debug\example.exe : fatal error LNK1120: 2
>unresolved externals
>Build log was saved at "file://c:\Documents and Settings\sharif.SYNRYU
>\My Documents\Visual Studio 2005\Projects\example\example\Debug
>\BuildLog.htm"
>example - 3 error(s), 9 warning(s)
>========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
>==========
>this is all
>
>what is problem in my work ?
>[Looks to me like you're using a Bison parser that expects to find
>GCC extensions in the C compiler. -John]
There is indeed a GCC'ism, but there are some errors and omissions in
the source that must be fixed first.
>#define int yylex()
This line in the parser is incorrect. You have written a preprocessor
macro that, before compiling, replaces each occurrence of the string
"int" in the source with the string "yylex()". You need instead a
declaration of the external yylex() function.
You are calling C library functions in your code (atoi,printf) without
including the relevant header files. Bison and lex are also using C
library functions (malloc,realloc,free,exit) that do not appear in
your source code but appear in the generated C code. You need to
include header files for any library functions that are used.
[You are not getting an error about printf() because its header file
is already being included through stdafx.h.]
You don't (normally) chase errors in generated files - the fix should
almost always be in the source file that produces it. For example, to
fix a "function undefined" error in the lexer C file, you insert a
declaration for it or include the right header file in the ".l" source
file.
Now, an incompatibility we need to work around. Bison uses a
non-standard function, alloca(), which allocates temporary memory
buffers directly from the stack. VisualC does not support alloca().
To work around this, we need to replace all the calls to alloca() with
calls to the standard allocator function malloc(). Rather than search
and replace in the generated C code [we don't do that, remember?], we
use a preprocessor macro in the parser:
#ifdef _MSC_VER
#define alloca( x ) malloc( x )
#endif
The #ifdef ensures that the macro only works when you compile with
VisualC - so you can use the same Bison source with another compiler
that does support alloca().
I don't know offhand the reason for the "size_t" or "fileno" warnings,
but they shouldn't stop you from compiling and we can return to them
later if you can't figure them out yourself.
All of your problems seem to be with the C language rather than with
Bison or Flex. You need to learn more about C programming and about
the compiler so you can understand its error messages and figure out
for yourself how to fix problems. I suggest that you forget about
parsers for a while and get yourself a good C tutorial.
George
|
|
|
|
|