help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can't compile bison/flex output


From: Richard Garand
Subject: Can't compile bison/flex output
Date: Fri, 16 Feb 2001 18:45:05 -0600

I'm writing a parser in flex and bison, and I can't compile the output
files. Flex and Bison run with no errors (i do have a warning about useless
non-terminals and rules), but when I try to compile in Visual C++ or gcc,
both c files have errors. I'm using flex "Cygnus version 2.5-cygwin-990830"
and bison version 1.28 in the latest version of Cygwin. Why won't the C
files compile (with command gcc file)? I've appended the source for the flex
and bison files to this message.

Richard Garand
address@hidden
ICQ: 12190132
"The original, complete, and unadulterated Descent! Play the game that
turned 3D action on its head and made it puke!" - CGW

/////////////////////// bison file ////////////////////////////
%{
/*C declarations*/
extern YY_MALLOC_DECL int yylex();

class CLinkedListItem
{
public:
        void* data;
        CLinkedListItem* prev;
        CLinkedListItem* next;
}

class CInterface
{
public:
        CInterface( CPackage* pk ) { package = pk; }
        CInterface( CPackage* pk, char* iname ) : name(iname) { CInterface(pk); 
}

        char*           name;
        CPackage*       package;
}

class CPackage
{
public:
        // constructors
        CPackage( ) { interfaces = 0; }
        CPackage( char* pkname ) : name(pkname) { CPackage(); }
        CPackage( char* pkname, char* doc ) : documentation(doc) {
CPackage(pkname); }

        char*                           name;                   // name of 
package
        char*                           documentation;  // documentation
        //int                                   x, y;                   // 
position of object
        CLinkedListItem*        interfaces;             // interfaces
        CLinkedListItem*        links;                  // links in package 
graph
}

CLinkedListItem* packages = 0;
int nPackages = 0;
CPackage* curPackage = 0;

void newpackage( char* name, char* doc )
{
        CPackage* pk;
        CLinkedListItem* item;

        // test package docs ("[ignore]" means it's not added to the graph)
        if ( strcmp(doc, "[ignore]") )
                return;

        // create package in list
        pk = new CPackage( name, doc );
        if ( !packages )
        {
                packages = new CLinkedListItem;
                packages->data = (void*)pk;
                packages->next = 0;
                packages->prev = 0;
        }
        else
        {
                item = packages;
                while( item->next )
                        item = item->next;
                item->next = new CLinkedListItem;
                item->next->data = (void*)pk;
                item->next->prev = item;
                item->next->next = 0;
        }
        nPackages++;

        // set current package
        curPackage = pk;
}

void addintf( char* name )
{
        // add to current package
        if ( !curPackage )
                return;

        CInterface* intf = new CInterface( curPackage, name );
        CLinkedListItem* item = curPackage->interfaces;
        if ( !item )
        {
                curPackage->interfaces = new CLinkedListItem;
                curPackage->interfaces = (void*)pk;
                curPackage->interfaces = 0;
                curPackage->interfaces = 0;
        }
        else
        {
                while( item->next )
                        item = item->next;
                item->next = new CLinkedListItem;
                item->next->data = (void*)pk;
                item->next->prev = item;
                item->next->next = 0;
        }
}

void outputDOT( char* filename )
{
        // open file
        FILE file = fopen( filename, "w" );

        // open graph
        fprintf( file,  "digraph \"g\" {\n" );
        fprintf( file, "graph [\n" );
        fprintf( file, "rankdir = \"LR\"\n" );
        fprintf( file, "]\n" );
        fprintf( file, "node [\n" );
        fprintf( file, "fontsize = \"12\"\n" );
        fprintf( file, "]\n" );

        // write packages
        CLinkedListItem* item = packages;
        CLinkedListItem* intfitem;
        CInterface* intf;
        CPackage* pk;
        int x;
        while ( item )
        {
                pk = (CPackage*)item->data;
                fprintf( file, "\"%s\" [\n", pk->name );
                fprintf( file, "label = \"<f0> %s", pk->name );
                intfitem = pk->interfaces;
                x = 0;
                while ( intfitem )
                {
                        intf = (CInteface*)intfitem->data;
                        fprintf( file, " | <f%d> %s", x++, intf->name );
                        intfitem = intfitem->next;
                }
                fprintf( file, "\"\nshape = \"record\"\n" );
                fprintf( file, "]\n" );
                //fprintf( file, "" );
                item = item->next;
        }

        // write links

        // close graph
        fprintf( file, "}" );

        // close file
        fclose( file );
}
%}
/*Bison declarations*/
%union {
int             numeric;
char*   string;
}

%token  <string>        NAME
%token  <string>        BLANKS
%token  <string>        QUID
%token  <string>        DOC_STRING
%token  <string>        CLASS_CAT               /* "Class_Category"             
*/
%token  <string>        OBJECT                  /* "object"                     
        */
