2009年2月27日 星期五

Setup Lex and Yacc From Unix to Linux

首先,先替王立天老師的編譯器課程終於開課了高興一下,也讓老師搬出了多年前回台灣就封起來的紙箱從見天日啦。給我們以前在外國的學生所交的作業,並要我們先把讓這些程式可以執行。大致跟老師討論一下程式的內容,花了點時間研究一下,也成功的執行。大概的整理了一下,以 Eishi 同學所繳交的 asgn1 為例,筆記如下:

Step 1. Makefile

Step 1-1. Introduction
因為程式是經由 make 來編譯的,所以先了解一下 Makefile 是做什麼的。可以到 跟我一起寫 Makefile 這裡來了解一下。只需閱讀 概述 MakeFile 介紹 即可。

Step1-2. Error : No targets specified and no makefile found. Stop.
打開 asgn1 可以發現裡有 LEX.l , GRAM.y 和 MAKEFILE。直接給他執行 make,會得到
make: *** No targets specified and no makefile found. Stop.
這樣的錯誤,根據 Appendix B Errors Generated by Make 的說明是找不到 makefile。我記得 Linux 中的 make 的檔名應該只有 Makefile 和 makefile,所以重新命名就解決了。
mv MAKEFILE makefile


Step 1-3. Error : *** missing separator. Stop.
重新命名後,執行 make ,卻發生錯誤
makefile:1:*** missing separator. Stop.
那個 1 應該是代表行數,第一行就發生錯誤...。打開 Makefile,第一行和第二行是 /* ... */ 的註解,但是可以看到下面是以有亮顯 # 的符號來當做註解。第一直覺就是 /* ... */ 不是 Makefile 的註解,刪掉。

Step 1-4. Error : makefile:2: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
再度 make ,出現的錯誤是
makefile:2: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
Orz...看了一下錯誤說明,了解 Makefile 裡面的分隔字元都必須很清楚的定義而且都有固定的格式。打開 Makefile ,箭頭直接瞄準 command 區塊,把左邊的空白字元清掉,以 Tab 代替,亮顯就出現了!以此類推,下面的也改一改。 clean 左邊有一個空白字元也刪掉。

Step 1-5. Error : make: *** No rule to make target `y_tab.obj', needed by `asgn1.exe'. Stop.
看來是因為要產生 asgn1.exe 而找不到 y_tab.obj,打開 makefile,把最下面的 rule

y_tab_obj : y_tab.c lexyy.c
tcc -c -w- y_tab.c

改成

y_tab.obj : y_tab.c lexyy.c
tcc -c -w- y_tab.c

Step 1-6. Error : make: *** No rule to make target `gram.y', needed by `y_tab.c'. Stop.
這訊息是說找不到指定的 gram.y,老師有跟我說 UNIX 是不分大小寫的,但是 Linux 有分。所以把 GRAM.y 和 LEX.l 分別改成 gram.y 和 lex.l 就解決了兩個問題了。

Step 1-7. Error : lex.l:36: unrecognized rule
發生這錯誤,明顯的就是 lex 的程式有問題,打開 lex.l ,找到 36 行。區塊唯一沒亮顯就是他...看了一下,給他一個空格來區隔 match 和 function 就可以了。

Step 2. Start Make
Step 2-1. Error : make: tcc: Command not found
再 make lex.l 時,可能產生了一些 warning ,但我想應該不影響,所以先不理他。看看這個錯誤訊息,找不到 tcc 這個指令。查了一下,tcc 是 Tiny C Compiler 。Linux 下都用 gcc ,所以把 tcc 的部份修改成 gcc ,並對照一下 option 修改所需要的參數。如下:

gcc -c file_name


Step 2-2. Error : gcc: y_tab.c: No such file or directory
很明顯的是因為 gcc 找不到要 compile 的檔案,看了一下,資料夾卻有 y.tab.c。這邊可以知道 Yacc 所產生的檔案應為 y.tab.c ,而不是 y_tab.c,修改

