Saturday, August 22, 2009

Equation Processing with Boost Spirit parserr

One nice example of some code that uses the Boost Spirit parsing framework is to process equation formats and convert them into SSA style form with 2 variables.

# = OR, & = AND, $ = XOR and ! = NEGATION
= is assignment
( ) can be used for grouping, thus
Z = A # B
is Z = A or B, in Boolean logic,

This is for logic synthesis, consider the example

Type an expression...or [q or Q] to quit

z0=x0#(y0$y1);
Parsing succeeded
z1=(!x0)$(!y0);
Parsing succeeded
z2=(x1$x0)&(y0#!x1);
Parsing succeeded
z3=(x0&!x1)$(y1&!y0);
Parsing succeeded

Converting this to BLIF style output cover

InSet has 4 inputs
OutSet has 4 outputs
INORDER = x0 x1 y0 y1 ;
OUTORDER = z0 z1 z2 z3 ;

z0 = T1;
T1 = T0 + x0;
T0 = y1 * !y0 + ! y1 * y0 ;

z1 = T4;
T4 = T3 * !T2 + ! T3 * T2 ;
T3 = !y0;
T2 = !x0;

z2 = T8;
T8 = T7 * T5;
T7 = T6 + y0;
T6 = !x1;
T5 = x0 * !x1 + ! x0 * x1 ;

z3 = T13;
T13 = T12 * !T10 + ! T12 * T10 ;
T12 = T11 * y1;
T11 = !y0;
T10 = T9 * x0;
T9 = !x1;

Bye... :-)

--
Reading the output in reverse order gives you a topologically correctly ordered 2 variable
processing scheme which you can map to logic gates.

The source code uses Boost Spirit parser, as an example:
struct calculator : public grammar {
template
struct definition {
definition(calculator const& /*self*/) {
expression
= str_p("INPUT()")[&do_input]
| str_p("OUTPUT()")[&do_output]
| term
>> *( ('#' >> term)[&do_add]
| ('$' >> term)[&do_xor]
| ('-' >> term)[&do_subt]
);
term
= factor
>> *( ('&' >> factor)[&do_mult]
| ('/' >> factor)[&do_div]
);


Enjoy!!

No comments:

Post a Comment