2011年11月6日 星期日

lex 學習

Lex是個古老的工具
雖然是個老東西,但是還是挺好用的!
Lex的功用主要是對一個文件寫下rule
然後產生一個compiler去paser這種文件
安裝:
目前Lex / Flex在linux下皆可以安裝執行
Ubuntu為例,只要下指令
% apt-get install flex
即自動幫你安裝完成
接著只要輸入指令flex即可執行Lex程式了
執行Lex的順序:
Lex的input file,必須是*.l 的檔案 ( 副檔名為l ... 小寫的L )
接著只要輸入指令
% flex test.l
  然後Lex就會自動產生一個output file:lex.yy.c
接著只要compile這個lex.yy.c 就可以執行這個token parser了
% gcc lex.yy.c -ll
  而-ll是為了include lex的library
Lex的Input File架構:
*.l 主要分三個部分:definition & rules & user code
這三個部分以「%%」為分界

  definition
%%
rules
%%
user code


definition:使用者自己定義的變數,都放在這個地方
  rulesparser對token match的規則
user code:最後產生的lex.yy.c最底下會有一模一樣的code
Definition:
在Definition的區間裡,可以宣告一些在rule中的code要使用的變數(寫法跟c一模一樣)
而這些code必須用%{%} 將跨行的code包起來
因為在這個區間的code都會被完完整整、一字不漏地output至lex.yy.c檔中
所以在compile lex.yy.c檔時,才不會產生error!
Ex:
%{//要記錄parser的input file的總字數與行數
int num_char = 0;
int num_line = 0;
%}
%%
\n { num_line++; }
. { num_char++; }

也可以宣告一些「rule的變數」,讓rule的寫法更簡潔
寫法為:
name definition

Ex :
number [0-9]+
identifier [a-zA-Z_][a-zA-Z_0-9]*
%%
{number} printf("%s this token is a number\n", yytext);
{identifier} printf("%s this token is a identifier\n", yytext);

上面的意思其實就是...
{[0-9]+} printf("%s this token is a number\n", yytext);
{[a-zA-Z_][a-zA-Z_0-9]*} printf("%s this token is a identifier\n", yytext);

Rule:
要對input file切token的規則,全寫在這裡。
寫法的規則是:
pattern action

pattern可以輸入一些正規表示法,或是一些word,而正規表示法在此不再贅述
想了解的人,自己想辦法吧,筆者累了....Or2...
action則是當pattern match後,執行相對應的code(跟c一模一樣),因此這些code會原封不動地寫入output file中
若action的code太多,則可以用"{" "}"跨行將code包起來
Ex:
[0-9]+ ECHO;printf("this is a number!\n");
等同於...
[0-9]+ {
ECHO;
printf("this is a number!\n");
}

在這邊有一個特別的word可以用在action中
ECHO 可以印出yytext(match pattern的字串)中的內容至output中

Global Variable:
這個是lex的預設變數,在寫*.l檔的definition & rule時,可以直接使用這些變數
yyin 是lex的input來源,型態為FILE * ,初始預設為stdin
yytext 當rule中match一個pattern時,match的string就會存在yytext中,型態為char *
yyleng 記錄yytext的長度
yylineno 記錄目前的yyin讀到第幾行了


Example:
這是一個計算input file的總字數&行數的lex檔  

%{
int num_lines = 0, num_chars = 0;
%}

%%
\n   { ++num_lines; ++num_chars; }
.    { ++num_chars; }

%%
main()
{
yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
}
 
 
執行方式:
flex [filename].l
gcc lex.yy.c -ll
cat [filename] | ./a.out //把此文件與pipeline到執行檔產出
如:
file => 
---------------------------- 
1
23
4
5
6
7123adsf
sdf
--------------------------------
下:
flex file.l 
gcc lex.yy.c -ll 
cat file | ./a.out 
 
print => 
24 7 7     //24個字元 個字數 77行
 
解釋:
在這份定義文件內,我們看到在定義區塊裡面有一行C語言的變數宣告,宣告了三個整數變數,nchar、nword及nline,分別用來代表接下來我們要計算的字元數、字數以及行數。