y_tab.obj : y_tab.c lexyy.c
gcc -c y_tab.c



y_tab.obj : y_tab.c lexyy.c
gcc -c y.tab.c

*注意:這邊 make 裡面用的是 bycc ,實為 Berkeley Yacc 若無安裝,改成 yacc 即可。

Step 2-3. Error : gram.y:89:20: error: lexyy.c: No such file or directory
嗯...找不到 lexyy.c,快瘋了喔!看了資料夾,裡面有 lex.yy.c,所以 Lex 所產生的檔案應為 lex.yy.c 而不是 lexyy.c。打開 gram.y ,尋找 lexyy.c 並修改即可。

Step 2-4. Error : gcc: y_tab.obj: No such file or directory
一樣,找不到該檔,打開 makefile :

asgn1.exe : y_tab.obj
gcc -c y.tab.o

修改成

asgn1.exe : y_tab.obj
gcc -o asgn1.exe y.tab.o

即可,gcc 的參數設定可自行執行 gcc --help 來了解

Step 2-5. Error : y.tab.c:(.text+0xb31): undefined reference to `yywrap'
Google 一下,這個錯誤可以透過加上

int yywrap()
{
return(1);
}

來解決。打開 gram.y 發現原本就有加了,只是備註解掉而已。

Step 3. Make Successed
Step 3-1. asgn1.exe
終於,make 沒有錯誤訊息了,並且產生了一個 asgn1.exe 的檔案。接下來可以自己玩一玩了,透過
./asgn1.exe

執行,然後輸入 +,/ 和 if,else,while,或是亂輸入也可以喔!

2009年2月25日 星期三

jQuery Plugins 介紹

jQuery 好用又強大,但也因為如此,基於 jQuery 開發的 Plugin 也就多到一個恐怖的地步。同樣的一種效果,可能有幾十種的選擇,真的會讓人家很困擾。我想大家應該都有一樣的困擾,所以就找了些人家整理的資料:

http://ka-yue.com/blog/useful-jquery-plugin
介紹了十七種常用的 Plugins

http://www.open-open.com/ajax/jQuery.htm
專門在收集 jQuery 的 Plugins 和截圖

2009年2月24日 星期二

Windows Server 2003 - Windows Media Service and FTP

今天花了一些時間幫教資中心架設一台多媒體串流的伺服器,系統是 Windows Server 2003,並且要以 Windows Media Service 來提供影音串流的服務。

在 Windows Media Service 設定的部分非常的簡單,只需要以下步驟 :
  1. 新增/移除程式
  2. 管理 Windows 元件
  3. 勾選 Windows Media Service 並安裝
  4. 在程式集裡面啟動 Windows Media Service ,再設定幾下就好了。
接下來要提供 FTP 的服務,讓他們可以透過 FTP 來上傳影片到指定的資料夾下 ,過程也是很簡單,只要參照下面這個網址做就行了:
並且新增個使用者,開啟寫入權限等等。原本以為這樣就結束了,結果使用 FTP Client 軟體連線時,會卡在 Entering Passive Mode,然後出現 list error 等錯誤訊息。Google 了一下,原來是因為啟動了 Windows Firewall 導致 Passive Ports 無法存取而發生的錯誤。下面的網址有提供解決的方法 :
大致設定如下:

On Windows 2003 Server with IIS6
  • To Enable Direct Metabase Edit
    1. Open the IIS Microsoft Management Console (MMC).
    2. Right-click on the Local Computer node.
    3. Select Properties.
    4. Make sure the Enable Direct Metabase Edit checkbox is checked.
  • Configure PassivePortRange via ADSUTIL script
    1. Click Start, click Run, type cmd, and then click OK.
    2. Type cd Inetpub\AdminScripts and then press ENTER.
    3. Type the following command where the range is specified in "..". cscript.exe adsutil.vbs set /MSFTPSVC/PassivePortRange "5001-5201"
    4. Restart the FTP Publishing Service.
    You'll see the following output, when you configure via ADSUTIL script:

    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    PassivePortRange : (STRING) "5001-5201"

  • Add each port to the Windows Firewall
    1. Click Start, click Control Panel, open Windows Firewall, and select the Exceptions tab.
    2. Click the Add Port button.
    3. Enter a Name for the Exception and the first number in the port range.
    4. Click TCP if not already selected and click OK.
    5. Repeat for each port in the range - for large ranges see the end of the document.
    6. Enable the Windows Firewall on the General Tab.


因為要開啟的 Ports 非常的多,所以它寫了一個小小的 Script 來開啟所有 Port :

To add a range of ports to Windows Firewall from the Command Line
  1. Click Start, click Run, type cmd, and then click OK.
  2. Type in the following where the range is specified in ( ) and the name of the firewall entry is in " ".
    FOR /L %I IN (5001,1,5201) DO netsh firewall add portopening TCP %I "Passive FTP"%I
  3. Each port in the range will be added with an "OK" confirmation.


你就看到一堆 Port 開啟的訊息出現了。最後再用 FTP Client 連線,順利完成 !

如果是使用 Windows Server 2000 的話 :

On Windows 2000 Server with IIS5 Configure PassivePortRange via Registry Editor
  1. Start Registry Editor (Regedt32.exe).
  2. Locate the following registry key: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Msftpsvc\Parameters\
  3. Add a value named "PassivePortRange" (without the quotation marks) of type REG_SZ.
  4. Close Registry Editor.
  5. Restart the FTP Publishing Service.
    Note: The range that FTP will validate is from 5001 to 65535.

2009年2月8日 星期日

在 Ubuntu 中安裝 Flex 與 Bison

非常的簡單,直接安裝就行了:
sudo apt-get install flex bison

再試著執行:
flex -h

以及
bison -h

都有跑出說明資訊就代表安裝成功了。

2009年2月7日 星期六

Lex and Yacc ( or Flex and Bison )

Lex - Lexical Analyzer Generator
用來產生以 C 語言實做的字彙分析器(Lexical Analyzer)的原始碼的程式,只需給予使用者自訂的正規表示式。所謂的字彙分析器,即將一連串的輸入資料切割成有意義的小單位並輸出。

Yacc - Yet Another Compiler-Compiler
用來產生以 C 語言實做的剖析器(Parser)的原始碼的程式,只需給予使用者自訂的語法。主要目的就是用來判斷所輸入的資料是否符合語法。

Flex - Fast Lexical Analyzer Generator
由 Lex 所改寫,改進了原本的缺點,並且更快。

Bison
Yacc 的另一個選擇,極度相容於 Yacc,更是由鼎鼎大名的自由軟體之父 - Richard Stallman 所領導製作的。

Lex 與 Yacc 是 Unix 系統的標準配備,而 Linux 的選擇就是 Flex 與 Bison 了。它們都是用來製作程式編譯器的標準工具。

淺談編譯器
編譯器雖然一直都是資訊相關領域必備的知識,但在近代已不再是一個熱門議題。這也意味著編譯器已是相當成熟的技術,不再具有競爭的價值。而隨著近年新興的直譯程式語言的竄紅(Ruby、Python 和 Groovy 等等),相關的資訊似乎又開始引起大家的注意和興趣。使得由 Aho 等作者,在西元 1986 年所出版的編譯器聖經本 Compilers: Principles, Techniques, and Tools. [First Edition],在 20 年後的西元 2006 年出了第二版 Compilers: Principles, Techniques, and Tools (2nd Edition),封面也還是經典的屠龍武士與飛龍。



參考資料:
http://dinosaur.compilertools.net/
http://www.amazon.com/
http://en.wikipedia.org/