252-210: Compiler Design 3.2 Lexical analysis 3.3 - - PowerPoint PPT Presentation

252 210 compiler design 3 2 lexical analysis 3 3 top down
SMART_READER_LITE
LIVE PREVIEW

252-210: Compiler Design 3.2 Lexical analysis 3.3 - - PowerPoint PPT Presentation

252-210: Compiler Design 3.2 Lexical analysis 3.3 Top-down parsing Thomas R. Gross Computer Science Department ETH Zurich, Switzerland


slide-1
SLIDE 1

252-­‑210: ¡Compiler ¡Design ¡ ¡ 3.2 ¡ ¡Lexical ¡analysis ¡ 3.3 ¡ ¡Top-­‑down ¡parsing ¡

Thomas ¡R. ¡Gross ¡ ¡ Computer ¡Science ¡Department ¡ ETH ¡Zurich, ¡Switzerland ¡

slide-2
SLIDE 2

3.0 ¡Frontend ¡

§ 3.1 ¡IntroducFon ¡ § 3.2 ¡Lexical ¡analysis ¡ ¡ § 3.3 ¡“Top ¡down” ¡parsing ¡ § 3.4 ¡“BoNom ¡up” ¡parsing ¡

2 ¡

slide-3
SLIDE 3

Outline ¡

§ Using ¡mulFple ¡grammars ¡to ¡save ¡work ¡

§ Use ¡in ¡compiler ¡front ¡end ¡

§ Top-­‑down ¡parsing ¡

§ Simple ¡(backtracking) ¡parsers ¡ § Simple ¡(predic@ve) ¡parsers ¡ ¡

3 ¡

slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

Comments ¡and ¡whitespace ¡

§ Some ¡languages ¡aNach ¡meaning ¡to ¡whitespace ¡

§ Nes@ng ¡level ¡in ¡Python ¡ § “make” ¡u@lity ¡ § Extreme: ¡Whitespace ¡programming ¡language ¡

§ Only ¡space, ¡tab, ¡newline ¡ma\er ¡

§ Warning: ¡macro ¡facili@es, ¡pragma ¡

§ Not ¡all ¡comments ¡are ¡whitespace ¡

§ Direc@ves ¡hidden ¡in ¡comments ¡ § Example: ¡Fortran90 ¡comment ¡start ¡with ¡“!” ¡ !DEC$ IVDEP – ¡ignore ¡vector ¡dependencies DO I=1, N A(INDARR(I)) = A(INDARR(I)) + B(I) END DO

7 ¡

slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14

3.0 ¡Frontend ¡

§ 3.1 ¡IntroducFon ¡ § 3.2 ¡Lexical ¡analysis ¡ ¡ § 3.3 ¡“Top ¡down” ¡parsing ¡ § 3.4 ¡“BoNom ¡up” ¡parsing ¡

13 ¡

slide-15
SLIDE 15

3.3 ¡“Top ¡down” ¡parsing ¡

§ Given ¡w ¡∈ ¡T* ¡and ¡context ¡free ¡grammar ¡G ¡(S, ¡T, ¡NT, ¡P). ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Is ¡w ¡∈ ¡L(G)? ¡ § Top-­‑down: ¡find ¡a ¡derivaFon ¡S ¡⇒ ¡… ¡⇒ ¡w ¡

§ Want ¡to ¡find ¡a ¡lee-­‑most ¡deriva@on ¡ § Process ¡input ¡from ¡lee-­‑to-­‑right ¡

§ Languages ¡described ¡by ¡a ¡context-­‑free ¡grammar ¡can ¡be ¡ recognized ¡by ¡a ¡stack ¡machine. ¡

§ w ¡recognized ¡ ¡⇔ ¡w ¡∈ ¡L(G) ¡ § Get ¡deriva@on ¡for ¡free ¡(sequence ¡of ¡ac@ons ¡by ¡stack ¡machine) ¡

16 ¡

slide-16
SLIDE 16
slide-17
SLIDE 17

AcFons ¡

§ Error ¡ § Accept ¡ § Push ¡onto ¡stack ¡

§ Remove ¡from ¡input, ¡advance ¡read ¡pointer ¡

§ ReducFon ¡

§ Use ¡produc@on ¡to ¡expand/contract ¡the ¡top ¡of ¡the ¡stack ¡

19 ¡

slide-18
SLIDE 18

Parser ¡decisions ¡

§ Parser ¡must ¡decide ¡based ¡on ¡top ¡of ¡stack ¡and ¡current ¡input ¡ § Current ¡input ¡

§ Either ¡the ¡next ¡token ¡ § Some ¡number ¡k ¡of ¡remaining ¡tokens ¡

20 ¡

slide-19
SLIDE 19

Trivial ¡grammar ¡

