ניתוח לקסיקלי wilhelm, and maurer – chapter 7 aho, sethi, and ullman – chapter 3...

26
ייייי יייייייWilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

Post on 22-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

ניתוח לקסיקלי

Wilhelm, and Maurer – Chapter 7

Aho, Sethi, and Ullman – Chapter 3

Cooper and Torczon – Chapter 2

Page 2: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

front endשלב הניתוח - תזכורת –

תוכנית מקור

Back end

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

Page 3: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

פישוט הניתוח הלקסיקלי )והגדרת השפה(•מודולריות• שימוש חוזר•יעילות•

מדוע מנתח לקסיקלי?

Page 4: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

lexical analyzer + screenerניתוח לקסיקלי --

•lexeme נקרא לעיתים( לקסמה = סדרת אותיות = symbol)•token אסימון = עצם לשוני מוגדר; מכיל את סוג הלקסמה =

ותכונות שלה.דוגמא:•

idcounter name=“counter”

Page 5: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

תוכנית מקור

scanner

screener

parser

symbol

token

get next symbol

get next token

מנתח לקסיקלי – מבנה סכמתי )לוגי(

errormessagemanager

Page 6: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

scannerתפקידי ה-

קריאת הקלט•(symbolsהפרדת הקלט ליחידות לקסיקליות )= •דוגמאות•

screener ל-symbolsהעברת ה- •macros ו- include filesטיפול ב- •ספירת מספר שורות• לא חוקייםsymbolsדיווח על •

integer, realמשתניםbooleanהערות

סימנים מיוחדים )למשל )<=

string

Page 7: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

symbol classes

symbol classתוכנית המשתמש

IDfoo n_14 last

NUM73 00 517 082

REAL66.1. 5 10 .1e67 5.5e-10

IFif

COMMA,

NOTEQ=!

LPAREN(

RPAREN)

Page 8: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

non-symbols

תוכנית המשתמש

