parseEqn.c 13.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**CFile****************************************************************

  FileNameIn  [parseEqn.c]

  PackageName [ABC: Logic synthesis and verification system.]

  Synopsis    [Boolean formula parser.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - December 18, 2006.]

  Revision    [$Id: parseEqn.c,v 1.0 2006/12/18 00:00:00 alanmi Exp $]

***********************************************************************/


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#include "parseInt.h"
25 26
#include "misc/vec/vec.h"
#include "aig/hop/hop.h"
Alan Mishchenko committed
27

28 29 30
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// the list of operation symbols to be used in expressions
#define PARSE_EQN_SYM_OPEN    '('   // opening paranthesis
#define PARSE_EQN_SYM_CLOSE   ')'   // closing paranthesis
#define PARSE_EQN_SYM_CONST0  '0'   // constant 0
#define PARSE_EQN_SYM_CONST1  '1'   // constant 1
#define PARSE_EQN_SYM_NEG     '!'   // negation before the variable
#define PARSE_EQN_SYM_AND     '*'   // logic AND
#define PARSE_EQN_SYM_OR      '+'   // logic OR

// the list of opcodes (also specifying operation precedence)
#define PARSE_EQN_OPER_NEG    10    // negation
#define PARSE_EQN_OPER_AND     9    // logic AND
#define PARSE_EQN_OPER_OR      7    // logic OR
#define PARSE_EQN_OPER_MARK    1    // OpStack token standing for an opening paranthesis

// these are values of the internal Flag
#define PARSE_EQN_FLAG_START   1    // after the opening parenthesis 
#define PARSE_EQN_FLAG_VAR     2    // after operation is received
#define PARSE_EQN_FLAG_OPER    3    // after operation symbol is received
#define PARSE_EQN_FLAG_ERROR   4    // when error is detected

#define PARSE_EQN_STACKSIZE 1000

