Wednesday, March 31, 2010

CSS Position

In the CSS, one of the most difficult concepts is the position property. There is a wonderful page Learn CSS position in ten steps. Simple, detail and useful with examples.


Building a simple compiler series (8)–Yacc

Yacc(Yet another compiler-compiler), another powerful tool for generator a parser according to a specify context free grammar.

Yacc provides a general tool for imposing structure on the input to a computer program. The Yacc user prepares a specification of the input process; this includes rules describing the input structure, code to be invoked when these rules are recognized, and a low-level routine to do the basic input. Yacc then generates a function to control the input process. This function, called a parser, calls the user-supplied low-level input routine (the lexical analyzer) to pick up the basic items (called tokens) from the input stream. These tokens are organized according to the input structure rules, called grammar rules; when one of these rules has been recognized, then user code supplied for this rule, an action, is invoked; actions have the ability to return values and make use of the values of other actions.

Yacc file specifcation just like below which is similar with the Lex file specification.

declarations
%%
rules
%%
programs

Here is a quick demo which based on here. You can refer to that link for more iinformation.I add a main function and a yyerror function. I will add more detail information and other examples in next post. Here is just a simple example which shows what’s the yacc like, how to use it, and what is it like.

%{
#  include
#  include
int  regs[26];
int  base;
%}
%start  list
%token  DIGIT  LETTER
%left  '|'
%left  '&'
%left  '+'  '-'
%left  '*'  '/'  '%'
%left  UMINUS      /*  supplies  precedence  for  unary  minus  */
%%      /*  beginning  of  rules  section  */
list
:    /*  empty  */
     
|  list  stat  '\n'
     
|  list  error  '\n'
               
{    yyerrok;  }
     
;
stat
:    expr
               
{    printf( "%d\n", $1 );  }
     
|  LETTER  '='  expr
               
{    regs[$1]  =  $3;  }
     
;
expr
:    '('  expr  ')'
               
{    $$  =  $2;  }
     
|  expr  '+'  expr
               
{    $$  =  $1  +  $3;  }
     
|  expr  '-'  expr
               
{    $$  =  $1  -  $3;  }
     
|  expr  '*'  expr
               
{    $$  =  $1  *  $3;  }
     
|  expr  '/'  expr
               
{    $$  =  $1  /  $3;  }
     
|  expr  '%'  expr
               
{    $$  =  $1  %  $3;  }
     
|  expr  '&'  expr
               
{    $$  =  $1  &  $3;  }
     
|  expr  '|'  expr
               
{    $$  =  $1  |  $3;  }
     
|  '-'  expr       %prec  UMINUS
               
{    $$  =  -  $2;  }
     
|  LETTER
               
{    $$  =  regs[$1];  }
     
|  number
     
;
number    
:   DIGIT
               
{    $$ = $1; base  =  ($1==0)  ?  8  :  10;  }
     
|  number  DIGIT
               
{    $$  =  base * $1  +  $2;  }
     
;
%%      /*  start  of  programs  */
yylex
() {      /*  lexical  analysis  routine  */
             
/*  returns  LETTER  for  a  lower  case  letter,  yylval = 0  through  25  */
             
/*  return  DIGIT  for  a  digit,  yylval = 0  through  9  */
             
/*  all  other  characters  are  returned  immediately  */
     
int  c;
     
while(  (c=getchar())  ==  ' '  )  {/*  skip  blanks  */  }
     
/*  c  is  now  nonblank  */
     
if(  islower(  c  )  )  {
          yylval  
=  c  -  'a';
         
return  (  LETTER  );
         
}
     
if(  isdigit(  c  )  )  {
          yylval  
=  c  -  '0';
         
return(  DIGIT  );
         
}
     
return(  c  );
     
}
main
()
{
   
return yyparse();
}
int yyerror(char *s)
{
    fprintf
(stderr,"%s\n",s);
   
return 0;
}

The input file.

The result of the expression.

 

SICP Series-chapter 1.2.1-1.2.2

When we talk about the recursive, factorial is one of the most classical problem. In the LISP world, recursive is very normal, but you also can use the “iterative process”. But this is a special iterative process. You can take a look at the example below to compare it with others which are written by other programming language.
(defun fact-iter(product iter maxiter)
  (if (> iter maxiter)
      product
      (fact-iter (* product iter)
                 (+ iter 1)
                 maxiter)))

(defun fact (n)
  (fact-iter 1 1 n))


1.9 The first one is recursive, the second one is iterative.

1.10

(defun A (x y)
  (cond ((= y 0) 0)
        ((= x 0) (* y 2))
        ((= y 1) 2)
        (t (A (- x 1)
              (A x (- y 1))))))

(A 1 10)
(A 0 (A 1 9))
(* 2 (A 1 9))
(* 2 (A 0 (A 1 8)))
(* 2 (* 2 (A 1 8)))
….
You can see that the (A 1 10)=2^10

(defun f (n) (A 0 n)) ;2n
(defun g (n) (A 1 n));2^n
(defun h (n) (A 2 n)); 2^2^… (n times)

