abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parseEqn.c File Reference
#include "parseInt.h"
#include "misc/vec/vec.h"
#include "aig/hop/hop.h"

Go to the source code of this file.

Macros

#define PARSE_EQN_SYM_OPEN   '('
 DECLARATIONS ///. More...
 
#define PARSE_EQN_SYM_CLOSE   ')'
 
#define PARSE_EQN_SYM_CONST0   '0'
 
#define PARSE_EQN_SYM_CONST1   '1'
 
#define PARSE_EQN_SYM_NEG   '!'
 
#define PARSE_EQN_SYM_AND   '*'
 
#define PARSE_EQN_SYM_OR   '+'
 
#define PARSE_EQN_OPER_NEG   10
 
#define PARSE_EQN_OPER_AND   9
 
#define PARSE_EQN_OPER_OR   7
 
#define PARSE_EQN_OPER_MARK   1
 
#define PARSE_EQN_FLAG_START   1
 
#define PARSE_EQN_FLAG_VAR   2
 
#define PARSE_EQN_FLAG_OPER   3
 
#define PARSE_EQN_FLAG_ERROR   4
 
#define PARSE_EQN_STACKSIZE   1000
 

Functions

static Hop_Obj_tParse_ParserPerformTopOp (Hop_Man_t *pMan, Parse_StackFn_t *pStackFn, int Oper)
 
Hop_Obj_tParse_FormulaParserEqn (FILE *pOutput, char *pFormInit, Vec_Ptr_t *vVarNames, Hop_Man_t *pMan)
 FUNCTION DEFINITIONS ///. More...
 

Macro Definition Documentation

#define PARSE_EQN_FLAG_ERROR   4

Definition at line 50 of file parseEqn.c.

#define PARSE_EQN_FLAG_OPER   3

Definition at line 49 of file parseEqn.c.

#define PARSE_EQN_FLAG_START   1

Definition at line 47 of file parseEqn.c.

#define PARSE_EQN_FLAG_VAR   2

Definition at line 48 of file parseEqn.c.

#define PARSE_EQN_OPER_AND   9

Definition at line 42 of file parseEqn.c.

#define PARSE_EQN_OPER_MARK   1

Definition at line 44 of file parseEqn.c.

#define PARSE_EQN_OPER_NEG   10

Definition at line 41 of file parseEqn.c.

#define PARSE_EQN_OPER_OR   7

Definition at line 43 of file parseEqn.c.

#define PARSE_EQN_STACKSIZE   1000

Definition at line 52 of file parseEqn.c.

#define PARSE_EQN_SYM_AND   '*'

Definition at line 37 of file parseEqn.c.

#define PARSE_EQN_SYM_CLOSE   ')'

Definition at line 33 of file parseEqn.c.

#define PARSE_EQN_SYM_CONST0   '0'

Definition at line 34 of file parseEqn.c.

#define PARSE_EQN_SYM_CONST1   '1'

Definition at line 35 of file parseEqn.c.

#define PARSE_EQN_SYM_NEG   '!'

Definition at line 36 of file parseEqn.c.

#define PARSE_EQN_SYM_OPEN   '('

DECLARATIONS ///.

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

]

Definition at line 32 of file parseEqn.c.

#define PARSE_EQN_SYM_OR   '+'

Definition at line 38 of file parseEqn.c.

Function Documentation

Hop_Obj_t* Parse_FormulaParserEqn ( FILE *  pOutput,
char *  pFormInit,
Vec_Ptr_t vVarNames,
Hop_Man_t pMan 
)

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 []

Definition at line 72 of file parseEqn.c.

