The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

parse.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 1993, 2000 Christopher Seiwald.
00003  *
00004  * This file is part of Jam - see jam.c for Copyright information.
00005  */
00006 
00007 /*  This file is ALSO:
00008  *  (C) Copyright David Abrahams 2001. Permission to copy, use,
00009  *  modify, sell and distribute this software is granted provided this
00010  *  copyright notice appears in all copies. This software is provided
00011  *  "as is" without express or implied warranty, and with no claim as
00012  *  to its suitability for any purpose.
00013  */
00014 
00015 # include "jam.h"
00016 # include "lists.h"
00017 # include "parse.h"
00018 # include "scan.h"
00019 # include "newstr.h"
00020 # include "modules.h"
00021 # include "frames.h"
00022 
00023 /*
00024  * parse.c - make and destroy parse trees as driven by the parser
00025  *
00026  * 09/07/00 (seiwald) - ref count on PARSE to avoid freeing when used,
00027  *              as per Matt Armstrong.
00028  * 09/11/00 (seiwald) - structure reworked to reflect that (*func)()
00029  *              returns a LIST *.
00030  */
00031 
00032 static PARSE *yypsave;
00033 
00034 void
00035 parse_file( char *f, FRAME* frame )
00036 {
00037         /* Suspend scan of current file */
00038         /* and push this new file in the stream */
00039 
00040         yyfparse(f);
00041 
00042         /* Now parse each block of rules and execute it. */
00043         /* Execute it outside of the parser so that recursive */
00044         /* calls to yyrun() work (no recursive yyparse's). */
00045 
00046         for(;;)
00047         {
00048             PARSE *p;
00049 
00050             /* Filled by yyparse() calling parse_save() */
00051 
00052             yypsave = 0;
00053 
00054             /* If parse error or empty parse, outta here */
00055 
00056             if( yyparse() || !( p = yypsave ) )
00057                 break;
00058 
00059             /* Run the parse tree. */
00060 
00061             parse_evaluate( p, frame );
00062             parse_free( p );
00063         }
00064 }
00065 
00066 void
00067 parse_save( PARSE *p )
00068 {
00069         yypsave = p;
00070 }
00071 
00072 PARSE *
00073 parse_make( 
00074         LIST    *(*func)( PARSE *p, FRAME *args ),
00075         PARSE   *left,
00076         PARSE   *right,
00077         PARSE   *third,
00078         char    *string,
00079         char    *string1,
00080         int     num )
00081 {
00082         PARSE   *p = (PARSE *)malloc( sizeof( PARSE ) );
00083 
00084         p->func = func;
00085         p->left = left;
00086         p->right = right;
00087         p->third = third;
00088         p->string = string;
00089         p->string1 = string1;
00090         p->num = num;
00091         p->refs = 1;
00092         p->rulename = 0;
00093         
00094         if ( left )
00095         {
00096             p->file = left->file;
00097             p->line = left->line;
00098         }
00099         else
00100         {
00101             yyinput_stream( &p->file, &p->line );
00102         }
00103 
00104         return p;
00105 }
00106 
00107 void
00108 parse_refer( PARSE *p )
00109 {
00110         ++p->refs;
00111 }
00112 
00113 void
00114 parse_free( PARSE *p )
00115 {
00116         if( --p->refs )
00117             return;
00118 
00119         if( p->string )
00120             freestr( p->string );
00121         if( p->string1 )
00122             freestr( p->string1 );
00123         if( p->left )
00124             parse_free( p->left );
00125         if( p->right )
00126             parse_free( p->right );
00127         if( p->third )
00128             parse_free( p->third );
00129         if ( p->rulename )
00130             freestr( p->rulename );
00131         
00132         free( (char *)p );
00133 }
00134 
00135 LIST* parse_evaluate( PARSE *p, FRAME* frame )
00136 {
00137     frame->procedure = p;
00138     return (*p->func)(p, frame);
00139 }

Generated on Mon Nov 8 17:07:46 2004 for MPT by  doxygen 1.3.9.1