接著在樣式區塊裡面定義了三個樣式,根據我們在上一節裡面學到的對於常規表示式的知識,我們可以知道這三條樣式所代表的意義。 \n   換行字元
 [^ \t\n]+  1個以上的非空白字元
 .   換行字元之外的所有字元我們現在要注意的是在樣式常規表示代的右邊以大括號括起來的程式碼,這就是前頭我們有提到過的動作程式碼。這是在字彙剖析器在分析字串樣式匹配時,若樣式符合且樣式有指定動作程式碼,也就是在樣式右側的程式碼,則這段程式碼就會被執行。

以\n
這條樣式來說明,如果樣式匹配,則程式碼{ nline++; nchar++; 
}會被執行。注意到這裡的大括號,假如動作程式碼只有一行的話,則大括號是可有可無的,像是第三條樣式。但我們一律還是都寫上大括號以免有所遺漏,同時也
要注意的是不管動作程式碼有幾條指令,全部都要寫在同一行裡面。

最後是程式碼區塊。這區塊裡有個main進入點,第一行指令呼叫了
yylex這個函式。在前面的介紹中,我們知道yylex是由lex自動幫我們產生的函式,這個函式的工作就是幫我們作字彙剖析,一直到結束後才會返回。
返回之後,我們再把計算的字元數、字數以及行數列印到畫面上。 
 
from:
http://falldog7.blogspot.com/2007/09/lex.html 
http://good-ed.blogspot.com/2010/04/lexyacc.html

2011年10月17日 星期一


3.1 Compiler vs. Interpreter

An interpreter translates some form of source code into a target representation that it can immediately execute and evaluate. The structure of the interpreter is similar to that of a compiler, but the amount of time it takes to produce the executable representation will vary as will the amount of optimization. The following diagram shows one representation of the differences.
graphic
Compiler characteristics:
  • spends a lot of time analyzing and processing the program
  • the resulting executable is some form of machine- specific binary code
  • the computer hardware interprets (executes) the resulting code
  • program execution is fast
Interpreter characteristics:
  • relatively little time is spent analyzing and processing the program
  • the resulting code is some sort of intermediate code
  • the resulting code is interpreted by another program
  • program execution is relatively slow
The above characteristics are typical. There are well-known cases that are somewhere in between, such as Java with it's JVM.

refer from:

2011年10月15日 星期六

Resource is not public

另外如果使用Theme.Dialog.Alert之類而出現
Error: Resource is not public. (at 'theme' with value '@android:style/Theme.Dialog.Alert')的錯誤,因為在frameworks/base/core /res/res/values/public.xml找不到這個資料

只要加個*改成
  '@*android:style/Theme.Dialog.Alert'
就可以使用了。



refer from:
http://slashgill.blogspot.com/2010/11/theme.html

2011年10月14日 星期五

Login Failed: invalid_key when Android Facebook app is installed


As Sean suggests in his second tack...
I fixed the issue by removing this line (which uses SSO):
    mFacebook.authorize(activity, mAppId, PERMS_NEEDED, new LoginDialogListener(r));
in favor of this line (which does not):
    mFacebook.authorize(
            activity, 
            PERMS_NEEDED,
            Facebook.FORCE_DIALOG_AUTH,   // avoids SSO
            new LoginDialogListener(r));
refer from:
https://github.com/facebook/facebook-android-sdk/issues/191

2011年10月7日 星期五

nVidia BSOD nvlddmkm.sys

I have found and fixed the problem today. This is what appears to happen.
during the installation of the most current drivers 100.65 Vista, an OLD file
nvlddmkm.sys is copied into windows/system32/drivers and not the current one
in the install. As a result the new drivers are attempting to access a file
dated 11/2006 instead of 2/2007 ver 7.15.11.0065 which is in the newest WHQL
driver ver 100.65 vista 32.

