Re: flex and bison in vc++6 or vc++8

George Neuner <gneuner2@comcast.net>
21 Mar 2007 00:05:13 -0400

          From comp.compilers

Related articles
flex and bison in vc++6 or vc++8 eng.sharif@gmail.com (eng.sharif@gmail.com) (2007-03-14)
Re: flex and bison in vc++6 or vc++8 bill@qswtools.com (Bill Cox) (2007-03-16)
Re: flex and bison in vc++6 or vc++8 gneuner2@comcast.net (George Neuner) (2007-03-17)
Re: flex and bison in vc++6 or vc++8 eng.sharif@gmail.com (eng.sharif@gmail.com) (2007-03-20)
Re: flex and bison in vc++6 or vc++8 gneuner2@comcast.net (George Neuner) (2007-03-21)
| List of all articles for this month |

From: George Neuner <gneuner2@comcast.net>
Newsgroups: comp.compilers
Date: 21 Mar 2007 00:05:13 -0400
Organization: Compilers Central
References: 07-03-05207-03-062 07-03-078
Keywords: yacc, Windows
Posted-Date: 21 Mar 2007 00:05:13 EDT

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


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.