S ¡ ¡Id ¡Op ¡Id ¡| ¡ ¡(1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡Id ¡ ¡ ¡ ¡ ¡ ¡ ¡(2) ¡ Op ¡ ¡+ ¡| ¡ ¡ ¡(3) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡| ¡ ¡ ¡(4) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡* ¡| ¡ ¡(5) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡(6) ¡ § Start ¡symbol ¡S ¡ § Terminals ¡: ¡{ ¡Id, ¡+, ¡-­‑, ¡*, ¡/ ¡} ¡ ¡ § Non-­‑terminals: ¡{S, ¡Op} ¡

21 ¡

slide-20
SLIDE 20
slide-21
SLIDE 21

Parser ¡decisions ¡

§ Parser ¡must ¡decide ¡based ¡on ¡top ¡of ¡stack ¡and ¡current ¡input ¡ § Current ¡input ¡

§ Either ¡the ¡next ¡token ¡ § Some ¡number ¡k ¡of ¡remaining ¡tokens ¡

§ How ¡can ¡control ¡the ¡parser? ¡

§ Must ¡be ¡sure ¡that ¡w ¡∉ ¡L(G) ¡if ¡we ¡say ¡there ¡is ¡no ¡deriva@on. ¡

23 ¡

slide-22
SLIDE 22

Grammars ¡& ¡words ¡

§ Words ¡are ¡finite ¡ § Grammars ¡are ¡finite ¡

§ Finite ¡alphabets ¡ § Finite ¡number ¡of ¡produc@ons ¡ § Try ¡un@l ¡you ¡succeed ¡ ¡

24 ¡

slide-23
SLIDE 23
slide-24
SLIDE 24

Backtracking ¡ ¡

§ Accept ¡if ¡stack ¡is ¡empty ¡and ¡all ¡input ¡ ¡consumed ¡ § Reject ¡if ¡there ¡are ¡no ¡more ¡choices ¡to ¡try ¡ § ImplementaFon ¡easy ¡ § May ¡not ¡be ¡efficient ¡– ¡but ¡fast ¡enough ¡in ¡some ¡serngs ¡

§ Can ¡be ¡used ¡for ¡any ¡grammar ¡

26 ¡

slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29
slide-30
SLIDE 30

§ Not ¡all ¡grammars ¡can ¡be ¡handled ¡with ¡k ¡ ¡symbols ¡of ¡ lookahead ¡

§ For ¡any ¡finite ¡(fixed) ¡k ¡

§ What ¡if ¡we ¡allow ¡infinite ¡lookahead? ¡

32 ¡

slide-31
SLIDE 31
slide-32
SLIDE 32
slide-33
SLIDE 33

3.3.3 ¡ConstrucFon ¡of ¡predicFve ¡parsers ¡

§ Top-­‑down ¡ § PredicFve: ¡ ¡for ¡any ¡combinaFon ¡of ¡(top-­‑of-­‑stack, ¡input) ¡ parser ¡knows ¡how ¡to ¡move ¡forward ¡

§ Towards ¡an ¡“accept” ¡or ¡“reject” ¡decision ¡

§ Look ¡again ¡at ¡stack ¡machine ¡

§ Prepara@on ¡for ¡Assignment ¡2 ¡ § Key ¡concepts ¡apply ¡to ¡bo\om-­‑up ¡parsing ¡

41 ¡

slide-34
SLIDE 34
slide-35
SLIDE 35

43 ¡

slide-36
SLIDE 36

Parsing ¡table ¡M ¡

§ Controls ¡operaFon ¡of ¡parsing ¡engine ¡ ¡ 1) ¡OperaFon ¡ 2) ¡How ¡to ¡set ¡M ¡ § Assume ¡context-­‑free ¡grammar ¡G ¡(S, ¡T, ¡NT, ¡P) ¡ § Add ¡$ ¡to ¡mark ¡boNom ¡of ¡stack, ¡end ¡of ¡input ¡ § Goal: ¡find ¡let-­‑most ¡derivaFon ¡

44 ¡

slide-37
SLIDE 37

Parsing ¡engine ¡

repeat ¡ ¡{ ¡

X ¡= ¡top ¡of ¡stack ¡ a: ¡terminal ¡pointed ¡to ¡by ¡ip ¡(input ¡pointer) ¡ if ¡( ¡X ¡∈ ¡T) ¡{ ¡ if ¡( ¡X ¡== ¡a ¡) ¡ ¡{ ¡pop ¡X; ¡ip++ ¡} ¡ else ¡error(); ¡ ¡} ¡ else ¡if ¡( ¡M[X, ¡a] ¡is ¡error-­‑entry) ¡error(); ¡ else ¡if ¡( ¡M=[X, ¡a] ¡= ¡X ¡à ¡Y1 ¡Y2 ¡… ¡Yn) ¡{ ¡ pop ¡stack ¡ push ¡Yn ¡… ¡Y2 ¡Y1 ¡onto ¡the ¡stack ¡ record ¡produc@on ¡ ¡X ¡à ¡Y1 ¡Y2 ¡… ¡Yn) ¡ } ¡

unFl ¡(X ¡== ¡$); ¡

45 ¡