Another common pattern of computation is called tree recursive, Fibonacci is one of the most classical example.
1.11 Tree recursive is very inefficiency, when I take the n as 100, my laptop runs for a very long time. When I use the iterative version, even the n is 10000, it takes less then one second. You can see the run-time information when the n=2000 and n=20. You will see the big gap between them.

(defun f-rec(n)
    (cond ((< n 3) n)
           (t (+ (f-rec (- n 1))
                 (* 2 (f-rec (- n 2)))
                 (* 3 (f-rec (- n 3)))))))
 
(defun f-iter(n)
    (if (< n 3)
        n
        (f-iter-aux 2 1 0 n)))
 
(defun f-iter-aux (a b c n)
    (if (= n 2)
        a
        (f-iter-aux (+ a
                        (* 2 b)
                        (* 3 c))
                    a
                    b
                    (- n 1))))


1.12 This exercise is very similar to the Fibonacci
(defun pascal (positionr positionc)
    (cond ((= positionr 1) 1)
          ((= positionc positionr) 1)
          ((= positionc 1) 1)
          (t (+ (pascal (- positionr 1) positionc)
                (pascal (- positionr 1) (- positionc 1))))))



Regular Expression to replace number with new value based on orig

Today, I meet a problem, I need replace a lot of numbers in a file with a new value which is based on original value, like the original value is 50, I need use 50*0.8 to replace it.  Regular expression is the best choice, which is a Swiss knife for software engineer. But I am shamed to say that I am not familiar with the regular expression. But we have powerful Stackoverflow. I asked a question in this site, Jens A gave me a pretty answer about it soon.
String s = "This is the number 2.5. And this is 7";
s = Regex.Replace(s, @"(\+|-)?\d+(\.\d*)?", m => {return (Double.Parse(m.ToString())*0.8).ToString();});

Tuesday, March 30, 2010

SICP Series-chapter 1.1

In the section, it gives some very basic introduction about the LISP(a familiar of LISP, mainly Schema in this book). Like the basic expression, naming ,environment and other element of language.

1.4. The common lisp has some difference with Schema, so I re-write the small program in the exercise. Actually, you can use the function name as you like, just like the example in the exercise. But in the Common Lisp, if you want to tell the interpreter that a name is a function , you need add a key word function. If you want to call a function based on a symbol, you need use the funcall key word to tell the interpreter, you also can use the #’ to instead of funcall.

(defun a-plus-b-v2 (a b)
    (funcall (if (> b 0)
                 (function +)
                 (function -))
             a
             b
))

1.5 From the code, you can easily know that the p is a infinite recursive function. If the test function return 0, it means normal order, the p would be evaluated until it’s actually needed.

From the screenshot, we can tell that it’s normal order.

1.6 It would cause a infinite function call.

1.7  According to the exercise, the good-enough? function may cause big problem when the number is very small. From the code we can see that it use the guess number’s square to compare the original number, the comparison number is o.oo1. When the number is larger, it’s ok. But when the number is very small, like 0.06 or 0.005, it would cause big inaccuracy. In order to decrease the inaccuracy, there is a strategy that compare last iteration’s guess with current guess.

(defun sqrt-iter(guess x)
    (let (improveguess (improve guess x))
        (if (good-enoughp guess improveguess)
            improveguess
            (sqrt-iter (improve improveguess x)
                        x))))
 
(defun improve (guess x)
    (average guess (/ x guess)))
 
(defun average (x y)
    (/ (+ x y) 2))
 
(defun good-enoughpv2 (guess x)
    (let (temp (/ guess x))
        (and (> temp 0.99) (< temp 1.01))))
 
(defun sqrtv(x)
    (sqrt-iter 1.0 x))
1.8 This is the same as the example in the textbook. The only different is the improve-guess function


Structure and Interpretation of Computer Programs Note Series

I have mentioned in my previous post that I have two goals this year, one is to build a simple compiler, another one is to write a LISP interpreter using LISP. I am learning the compiler principle now. It’s time to start my second goal. I am always willing to learn the Common Lisp. I have been learning it from last year, but I am just reading some manuals and some articles. SICP is a an excellent book about the LISP, not only the language itself, but also some great minds about computer science.  Doing the excises and writing a small interpreter just like the Register Machine in chapter 5 is a good path to master this language and learn those great minds.

Structure and Interpretation of Computer Programs is a great book which are used by many universities as textbook published by MIT. But, recently, MIT uses the Python to replace the LISP in his course.The language used in this book is a dialect of LISP called Schema, I will use the Common Lisp in this note series. (Actually, clisp)



Monday, March 29, 2010

Different about DateDiff between SSMS and ReportBuilder

Recently, I am building a report using the SQL Server reporting service. I met a problem about datediff.  There are two datetime values 2010-04-23 08:00:00 and 2010-04-22 17:00:00. If you use the TSQL built-in function DATEDIFF to check the value in SQL Server Management Studio  like below:

DATEDIFF(d,'2010-04-23 08:00:00','2010-04-22 17:00:00')

You would get the -1.
But, if you use the DATEDIFF in the ReportBuilder, it would return 0. I guess that Within SSMS if you cross midnight you have triggered a day boundary so  it get -1. in ReportBuilder it is looking for 24 hours to be between the two values so it gets 0.