73 {
74  char * pFormula;
75  Parse_StackFn_t * pStackFn;
76  Parse_StackOp_t * pStackOp;
77  Hop_Obj_t * gFunc;
78  char * pTemp, * pName;
79  int nParans, fFound, Flag;
80  int Oper, Oper1, Oper2;
81  int i, v;
82 
83  // make sure that the number of opening and closing parantheses is the same
84  nParans = 0;
85  for ( pTemp = pFormInit; *pTemp; pTemp++ )
86  if ( *pTemp == '(' )
87  nParans++;
88  else if ( *pTemp == ')' )
89  nParans--;
90  if ( nParans != 0 )
91  {
92  fprintf( pOutput, "Parse_FormulaParserEqn(): Different number of opening and closing parantheses ().\n" );
93  return NULL;
94  }
95 
96  // copy the formula
97  pFormula = ABC_ALLOC( char, strlen(pFormInit) + 3 );
98  sprintf( pFormula, "(%s)", pFormInit );
99 
100  // start the stacks
103 
104  Flag = PARSE_EQN_FLAG_START;
105  for ( pTemp = pFormula; *pTemp; pTemp++ )
106  {
107  switch ( *pTemp )
108  {
109  // skip all spaces, tabs, and end-of-lines
110  case ' ':
111  case '\t':
112  case '\r':
113  case '\n':
114  continue;
116  Parse_StackFnPush( pStackFn, Hop_ManConst0(pMan) ); // Cudd_Ref( b0 );
117  if ( Flag == PARSE_EQN_FLAG_VAR )
118  {
119  fprintf( pOutput, "Parse_FormulaParserEqn(): No operation symbol before constant 0.\n" );
120  Flag = PARSE_EQN_FLAG_ERROR;
121  break;
122  }
123  Flag = PARSE_EQN_FLAG_VAR;
124  break;
126  Parse_StackFnPush( pStackFn, Hop_ManConst1(pMan) ); // Cudd_Ref( b1 );
127  if ( Flag == PARSE_EQN_FLAG_VAR )
128  {
129  fprintf( pOutput, "Parse_FormulaParserEqn(): No operation symbol before constant 1.\n" );
130  Flag = PARSE_EQN_FLAG_ERROR;
131  break;
132  }
133  Flag = PARSE_EQN_FLAG_VAR;
134  break;
135  case PARSE_EQN_SYM_NEG:
136  if ( Flag == PARSE_EQN_FLAG_VAR )
137  {// if NEGBEF follows a variable, AND is assumed
139  Flag = PARSE_EQN_FLAG_OPER;
140  }
142  break;
143  case PARSE_EQN_SYM_AND:
144  case PARSE_EQN_SYM_OR:
145  if ( Flag != PARSE_EQN_FLAG_VAR )
146  {
147  fprintf( pOutput, "Parse_FormulaParserEqn(): There is no variable before AND, EXOR, or OR.\n" );
148  Flag = PARSE_EQN_FLAG_ERROR;
149  break;
150  }
151  if ( *pTemp == PARSE_EQN_SYM_AND )
153  else //if ( *pTemp == PARSE_EQN_SYM_OR )
155  Flag = PARSE_EQN_FLAG_OPER;
156  break;
157  case PARSE_EQN_SYM_OPEN:
158  if ( Flag == PARSE_EQN_FLAG_VAR )
159  {
160 // Parse_StackOpPush( pStackOp, PARSE_EQN_OPER_AND );
161  fprintf( pOutput, "Parse_FormulaParserEqn(): An opening paranthesis follows a var without operation sign.\n" );
162  Flag = PARSE_EQN_FLAG_ERROR;
163  break;
164  }
166  // after an opening bracket, it feels like starting over again
167  Flag = PARSE_EQN_FLAG_START;
168  break;
169  case PARSE_EQN_SYM_CLOSE:
170  if ( !Parse_StackOpIsEmpty( pStackOp ) )
171  {
172  while ( 1 )
173  {
174  if ( Parse_StackOpIsEmpty( pStackOp ) )
175  {
176  fprintf( pOutput, "Parse_FormulaParserEqn(): There is no opening paranthesis\n" );
177  Flag = PARSE_EQN_FLAG_ERROR;
178  break;
179  }
180  Oper = Parse_StackOpPop( pStackOp );
181  if ( Oper == PARSE_EQN_OPER_MARK )
182  break;
183 
184  // perform the given operation
185  if ( Parse_ParserPerformTopOp( pMan, pStackFn, Oper ) == NULL )
186  {
187  Parse_StackFnFree( pStackFn );
188  Parse_StackOpFree( pStackOp );
189  fprintf( pOutput, "Parse_FormulaParserEqn(): Unknown operation\n" );
190  ABC_FREE( pFormula );
191  return NULL;
192  }
193  }
194  }
195  else
196  {
197  fprintf( pOutput, "Parse_FormulaParserEqn(): There is no opening paranthesis\n" );
198  Flag = PARSE_EQN_FLAG_ERROR;
199  break;
200  }
201  if ( Flag != PARSE_EQN_FLAG_ERROR )
202  Flag = PARSE_EQN_FLAG_VAR;
203  break;
204 
205 
206  default:
207  // scan the next name
208  for ( i = 0; pTemp[i] &&
209  pTemp[i] != ' ' && pTemp[i] != '\t' && pTemp[i] != '\r' && pTemp[i] != '\n' &&
210  pTemp[i] != PARSE_EQN_SYM_AND && pTemp[i] != PARSE_EQN_SYM_OR && pTemp[i] != PARSE_EQN_SYM_CLOSE; i++ )
211  {
212  if ( pTemp[i] == PARSE_EQN_SYM_NEG || pTemp[i] == PARSE_EQN_SYM_OPEN )
213  {
214  fprintf( pOutput, "Parse_FormulaParserEqn(): The negation sign or an opening paranthesis inside the variable name.\n" );
215  Flag = PARSE_EQN_FLAG_ERROR;
216  break;
217  }
218  }
219  // variable name is found
220  fFound = 0;
221  Vec_PtrForEachEntry( char *, vVarNames, pName, v )
222  if ( strncmp(pTemp, pName, i) == 0 && strlen(pName) == (unsigned)i )
223  {
224  pTemp += i-1;
225  fFound = 1;
226  break;
227  }
228  if ( !fFound )
229  {
230  fprintf( pOutput, "Parse_FormulaParserEqn(): The parser cannot find var \"%s\" in the input var list.\n", pTemp );
231  Flag = PARSE_EQN_FLAG_ERROR;
232  break;
233  }
234  if ( Flag == PARSE_EQN_FLAG_VAR )
235  {
236  fprintf( pOutput, "Parse_FormulaParserEqn(): The variable name \"%s\" follows another var without operation sign.\n", pTemp );
237  Flag = PARSE_EQN_FLAG_ERROR;
238  break;
239  }
240  Parse_StackFnPush( pStackFn, Hop_IthVar( pMan, v ) ); // Cudd_Ref( pbVars[v] );
241  Flag = PARSE_EQN_FLAG_VAR;
242  break;
243  }
244 
245  if ( Flag == PARSE_EQN_FLAG_ERROR )
246  break; // error exit
247  else if ( Flag == PARSE_EQN_FLAG_START )
248  continue; // go on parsing
249  else if ( Flag == PARSE_EQN_FLAG_VAR )
250  while ( 1 )
251  { // check if there are negations in the OpStack
252  if ( Parse_StackOpIsEmpty(pStackOp) )
253  break;
254  Oper = Parse_StackOpPop( pStackOp );
255  if ( Oper != PARSE_EQN_OPER_NEG )
256  {
257  Parse_StackOpPush( pStackOp, Oper );
258  break;
259  }
260  else
261  {
262  Parse_StackFnPush( pStackFn, Hop_Not((Hop_Obj_t *)Parse_StackFnPop(pStackFn)) );
263  }
264  }
265  else // if ( Flag == PARSE_EQN_FLAG_OPER )
266  while ( 1 )
267  { // execute all the operations in the OpStack
268  // with precedence higher or equal than the last one
269  Oper1 = Parse_StackOpPop( pStackOp ); // the last operation
270  if ( Parse_StackOpIsEmpty(pStackOp) )
271  { // if it is the only operation, push it back
272  Parse_StackOpPush( pStackOp, Oper1 );
273  break;
274  }
275  Oper2 = Parse_StackOpPop( pStackOp ); // the operation before the last one
276  if ( Oper2 >= Oper1 )
277  { // if Oper2 precedence is higher or equal, execute it
278  if ( Parse_ParserPerformTopOp( pMan, pStackFn, Oper2 ) == NULL )
279  {
280  fprintf( pOutput, "Parse_FormulaParserEqn(): Unknown operation\n" );
281  ABC_FREE( pFormula );
282  Parse_StackFnFree( pStackFn );
283  Parse_StackOpFree( pStackOp );
284  return NULL;
285  }
286  Parse_StackOpPush( pStackOp, Oper1 ); // push the last operation back
287  }
288  else
289  { // if Oper2 precedence is lower, push them back and done
290  Parse_StackOpPush( pStackOp, Oper2 );
291  Parse_StackOpPush( pStackOp, Oper1 );
292  break;
293  }
294  }
295  }
296 
297  if ( Flag != PARSE_EQN_FLAG_ERROR )
298  {
299  if ( !Parse_StackFnIsEmpty(pStackFn) )
300  {
301  gFunc = (Hop_Obj_t *)Parse_StackFnPop(pStackFn);
302  if ( Parse_StackFnIsEmpty(pStackFn) )
303  if ( Parse_StackOpIsEmpty(pStackOp) )
304  {
305  Parse_StackFnFree(pStackFn);
306  Parse_StackOpFree(pStackOp);
307 // Cudd_Deref( gFunc );
308  ABC_FREE( pFormula );
309  return gFunc;
310  }
311  else
312  fprintf( pOutput, "Parse_FormulaParserEqn(): Something is left in the operation stack\n" );
313  else
314  fprintf( pOutput, "Parse_FormulaParserEqn(): Something is left in the function stack\n" );
315  }
316  else
317  fprintf( pOutput, "Parse_FormulaParserEqn(): The input string is empty\n" );
318  }
319  ABC_FREE( pFormula );
320  return NULL;
321 }
#define PARSE_EQN_SYM_NEG
Definition: parseEqn.c:36
#define PARSE_EQN_FLAG_OPER
Definition: parseEqn.c:49
Parse_StackOp_t * Parse_StackOpStart(int nDepth)
Definition: parseStack.c:156
static Hop_Obj_t * Hop_ManConst1(Hop_Man_t *p)
Definition: hop.h:132
void * Parse_StackFnPop(Parse_StackFn_t *p)
Definition: parseStack.c:115
#define PARSE_EQN_SYM_AND
Definition: parseEqn.c:37
#define PARSE_EQN_OPER_OR
Definition: parseEqn.c:43
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
void Parse_StackFnFree(Parse_StackFn_t *p)
Definition: parseStack.c:136
static Hop_Obj_t * Hop_Not(Hop_Obj_t *p)
Definition: hop.h:127
Definition: hop.h:65
int Parse_StackFnIsEmpty(Parse_StackFn_t *p)
Definition: parseStack.c:78
int Parse_StackOpIsEmpty(Parse_StackOp_t *p)
Definition: parseStack.c:177
#define PARSE_EQN_SYM_CLOSE
Definition: parseEqn.c:33
if(last==0)
Definition: sparse_int.h:34
static Hop_Obj_t * Parse_ParserPerformTopOp(Hop_Man_t *pMan, Parse_StackFn_t *pStackFn, int Oper)
Definition: parseEqn.c:334
#define PARSE_EQN_FLAG_START
Definition: parseEqn.c:47
void Parse_StackOpPush(Parse_StackOp_t *p, int Oper)
Definition: parseStack.c:193
char * sprintf()
#define PARSE_EQN_SYM_CONST1
Definition: parseEqn.c:35
#define PARSE_EQN_FLAG_VAR
Definition: parseEqn.c:48
typedefABC_NAMESPACE_HEADER_START struct ParseStackFnStruct Parse_StackFn_t
INCLUDES ///.
Definition: parseInt.h:43
#define PARSE_EQN_FLAG_ERROR
Definition: parseEqn.c:50
#define PARSE_EQN_STACKSIZE
Definition: parseEqn.c:52
#define PARSE_EQN_SYM_OPEN
DECLARATIONS ///.
Definition: parseEqn.c:32
#define PARSE_EQN_OPER_AND
Definition: parseEqn.c:42
void Parse_StackOpFree(Parse_StackOp_t *p)
Definition: parseStack.c:235
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define PARSE_EQN_SYM_OR
Definition: parseEqn.c:38
int strncmp()
void Parse_StackFnPush(Parse_StackFn_t *p, void *bFunc)
Definition: parseStack.c:94
#define PARSE_EQN_OPER_MARK
Definition: parseEqn.c:44
Parse_StackFn_t * Parse_StackFnStart(int nDepth)
GLOBAL VARIABLES ///.
Definition: parseStack.c:57
int strlen()
#define PARSE_EQN_SYM_CONST0
Definition: parseEqn.c:34
static Hop_Obj_t * Hop_ManConst0(Hop_Man_t *p)
Definition: hop.h:131
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
int Parse_StackOpPop(Parse_StackOp_t *p)
Definition: parseStack.c:214
Hop_Obj_t * Hop_IthVar(Hop_Man_t *p, int i)
FUNCTION DEFINITIONS ///.
Definition: hopOper.c:63
#define PARSE_EQN_OPER_NEG
Definition: parseEqn.c:41
Hop_Obj_t * Parse_ParserPerformTopOp ( Hop_Man_t pMan,
Parse_StackFn_t pStackFn,
int  Oper 
)
static