static Hop_Obj_t * Parse_ParserPerformTopOp( Hop_Man_t * pMan, Parse_StackFn_t * pStackFn, int Oper );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Derives the AIG corresponding to the equation.]

  Description [Takes the stream to output messages, the formula, the vector
  of variable names and the AIG manager.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Parse_FormulaParserEqn( FILE * pOutput, char * pFormInit, Vec_Ptr_t * vVarNames, Hop_Man_t * pMan )
{
    char * pFormula;
    Parse_StackFn_t * pStackFn;
    Parse_StackOp_t * pStackOp;
    Hop_Obj_t * gFunc;
    char * pTemp, * pName;
    int nParans, fFound, Flag;
    int Oper, Oper1, Oper2;
    int i, v;

    // make sure that the number of opening and closing parantheses is the same
    nParans = 0;
    for ( pTemp = pFormInit; *pTemp; pTemp++ )
        if ( *pTemp == '(' )
            nParans++;
        else if ( *pTemp == ')' )
            nParans--;
    if ( nParans != 0 )
    {
        fprintf( pOutput, "Parse_FormulaParserEqn(): Different number of opening and closing parantheses ().\n" );
        return NULL;
    }

    // copy the formula
Alan Mishchenko committed
97
    pFormula = ABC_ALLOC( char, strlen(pFormInit) + 3 );
Alan Mishchenko committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    sprintf( pFormula, "(%s)", pFormInit );

    // start the stacks
    pStackFn = Parse_StackFnStart( PARSE_EQN_STACKSIZE );
    pStackOp = Parse_StackOpStart( PARSE_EQN_STACKSIZE );

    Flag = PARSE_EQN_FLAG_START;
    for ( pTemp = pFormula; *pTemp; pTemp++ )
    {
        switch ( *pTemp )
        {
        // skip all spaces, tabs, and end-of-lines
        case ' ':
        case '\t':
        case '\r':
        case '\n':
            continue;
        case PARSE_EQN_SYM_CONST0:
            Parse_StackFnPush( pStackFn, Hop_ManConst0(pMan) );  // Cudd_Ref( b0 );
            if ( Flag == PARSE_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Parse_FormulaParserEqn(): No operation symbol before constant 0.\n" );
                Flag = PARSE_EQN_FLAG_ERROR; 
                break;
            }
            Flag = PARSE_EQN_FLAG_VAR; 
            break;
        case PARSE_EQN_SYM_CONST1:
            Parse_StackFnPush( pStackFn, Hop_ManConst1(pMan) );  //  Cudd_Ref( b1 );
            if ( Flag == PARSE_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Parse_FormulaParserEqn(): No operation symbol before constant 1.\n" );
                Flag = PARSE_EQN_FLAG_ERROR; 
                break;
            }
            Flag = PARSE_EQN_FLAG_VAR; 
            break;
        case PARSE_EQN_SYM_NEG:
            if ( Flag == PARSE_EQN_FLAG_VAR )
            {// if NEGBEF follows a variable, AND is assumed
                Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_AND );
                Flag = PARSE_EQN_FLAG_OPER;
            }
            Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_NEG );
            break;
        case PARSE_EQN_SYM_AND:
        case PARSE_EQN_SYM_OR:
            if ( Flag != PARSE_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Parse_FormulaParserEqn(): There is no variable before AND, EXOR, or OR.\n" );
                Flag = PARSE_EQN_FLAG_ERROR; 
                break;
            }
            if ( *pTemp == PARSE_EQN_SYM_AND )
                Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_AND );
            else //if ( *pTemp == PARSE_EQN_SYM_OR )
                Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_OR );
            Flag = PARSE_EQN_FLAG_OPER; 
            break;
        case PARSE_EQN_SYM_OPEN:
            if ( Flag == PARSE_EQN_FLAG_VAR )
            {
//                Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_AND );
                fprintf( pOutput, "Parse_FormulaParserEqn(): An opening paranthesis follows a var without operation sign.\n" ); 
                Flag = PARSE_EQN_FLAG_ERROR; 
                break; 
            }
            Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_MARK );
            // after an opening bracket, it feels like starting over again
            Flag = PARSE_EQN_FLAG_START; 
            break;
        case PARSE_EQN_SYM_CLOSE:
            if ( !Parse_StackOpIsEmpty( pStackOp ) )
            {
                while ( 1 )
                {
                    if ( Parse_StackOpIsEmpty( pStackOp ) )
                    {
                        fprintf( pOutput, "Parse_FormulaParserEqn(): There is no opening paranthesis\n" );
                        Flag = PARSE_EQN_FLAG_ERROR; 
                        break;
                    }
                    Oper = Parse_StackOpPop( pStackOp );
                    if ( Oper == PARSE_EQN_OPER_MARK )
                        break;

                    // perform the given operation
                    if ( Parse_ParserPerformTopOp( pMan, pStackFn, Oper ) == NULL )
                    {
187 188
                        Parse_StackFnFree( pStackFn );
                        Parse_StackOpFree( pStackOp );
Alan Mishchenko committed
189
                        fprintf( pOutput, "Parse_FormulaParserEqn(): Unknown operation\n" );
Alan Mishchenko committed
190
                        ABC_FREE( pFormula );
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                        return NULL;
                    }
                }
            }
            else
            {
                fprintf( pOutput, "Parse_FormulaParserEqn(): There is no opening paranthesis\n" );
                Flag = PARSE_EQN_FLAG_ERROR; 
                break;
            }
            if ( Flag != PARSE_EQN_FLAG_ERROR )
                Flag = PARSE_EQN_FLAG_VAR; 
            break;


        default:
            // scan the next name
            for ( i = 0; pTemp[i] && 
                         pTemp[i] != ' ' && pTemp[i] != '\t' && pTemp[i] != '\r' && pTemp[i] != '\n' &&
                         pTemp[i] != PARSE_EQN_SYM_AND && pTemp[i] != PARSE_EQN_SYM_OR && pTemp[i] != PARSE_EQN_SYM_CLOSE; i++ )
              {
                    if ( pTemp[i] == PARSE_EQN_SYM_NEG || pTemp[i] == PARSE_EQN_SYM_OPEN )
                    {
                        fprintf( pOutput, "Parse_FormulaParserEqn(): The negation sign or an opening paranthesis inside the variable name.\n" );
                        Flag = PARSE_EQN_FLAG_ERROR; 
                        break;
                    }
              }
            // variable name is found
            fFound = 0;
221
            Vec_PtrForEachEntry( char *, vVarNames, pName, v )
