1 / 20

2013 HPDS 寒假訓練課程 Parser and Interpreter.

2013 HPDS 寒假訓練課程 Parser and Interpreter. Presenter: Hung-Fu Li. Lecture 1. Lexical Recognition. 為什麼需要 Lex, 他是什麼 Lex 程式的規則 Regular Expression (regexp). Lecture 1. Lexical Recognition. 為什麼需要 Lex, 他是什麼. 為什麼需要 Lex, 他是什麼. C 語言的課本 在數字系統告訴我們 512  這是 10 進位數字 (DEC)

Download Presentation

2013 HPDS 寒假訓練課程 Parser and Interpreter.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2013HPDS 寒假訓練課程Parser and Interpreter. Presenter: Hung-Fu Li

  2. Lecture 1. Lexical Recognition • 為什麼需要Lex, 他是什麼 • Lex程式的規則 • Regular Expression (regexp)

  3. Lecture 1. Lexical Recognition 為什麼需要Lex, 他是什麼

  4. 為什麼需要Lex, 他是什麼 • C語言的課本 • 在數字系統告訴我們 • 512 這是10進位數字(DEC) • 0123  這是8進位數字(OCT) • 0x8A2C  這是16進位數字(HEX) • 0.123f  這是單精度浮點數(Floating Point) • 0.123 這是雙精度浮點數 • 字元與字串告訴我們 • ‘A’是字元 • “Apple”是字串 • 含有_a,b,c…開頭的可以是變數名稱

  5. 為什麼需要Lex, 他是什麼 • 你可以很清楚辨識C語言的整數、浮點數、字元、字串 • 你可以知道C語言變數怎麼命名 • 你會撰寫符合規定的程式

  6. 為什麼需要Lex, 他是什麼 • 初學者的認知 程式 執行檔

  7. 為什麼需要Lex, 他是什麼 • 進階使用者的認知 程式1 目的碼1 編譯 連結 執行檔 程式2 目的碼2 編譯

  8. 為什麼需要Lex, 他是什麼 • 實際上…這是好幾個程式的步驟 程式1 目的碼1 編譯器 連結器 執行檔 程式2 目的碼2 編譯器 中間碼優化 組譯 前處理

  9. 為什麼需要Lex, 他是什麼 • 編譯器的任務是將原始碼轉為目的碼 • 實際上C的編譯過程包含著 • 前處理:將include檔展開,決定哪些define/if/elif…等的文字取代 • 編譯:將C轉成組合語言 • 程式語言轉成中間碼(IR) • 中間碼指令優化與暫存器評估、轉組合語言 • 組譯:將組合語言轉成目的碼

  10. 為什麼需要Lex, 他是什麼 • 編譯器 • 1. 將使用者的程式切割成各式各樣的單字/token • 2. 依照定義的語法規則(grammar)把單字/token變成有意義的一個語言單元。 • 3.把語言單元變成節點後生成抽象符號樹(abstract syntax tree, AST) • 4.從AST轉組合語言

  11. 為什麼需要Lex, 他是什麼 • 程式語言允許兩個不同token中間沒有空白 • 大多語言內建的Tokenizer都自動把空白作為切割點 • 沒有了Lex你必須親自撰寫程式讀取字元,比對字串,給出字串的意義。給出字串轉Token的方法

  12. 為什麼需要Lex, 他是什麼 Lex是一個Token辨識器

  13. Lecture 1. Lexical Recognition • 為什麼需要Lex, 他是什麼 • Lex程式的規則 • Regular Expression (regexp)

  14. Lex程式的規則 • regular_expression { C-program statements }

  15. Lex程式的規則 %{ #include “header.h” extern int yyval; %} %% [0-9]*\.[0-9]f { printf(“FLOAT\n”); } [0-9]*\.[0-9] { printf(“DOUBLE\n”); } \”.*\” { printf(“STRING\n”); } \’.\’ { printf(“CHAR\n”); } [_a-zA-Z$] [_0-9a-zA-Z$]* { printf(“IDENT”\n”); } %% int yywrap(void) {return 1; } int main(){ yylex(); return 0;};

  16. Lecture 1. Lexical Recognition • 為什麼需要Lex, 他是什麼 • Lex程式的規則 • Regular Expression (regexp)

  17. Regular Expression (regexp) • 正規表示式用來表達一段文字的規則 • Lex用文字規則來辨識字串的詞彙

  18. Regular Expression (regexp) • 重複規則 • 某某規則+ 某某規則出現1次或以上 • 某某規則* 某某規則出現0次或以上 • 某某規則{次數} 某某規則出現幾次 • Ex: a+ (a,aa,aaa) • a* (,a,aa,aaa) • (abc)+ (abc,abcabc,abcabcabc) • 任意字與跳脫字、集合 • . 任何字元 • \. 一點 • [] 字組規則,[]集合內任何字 • () 連續出現的集合 • | 或(左邊或右邊出現就視為符合)

  19. 如何安裝與編譯 • 安裝-Ubuntu平台: • sudo apt-get install flex • 編譯: • lex lex文字檔 • lex -o 輸出檔名 lex文字檔 • 輸出檔為.yy.c,預設檔名為lex.yy.c • 直接用gcc編譯lex.yy.c

  20. 小作業 • 製作一個辨識使用者輸入字串的資料型別的程式

More Related