Function*************************************************************

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

Description []

SideEffects []

SeeAlso []

Definition at line 334 of file parseEqn.c.

335 {
336  Hop_Obj_t * gArg1, * gArg2, * gFunc;
337  // perform the given operation
338  gArg2 = (Hop_Obj_t *)Parse_StackFnPop( pStackFn );
339  gArg1 = (Hop_Obj_t *)Parse_StackFnPop( pStackFn );
340  if ( Oper == PARSE_EQN_OPER_AND )
341  gFunc = Hop_And( pMan, gArg1, gArg2 );
342  else if ( Oper == PARSE_EQN_OPER_OR )
343  gFunc = Hop_Or( pMan, gArg1, gArg2 );
344  else
345  return NULL;
346 // Cudd_Ref( gFunc );
347 // Cudd_RecursiveDeref( dd, gArg1 );
348 // Cudd_RecursiveDeref( dd, gArg2 );
349  Parse_StackFnPush( pStackFn, gFunc );
350  return gFunc;
351 }
Hop_Obj_t * Hop_Or(Hop_Man_t *p, Hop_Obj_t *p0, Hop_Obj_t *p1)
Definition: hopOper.c:171
Hop_Obj_t * Hop_And(Hop_Man_t *p, Hop_Obj_t *p0, Hop_Obj_t *p1)
Definition: hopOper.c:104
void * Parse_StackFnPop(Parse_StackFn_t *p)
Definition: parseStack.c:115
#define PARSE_EQN_OPER_OR
Definition: parseEqn.c:43
Definition: hop.h:65
#define PARSE_EQN_OPER_AND
Definition: parseEqn.c:42
void Parse_StackFnPush(Parse_StackFn_t *p, void *bFunc)
Definition: parseStack.c:94