Alan Mishchenko committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                if ( strncmp(pTemp, pName, i) == 0 && strlen(pName) == (unsigned)i )
                {
                    pTemp += i-1;
                    fFound = 1;
                    break;
                }
            if ( !fFound )
            { 
                fprintf( pOutput, "Parse_FormulaParserEqn(): The parser cannot find var \"%s\" in the input var list.\n", pTemp ); 
                Flag = PARSE_EQN_FLAG_ERROR; 
                break; 
            }
            if ( Flag == PARSE_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Parse_FormulaParserEqn(): The variable name \"%s\" follows another var without operation sign.\n", pTemp ); 
                Flag = PARSE_EQN_FLAG_ERROR; 
                break; 
            }
            Parse_StackFnPush( pStackFn, Hop_IthVar( pMan, v ) ); // Cudd_Ref( pbVars[v] );
            Flag = PARSE_EQN_FLAG_VAR; 
            break;
        }

        if ( Flag == PARSE_EQN_FLAG_ERROR )
            break;      // error exit
        else if ( Flag == PARSE_EQN_FLAG_START )
            continue;  //  go on parsing
        else if ( Flag == PARSE_EQN_FLAG_VAR )
            while ( 1 )
            {  // check if there are negations in the OpStack     
                if ( Parse_StackOpIsEmpty(pStackOp) )
                    break;
                Oper = Parse_StackOpPop( pStackOp );
                if ( Oper != PARSE_EQN_OPER_NEG )
                {
                    Parse_StackOpPush( pStackOp, Oper );
                    break;
                }
                else
                {
262
                      Parse_StackFnPush( pStackFn, Hop_Not((Hop_Obj_t *)Parse_StackFnPop(pStackFn)) );
Alan Mishchenko committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
                }
            }
        else // if ( Flag == PARSE_EQN_FLAG_OPER )
            while ( 1 )
            {  // execute all the operations in the OpStack
               // with precedence higher or equal than the last one
                Oper1 = Parse_StackOpPop( pStackOp ); // the last operation
                if ( Parse_StackOpIsEmpty(pStackOp) ) 
                {  // if it is the only operation, push it back
                    Parse_StackOpPush( pStackOp, Oper1 );
                    break;
                }
                Oper2 = Parse_StackOpPop( pStackOp ); // the operation before the last one
                if ( Oper2 >= Oper1 )  
                {  // if Oper2 precedence is higher or equal, execute it
                    if ( Parse_ParserPerformTopOp( pMan, pStackFn, Oper2 ) == NULL )
                    {
                        fprintf( pOutput, "Parse_FormulaParserEqn(): Unknown operation\n" );
Alan Mishchenko committed
281
                        ABC_FREE( pFormula );
282 283
                        Parse_StackFnFree( pStackFn );
                        Parse_StackOpFree( pStackOp );
Alan Mishchenko committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                        return NULL;
                    }
                    Parse_StackOpPush( pStackOp,  Oper1 );     // push the last operation back
                }
                else
                {  // if Oper2 precedence is lower, push them back and done
                    Parse_StackOpPush( pStackOp, Oper2 );
                    Parse_StackOpPush( pStackOp, Oper1 );
                    break;
                }
            }
    }

    if ( Flag != PARSE_EQN_FLAG_ERROR )
    {
        if ( !Parse_StackFnIsEmpty(pStackFn) )
        {    
301
            gFunc = (Hop_Obj_t *)Parse_StackFnPop(pStackFn);
Alan Mishchenko committed
302 303 304 305 306 307
            if ( Parse_StackFnIsEmpty(pStackFn) )
                if ( Parse_StackOpIsEmpty(pStackOp) )
                {
                    Parse_StackFnFree(pStackFn);
                    Parse_StackOpFree(pStackOp);
//                    Cudd_Deref( gFunc );
Alan Mishchenko committed
308
                    ABC_FREE( pFormula );
Alan Mishchenko committed
309 310 311 312 313 314 315 316 317 318
                    return gFunc;
                }
                else
                    fprintf( pOutput, "Parse_FormulaParserEqn(): Something is left in the operation stack\n" );
            else
                fprintf( pOutput, "Parse_FormulaParserEqn(): Something is left in the function stack\n" );
        }
        else
            fprintf( pOutput, "Parse_FormulaParserEqn(): The input string is empty\n" );
    }
Alan Mishchenko committed
319
    ABC_FREE( pFormula );
Alan Mishchenko committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    return NULL;
}

/**Function*************************************************************

  Synopsis    [Performs the operation on the top entries in the stack.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Parse_ParserPerformTopOp( Hop_Man_t * pMan, Parse_StackFn_t * pStackFn, int Oper )
{
    Hop_Obj_t * gArg1, * gArg2, * gFunc;
    // perform the given operation
338 339
    gArg2 = (Hop_Obj_t *)Parse_StackFnPop( pStackFn );
    gArg1 = (Hop_Obj_t *)Parse_StackFnPop( pStackFn );
Alan Mishchenko committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    if ( Oper == PARSE_EQN_OPER_AND )
        gFunc = Hop_And( pMan, gArg1, gArg2 );
    else if ( Oper == PARSE_EQN_OPER_OR )
        gFunc = Hop_Or( pMan, gArg1, gArg2 );
    else
        return NULL;
//    Cudd_Ref( gFunc );
//    Cudd_RecursiveDeref( dd, gArg1 );
//    Cudd_RecursiveDeref( dd, gArg2 );
    Parse_StackFnPush( pStackFn,  gFunc );
    return gFunc;
}


////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////
357 358
ABC_NAMESPACE_IMPL_END