%token  <string>        CLASS                   /* "Class"                      
        */
%token  <string>        QUID_STR                /* "quid"                       
        */
%token  <string>        DOCUMENTATION   /* "documentation"              */
%token  <string>        MLDOC_STRING
%type   <string>        pkdoc
%type   <string>        br
%type   <string>        doc_substr
%type   <string>        package_open
%type   <string>        quid
%type   <string>        name

%%
/*Grammar rules*/

name:                           '"' NAME '"' { $<string>$ = $2 }
;

package_open:           OBJECT CLASS_CAT
;

br:                                     BLANKS '\n'
;

quid:                           BLANKS QUID_STR BLANKS '"' QUID '"' br
;

doc_substr:                     '|' MLDOC_STRING br                             
                                        { $$ = $2; }
                                        | '|' DOC_STRING br                     
                                                { $$ = $2; }
                                        | doc_substr doc_substr                 
                                        { $$ = strcat($1,$2); }
;

pkdoc:                          BLANKS DOCUMENTATION BLANKS '"' DOC_STRING '"' 
br       { $$ = $5; }
                                        | BLANKS DOCUMENTATION br doc_substr
                                        | /* empty */                           
                                                { $$ = "no docs"; }
;

package_info:           package_open name br quid pkdoc         { 
newpackage($2,$5); }
;

interface:                      OBJECT CLASS name                               
        { addintf($3); }
;

link:                           /* have stuff for the links to another package 
*/
;

%%
/*Additional C code*/

#include <string.h>
main ( int argc, char** argv )
{
        ++argv;
        --argc;
        if ( argc > 0 )
        {
                yyin = fopen( argv[0], "r" );
                yyparse();
                char* outfile = argv[0];
                // !! set this to the length of the extension of the input file
                char* ext = outfile + strlen(outfile) - 3;
                *ext = "dot\0";
                outputDOT( outfile );
        }
        else
        {
                printf( "MDL to DOT syntax: mdl-dot filename\n" );
                printf( "The first parameter, filename, is the name of the 
input file." );
                printf( "For more help, contact address@hidden" );
        }
}

////////////////////////////////// flex file
/////////////////////////////////
/* definitions */
%{
#include "mdl-dot.bison.tab.h"
%}
QUID_CHAR               [[:digit:]]|[[:upper:]]
QUID                    {QUID_CHAR}{12}
DOC_CHARS               
[[:alnum:][:blank:],<.>/?':;\\[\]}{|+=-_)(*&address@hidden
NAME_CHARS              [[:alnum:][:blank:]]|\\[^[:cntrl:]\n]
MLDOC_CHARS             [^[:cntrl:]]
%x object_m
%x pkname_m
%x pk_m
%x intf_m
%x intfname_m
%x quid_m
%x quidval_m

%x doc_m
%x pldoc_m
%x mldoc_m
%%
        /* basic object rule */
"Object "                                               BEGIN(object_m); return 
OBJECT;

        /* package information */
<object_m>"Class_Category "             BEGIN(pk_m); return CLASS_CAT;
<pk_m>"\""                                              BEGIN(pkname_m); return 
'"';
<pkname_m>{NAME_CHARS}+                 yylval.string = yytext; return NAME; // 
need to
escape the \ed chars.
<pkname_m>"\""                                  BEGIN(0); return '"';

        /* interface information */
<object_m>"Class \""                    BEGIN(intf_m); return CLASS;
<intf_m>"\""                                    BEGIN(intfname_m); return '"';
<intfname_m>{NAME_CHARS}+               yylval.string = yytext; return NAME;
<intfname_m>"\""                                BEGIN(0); return '"';

        /* quid */
"quid"                                                  BEGIN(quid_m); return 
QUID_STR;
<quid_m>"\""                                    BEGIN(quidval_m); return '"';
<quidval_m>{QUID}                               return QUID;
<quidval_m>"\""                                 BEGIN(0); return '"';

        /* documentation */
"documentation"                                 BEGIN(doc_m); return 
DOCUMENTATION;
<doc_m>"\""                                             BEGIN(pldoc_m); return 
'"';
<pldoc_m>{DOC_CHARS}+                   yylval.string = yytext; return 
DOC_STRING;
<pldoc_m>"\""                                   BEGIN(0); return '"';
<doc_m>^"|"                                             BEGIN(mldoc_m); return 
'|';
<mldoc_m>{MLDOC_CHARS}+                 yylval.string = yytext; return 
MLDOC_STRING;
<mldoc_m>^([:blank:]*"\n")              BEGIN(0);

[ \t]+                                                  return BLANKS;
\n                                                              return '\n';

%%
/* other C code */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]