comment */ignored/*

preprocessor directive#include <foo.h<

macro#define NUMS 5, 6

white space\t \n

Page 9: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

– דוגמאותscannerתפקידי ה-

בשפת פסקל•const pi = 3.1416;

Fortranב-•do 5 I = 1.2

Fortranב-•do 5 I = 1 , 2

lookaheadכאן רואים את הצורך ב-

)אבל כיום לא עושים את זה כך...!( אינם נחשבים שברים, כי אחרת .10 ו- .Ada 1 ו- Pascalב-•

(scanner- )זה מקשה על ה10..1 לא ברור מהו

Page 10: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

scanner generatorsymbol specification

Input program

scanner

token stream

scanning table

: מפורמליזם לתוכנהscannerה-

יתרונות•המפרט – קצר יחסית–אפשר לוודא שהמפרט תקין )לא אוטומטית!(–התוכנה קלה לתחזוקה–

שימושים נוספים בקומפילציה ובשטחים generationלשיטת ה- •נוספים

scanner

scanning table

driver

Page 11: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

: פורמליזם scannerה-

- ביטויים רגולריים tokenתאור ה- • – אוטומט סופיscannerמבנה ה- •

הוא שלד האוטומט driverה-– היא טבלת המצביםscanning tableה- –

Page 12: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

רקע תיאורטי – מושגי יסוד

• Sאלפבית – S : S X: {1,…,n} מעל nמילה באורך •

X = X1 X2 . . . Xn

מילה ריקה: ••0 אוסף המילים באורך אפס – n אוסף המילים באורך – n* הסגור של( אוסף כל המילים –Kleene) + או יותר1 – אוסף המילים באורך •x.y מילה המהווה שרשור של – x -ו y בד"כ משמיטים את(

(x yאופרטור השרשור וכותבים •suffix, prefix, subword

Page 13: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

Sרקע תיאורטי – שפות מעל

* שפה – תת קבוצה של•

שפות L, L1, L2יהיו •

–L1 L2 איחוד שפות –

–L1 L2 = {x1 x2 | x1 L1, x2 L2 }שרשור שפות –

–L המשלים של – L -המילים ב(* -שאינן ב L)–Ln שרשור של – L לעצמו nפעמים –L* הסגור של – L שרשור – L לעצמו מספר כלשהו של פעמים

Page 14: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

ביטויים רגולריים, שפות רגולריות

הוא ביטוי רגולרי מעל המתאר את השפה הריקה הוא ביטוי רגולרי מעל המתאר את השפה }{ }a{ הוא ביטוי רגולרי המתאר את השפה a לכל • הם ביטויים רגולריים המתארים את השפות qו- p אם •

, אזי:Q ו- Pהרגולריות – p | qהוא ביטוי רגולרי המתאר את השפה הרגולרית P ∪ Q–( p q ) הוא ביטוי רגולרי המתאר את השפה P Q– (p *) הוא ביטוי רגולרי המתאר אתP*

הוא בעל הקדימות *הערה: על מנת לפשט את הסימונים, •הגבוהה ביותר, אח"כ שרשור, אח"כ סימן האלטרנטיבה

האותיות באדום הן מטה-אותיות•

Page 15: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

בעזרת מצייני קבוצות ותחומיםsymbolsתיאור

נעשה שימוש בשמות של קבוצות וסימנים•

דוגמא:letter = a-z A-Zdigit = 0-9id = letter ) letter | digit ( * number = digit digit * ) | . digit digit * ) | E digit digit ( (

Page 16: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

זיהוי שפות רגולריות – על ידי אוטומט סופי

= ) , Q , , q0 , F ( Mאוטומט – •

א"ב – • Qקבוצה סופית של מצבים –

•q0 Qמצב התחלתי –

•F Qקבוצת המצבים הסופיים – • Q ) \ { } ( Qיחס העברה

state

control

קלט

Page 17: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

מתיאור בעזרת מצייני קבוצות לאוטומט לא דטרמיניסטי

: הצב וקבל סדרת ביטויים רגולריים טהורים1שלב •

המזהה את Ai לכל Mi: בנה אוטומט לא דטרמיניסטי 2שלב •Riהביטוי הרגולרי

אוטומט זה מזהה את כל M: בנה אוטומט משולב 3שלב •הביטויים הרגולריים

Page 18: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

אוטומט דטרמיניסטי

הוא אוטומט דטרמיניסטי = ) , Q , , q0 , F ( Mאוטומט – •סופי אם

Q → Q : היא פונקציה חלקית כלומר אין מעברי – יש לכל היותר מעבר למצב יחידa ואות qלכל מצב –

, האוטומט יגיע למצב מסוים יחיד, אם בכללwלכל מילה •אנחנו נתעניין רק באוטומטים סופיים דטרמיניסטיים•משפט: לכל אוטומט לא דטרמיניסטי קיים אוטומט •

דטרמיניסטי שקולמשפט: לכל אוטומט סופי דטרמיניסטי קיים אוטומט סופי •

דטרמיניסטי שקול יחיד בעל מספר מצבים מינימאלי

Page 19: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2
Page 20: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2
Page 21: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2
Page 22: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2
Page 23: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

Lookahead

הבעיה: לעיתים צריך לסרוק מספר אותיות קדימה על מנת • עליו אנחנו מתכונניםsymbolלהחליט מהו ה-

/( המציינת את מקום תחילת הפתרון: אות מיוחדת )למשל -• בייצוג האוטומט, המוחלפת ב- lookaheadה-

יכולה להיות גם FORTRAN ב- IFדוגמא: המילה השמורה •שם של משתנה

אותיות2 של lookahead מספיק ADAבפסקל ו- הערה:•

0 1 2 3 4 5 6I F )(

startletter

any

Page 24: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

screenerה-

)scanner )LEXלעיתים – משולב כתוכנה "פתוחה" בתוך ה- • ואיסוף tokens – זיהוי ה- screenerתפקידו העיקרי של ה-•

עצמםlexemesאינפורמציה על ה-

Page 25: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

: תפקידים נוספיםscreenerה-

-- מילות מפתח שאינן שמורות, יחסית ל- keywordsטיפול ב- •reserved words

compiler directivesטיפול במקרים פשוטים של •הדפסת הפלט•

שילוב הודעות שגיאה–מקרו––pretty printing

)מעשית יש לעקוב אחרי מספרי השורות והעמודות בהם מופיעים ה- tokens)

Page 26: ניתוח לקסיקלי Wilhelm, and Maurer – Chapter 7 Aho, Sethi, and Ullman – Chapter 3 Cooper and Torczon – Chapter 2

סיכום

• For most programming languages lexical analyzers can be easily constructed

• Exceptions– Fortran– PL/1

• Lexical analyzer generator tools exist; they are used beyond compilers