Top Banner
( ייייי ייייייParsing ) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3
28

ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

(Parsingניתוח תחבירי )

Wilhelm, and Maurer – Chapter 8Aho, Sethi, and Ullman – Chapter 4Cooper and Torczon – Chapter 3

Page 2: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

scanner generatorsymbol specification

Input program

scanner

symbol string

scanning table

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

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

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

scannerscanning table

driver

Page 3: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

parse generatorgrammar

tokens

parser

syntax tree

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

scannersהמתאר – דומה לזה של יצירת הפורמליזם והשיטות – שונים

הפורמליזם – דקדוק חסר הקשרהאמצעי – אוטומט מחסנית סופי

parsing tables

driver

parserparsing tables

Page 4: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

טיפול בשגיאות – נושא חשוב

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

דרישותדיווח על שגיאות בצורה ברורה היחלצות מהירה משגיאות כך שאפשר יהיה לגלות שגיאות

המופיעות בהמשך-שימור יעילות הparser

Page 5: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

גישות לטיפול בשגיאות

panic mode השמטת קטעים מהקלט עד מציאת אות – המשמשת לסינכרוניזציה )למשל ";"(

phrase-level recoveryנסיונות תיקון מקומיים – ";" החלפת "," ב

error productionטיפול במצבים מיוחדים, בשיטות דקדוקיות -

global correction יקר, לא פרקטי –

Page 6: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דקדוק חסר הקשר

G=(V, T, P, S)V – nonterminalsמשתנים ,

T – terminals ,טרמינלים ,tokens

Pחוקי גזירה –

P = V (V U T)*

Sמשתנה תחילי –

S V

Page 7: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

סימון מלא

סימון מקוצר

V = {E, A}

T ={ ( , ) , - , id , +, - , , / , }

P =

{ E E A E

,A +,

E ( E ),A ‒,

E - E,A ,

E id,A /,A }

S =

E

E E A E | ( E ) | - E | id

A + | ‒ | | / |

Page 8: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דקדוק חסר הקשר - דוגמא

E → E A E | ( E ) | – E | idA → + | – | * | / | ^

Page 9: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

שפה חסרת הקשר

– סדרה של החלפות של אותיות לא טרמינליות תוך שימוש גזירהבחוקי הגזירה

– אוסף ביטויים הנגזרים מהמצב התחילי והמכילים טרמינלים שפהבלבד

EE A E

id A E

id + E

id + id

הגזירה סימון

Page 10: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

שפה חסרת הקשר

– סדרה של החלפות של אותיות לא טרמינליות תוך שימוש גזירהבחוקי הגזירה

– אוסף ביטויים הנגזרים מהמצב התחילי והמכילים טרמינלים שפהבלבד

– תוצאת סדרת גזירות בה (sentential formתבנית פסוקית )נותרו )אולי( לא-טרמינלים

E * id + E

– גזירה בה מוחלף בכל שלב הסימן השמאלי ביותר גזירה שמאלית)באופן דומה – גזירה ימנית(

שלבית רב גזירה

E

E EA

E

id

E A E

E

E A E

E

id +

E A E

E

id + id

Page 11: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

נתון דקדוקS → a A c B eA → A b | bB → d

a b b c d eקלט – a b b c d e

נבחר בשמאלית

a A b c d eנבחר בשמאלית

a A c d e

a A c B e

A A B אלטרנטיבות 3

A

B

S

A B אלטרנטיבות 3

Page 12: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

נתון דקדוקS → a A c B eA → A b | bB → d

a b b c d eקלט – a b b c d e

נבחר בשמאלית

a A b c d eנבחר בשמאלית

a A c d e

a A c B e

A A B אלטרנטיבות 3

A

B

S

A B אלטרנטיבות 3

Page 13: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

נתון דקדוקS → a A c B eA → A b | bB → d

a b b c d eקלט – a b b c d e

נבחר בשמאלית

a A b c d eנבחר בשמאלית

a A c d e

a A c B e

A A B אלטרנטיבות 3

A

B

S

A B אלטרנטיבות 3

Page 14: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

נתון דקדוקS → a A c B eA → A b | bB → d

a b b c d eקלט – a b b c d e

נבחר בשמאלית

a A b c d eנבחר בשמאלית

a A c d e

a A c B e

A A B אלטרנטיבות 3

A

B

S

A B אלטרנטיבות 3

Page 15: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

דוגמא

נתון דקדוקS → a A c B eA → A b | bB → d

a b b c d eקלט – a b b c d e

נבחר בשמאלית

a A b c d eנבחר בשמאלית

a A c d e

a A c B e

A A B אלטרנטיבות 3

A

B

S

A B אלטרנטיבות 3

Page 16: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

ביטוי רגולרי ודקדוק חסר הקשר

כל מבנה שניתן לבטא ע"י ביטוי רגולרי יכול להיות מתואר ע"י דקדוק

ההיפך לא נכון )למשל?(

אין "תורה מסיני" לגבי מה לשים במנתח הלקסיקלי ומה בדקדוק

, identifiersביטוים רגולרים שימושיים עבור תאור מבנים לקסיקלים כ- קבועים, מילות מפתח וכו'

דקדוקים שימושיים עבור מבנים מקוננים כסוגרים מאוזנים, התאמת begin-end'וכו ,

Page 17: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

סוגי הניתוח התחבירי

כל דקדוק חסר-הקשר שקול לאוטומט מחסנית. אז למה שלא נבנה אוטומט מחסנית ונשתמש בו בתור מנתח

תחבירי?

– לכל דקדוק נבדוק את כל Cocke-Younger-Kasamiכללי: – אינו מעשיO(n3)האפשרויות...

המטרהפענוח ובנית עץ הגזירה תוך מעבר בודד על הקלט, משמאל לימין בכל שלב, הקלטw מתחלק ל x y

החלק שטרם נקרא חלק הקלט שקראנו

Page 18: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

סוגי הניתוח התחבירי )המשך(

top-down – "מהשורש לעלים )נקרא גם – "ניתוח תחזית – predictive)

bottom-up מהעלים לשורש – מעבירים למחסנית, או מחליפים צד – (shift reduceימין בסימן מהצד השמאלי של חוק הדקדוק )

x y

s

x y

s

Page 19: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

מתרגם דקדוק באופן הבא:Recursive Descentאלגוריתם עבור כלnonterminal.מגדירים פונקציה המנתח מתחיל לפעול מהפונקציה המתאימה

הראשון.nonterminalל-:כל פונקציה מחקה את החוק שעבורו הוגדרה, באופן הבא

terminalמהקלט.המתאים אסימון מתורגם לקריאת ה nonterminal מתורגם להפעלת הפונקציה המתאימה

לו. אם ישנם כמה חוקי גזירה עבור אותוnonterminal ,

.lookaheadבוחרים ביניהם בעזרת

Recursive Descent בעזרת top-downניתוח

Page 20: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

matchפונקציית עזר:

void match(token t) {if (lookahead == t)

lookahead = next_token();else

error;}

פונקצייה זו משמשת כדי לקרוא טרמינלים.

בלבד.אסימון אחד הוא של lookaheadה-

Page 21: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

כתיבת פונקציות בהתאם לדקדוק

למשל, עבור הדקדוק:

E → LIT | ( E OP E ) | not ELIT → true | falseOP → and | or | xor

OP ו-E, LITנגדיר שלוש פונקציות:

Page 22: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

כתיבת פונקציות בהתאם לדקדוק

void E() {if (lookahead {TRUE, FALSE}) // E → LIT

LIT();else if (lookahead = LPAREN) // E → ( E OP E )

match(LPARENT); E(); OP(); E(); match(RPAREN);else if (lookahead = NOT) // E → not E

match(NOT); E();else

error;}

}

Page 23: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

כתיבת פונקציות בהתאם לדקדוק

void LIT() {if (lookahead = TRUE)

match(TRUE);else if (lookahead = FALSE)

match(FALSE);else

error;}

Page 24: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

כתיבת פונקציות בהתאם לדקדוק

void OP() {if (lookahead = AND)

match(AND);else if (lookahead = OR)

match(OR);else if (lookahead = XOR)

match(XOR);else

error;}

Page 25: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

איך מחליטים?

, יש לבחור nonterminalאם יש כמה כללים שונים לאותו ביניהם.

יחיד.אסימון של lookaheadבדוגמה האחרונה הסתפקנו ב-

פורמלית:

ם שהאסימונים – רשימת הFIRST(α), נגדיר: A→αעבור כלל באחת או יותר גזירות אפשריות הנובעות מכלל זה.יםראשונ

:Eבדוגמה שלנו, עבור הכללים ל-FIRST(LIT) = { true, false }FIRST( ( E OP E ) ) = { ( }FIRST(not E) = { not }

Page 26: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

איך מחליטים?

עבור הכללים FIRSTsאם אין חיתוך בין כל ה- נתון, אין כל בעיה.nonterminalהשונים ל-

אואם יש חיתוך... צריך לתקן את הדקדוק עמוק יותר.lookaheadלהשתמש ב-

Page 27: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

בעיות

, או כללים המתחילים εאיך מתגברים על כללי-?εב-

מה קורה עם רקורסיה שמאלית?

E → E A E | ( E ) | – E | idA → + | – | * | / | ^

Page 28: ניתוח תחבירי (Parsing) Wilhelm, and Maurer – Chapter 8 Aho, Sethi, and Ullman – Chapter 4 Cooper and Torczon – Chapter 3.

בעיות

, או כללים המתחילים εאיך מתגברים על כללי-?εב-

זו הופכת להיות ברירת המחדל.

מה קורה עם רקורסיה שמאלית?E → E A E | ( E ) | – E | idA → + | – | * | / | ^

מחליפים את הדקדוק. לכל דקדוק עם רקורסיה שמאלית יש דקדוק

מקביל בלעדיה.