Intro to Lex & Yacc [1]

 - by NAzT

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

lex &

หมายเหตุ ว่า

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

ซึ่ง

flex (Flex is a fast lexical analyser generator.) และ bison (Bison is a general-purpose parser 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

เรื่องที่เกี่ยวข้อง