18 ตุลาคม 2008Intro to Lex & Yacc [1]

มาทำ scanner ด้วย flex กัน !!

lex &

หมายเหตุ ว่า

ส่วนของ scanner (ทำ lexical analysis)  ผมจะใช้โปรแกรม flex หรือ lex
ส่วนของ เราใช้โปรแกรม bison

ซึ่ง

flex (Flex is a fast lexical analyser generator.) และ bison (Bison is a general-purpose generator) เป็นโปรแกรม บน unix based OS แต่บน windows  ก็มีให้ใช้ ลองไป search หา กันได้ครับ

วิธีการติดตั้งง่ายๆ บน (ubuntu)
พิมพ์ code ข้างล่างลงใน terminal (shell)
sudo apt-get install flex
sudo apt-get install bison

แล้วก็มาว่าด้วย flex กันต่อ

flex ก็เหมือน compiler ที่ compile code ภาษา c อะแหละ

แต่ว่า compiler ของภาษา c มันจะ compile ไฟล์ .c ให้เป็น ภาษาเครื่อง (.exe — executable file)

แต่ไอ้เจ้า flex เนี่ย จะ compile ไฟล์นามสกุล .l ให้เป็นไฟล์ภาษา c (ไฟล์ .c )

แล้วเราก็ต้องเอา ไฟล์ภาษา c ที่ได้มา compile ต่อเป็นภาษาเครื่อง

แล้วเราก็จะได้โปรแกรมที่รับ input แล้วเอาไปตัดเป็น tokens ตามที่เราตั้งกฎไว้

เดี๋ยวมาต่อตอน 2 นะครับนะ

Related Blogs


Tags: ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes