bison和flex的基本原理

见图:

1. The parser will obtain the first token from the scanner — obtaining “The”,
an article. The parser will push the “The” on the stack (Figure 1-a).

parser得到“The”,它是个冠词,所以parser将它置入堆栈

2. The parser will obtain another token from the scanner — obtaining “man”, a
noun. Since there is an article on the stack, the parser will pop the article
off the stack and replace it with a noun-phrase “The man” (Figure 1-b).
parser得到“man”,它是个名词,因为堆栈中有一个冠词,所以parser将这个冠词从堆栈中取出,和man合并后,放入堆栈
3. The parser will obtain another token from the scanner — obtaining “ate”, a
verb. There is no rule reducing “noun-phrase verb” so it pushes “ate” on the
stack (Figure 1-c).
parser得到“ate”,它是个动词,因为这里没有规则来减少“名词短语 动词”,所以parser将ate压栈

4. The parser will obtain another token from the scanner — obtaining “a”, an
article. There is no rule reducing “noun-phrase verb article” and so it pushes
the “a” on the stack (Figure 1-d).

parser得到“a”,它是个名词,因为这里没有规则来减少“名词短语 动词 冠词”,所以parser将a栈
5. The parser will obtain another token from the scanner — obtaining “worm”,
a noun. The parser can now reduce the “article noun” on the top of the stack
to a noun-phrase and places the noun-phrase on the stack (Figure 1-e).
parser得到“worm”,它是个名词,因为这里有规则“冠词 名词”–>“名词短语”,所以parser将a出栈,和worm合并后压栈
6. The parser will obtain another token from the scanner — obtaining “.”, a
period. The parser can now reduce the “noun-phrase verb noun-phrase period” to
a sentence and places this on the stack (Figure 1-f).

parser得到“.”,它是个分隔符,因为这里有规则“名词短语 动词 名词短语 分隔符”,所以parser将栈情况,在栈中放入一句话“The man ate
a worm”

这里的合并被称为Reduce,入栈被称为Shfit。

其实bison中的parser是一个有限状态机,下面一句话是从gun网站上copy来的,很好的说明了这个原理。

Each time a lookahead(当前获得到的token) token is read, the current parser state
together with the type of lookahead token are looked up in a table. This table
entry can say, “Shift the lookahead token.” In this case, it also specifies
the new parser state, which is pushed onto the top of the parser stack. Or it
can say, “Reduce using rule number n.” This means that a certain number of
tokens or groupings are taken off the top of the stack, and replaced by one
grouping. In other words, that number of states are popped from the stack, and
one new state is pushed.

车子内部雾刷

因为下雨天或者冬天,车窗都是关闭的,但是司机前面的那部分玻璃很容易

别雾气所蒙,这样就看不到前面的路况的,如果加一个类似雨刷的刷子,

就不需要人为的去刷内部的玻璃了。

角度和弧度

弧度=弧长/半径

角度=使用量角器获得的度数

使用弧度的原因是,在微积分,三角函数合并时书写会很简单。

To convertdegrees to radians, multiply by π/180 ;to convert radians to
degrees, multiply by 180/π .

http://www.mathstat.strath.ac.uk/basicmaths/312_degreesandradians.html

三角函数: http://en.wikipedia.org/wiki/Trigonometric_functions

三角函数的等式: http://en.wikipedia.org/wiki/List_of_trigonometric_identities

java中如何实现检查一个数是不是2的幂次方

if((i & (i-1)) == 0) { i 是2的幂次方}



public class Test {
    public static void main(String args[]) {
        isPowerOfTwo(2);
        isPowerOfTwo(4);
        isPowerOfTwo(6);
        isPowerOfTwo(9);
        isPowerOfTwo(1024);
        isPowerOfTwo(2044);
    }

    private static void isPowerOfTwo(int num) {
        System.out.println();
        System.out.println("num is: " + num);
        if (((num - 1) & num) == 0) {
            System.out.println("The number is a power of two");
        } else {
            System.out.println("The number is a  NOT A power of two");
        }
    }

}

http://stackoverflow.com/questions/5082314/power-of-2-formula-help

http://www.vaughns-1-pagers.com/computer/powers-of-2.htm

java中的bitwize操作符

Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Logical Bitwise Operations bit 1 | bit 2 | OR ( | ) | AND ( & )
| XOR ( ^ )
—|—|—|—|—
0 | 0 | 0 | 0 | 0
1 | 0 | 1 | 0 | 1
0 | 1 | 1 | 0 | 1
1 | 1 | 1 | 1 | 0

http://vipan.com/htdocs/bitwisehelp.html