Fix: Go to windows/system32/drivers and rename nvlddmkm.sys to
nvlddmkm.sys.old. Go to the nvidia directory and find the file nvlddmkm.sy_
and copy it to windows/system32. Using the cmd window (DOS box) type
EXPAND.EXE nvlddmkm.sy_ nvlddmkm.sys. When the expansion is complete, copy
the new nvlddmkm.sys to windows/system32/drivers and restart the computer.

Your computer should now work properly.

You will notice that any uninstall and reinstall of nvidia drivers will not
remove the old nvlddmkm.sys file and will not overwrite it with the newer
version. You have to do it manually. I do not know why this happens but who
cares as long it is fixed.

Good luck...

"Hohen" wrote:






nvlddmkm.sys 這是NVIDIA的顯卡驅動的文件吧,你入安全模式找到他刪了他,看看能不能開機,要是入到正常模式的話就去下你顯卡最新的FOR win7的驅動吧裝上新的驅動我想應該沒事,
要是你不是一入系統的藍的話那你不用刪直接下最新的驅動裝上看看



refer from:
http://www.vistax64.com/vista-hardware-devices/41867-nvidia-bsod-nvlddmkm-sys.html

2011年10月6日 星期四

android另類結束Activity方法——主動拋出異常





這種方法在網上已經有了部分介紹,但是大部分人不知道怎麼取消Force Close的對話框。「通過重寫Android應用程序的Application基類自己實現 Thread.UncaughtExceptionHandler接口的uncaughtException方法是可以避免出現FC窗口的,用戶感覺直接退出了一樣」。這是網上能找到的說明,這段話讓人看了摸不著頭腦,大部分人不知道怎麼重寫Application基類並實現接口。廢話不多說,直接上代碼! 首先需要在onCreate中添加此句代碼:Thread.setDefaultUncaughtExceptionHandler(new AntrouApp());表明出現異常由自己來處理。 接下來重寫異常處理類 
public class AntrouApp extends Application implements Thread.UncaughtExceptionHandler { 
    @Override 
public void onCreate() { 
// TODO Auto-generated method stub 
super.onCreate(); 
} 
@Override 
    public void uncaughtException(Thread thread, Throwable ex) { 
        android.os.Process.killProcess(android.os.Process.myPid());  
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, ex); 
//若沒有處理,則按照系統自己的處理方式處理 
    } 
} 

此方法會強制結束所有的Activity和service,當然也可以在service裡進行,達到後台強制結束任務的目的。在Activity結束後,系統會回到最近為onpause狀態的Activity。


refer from:
http://www.bangchui.org/simple/?t15896.html

2011年7月4日 星期一

證明 數學歸納法

數學歸納法(Mathematical Induction)立論的基礎是來自良序原理(Well-Ordering Property)。
Well-Ordering Property告訴我們:任何自然數的非空子集合會有一個最小的元素。(Every nonempty subset of the set of positive integers has a least element.)
首先我先以矛盾證法證明數學歸納法的合法性(Validity):
假設已知條件:P(1)為真,且對所有自然數k而言,敘述 P(k)→P(k+1)亦為真。

為證明P(n)對所有自然數n而言恆為真的話,假設存在一自然數n使得P(n)為假。
則根據Well-Ordering Property,讓P(n)為假的自然數集合S是自然數集合N的子集合,它是個非空集合,並存在一個最小的元素,我們稱此元素為m。
我們知道m不會是1,因為已知條件中已給出P(1)為真。
既然m不會是1,那它必定是個大於1的自然數,m-1也會是個自然數。
此外,因為m-1小於m,m已經是S集合中最小的元素了,所以m-1並不屬於S集合;因此知P(m-1)必為真。
所以我們得到了P(m-1)為真,P(m)為假的結論。
根據已知條件:P(k)→P(k+1) (P(k)為真,則P(k+1)亦為真),我們知道這個結論和已知條件矛盾,因此推得"存在一自然數n使P(n)為假"這個敘述是錯誤的!
也就是說,P(n)對任何自然數n恆為真。得證!



refer from:
http://tw.myblog.yahoo.com/jw!XToZojWTHRI2EfbctdR8ag--/article?mid=1628&prev=1629&l=f&fid=78