CS406: Compilers Spring 2020 Week 5: Parsers, AST, and Semantic - - PowerPoint PPT Presentation

cs406 compilers
SMART_READER_LITE
LIVE PREVIEW

CS406: Compilers Spring 2020 Week 5: Parsers, AST, and Semantic - - PowerPoint PPT Presentation

CS406: Compilers Spring 2020 Week 5: Parsers, AST, and Semantic Routines 1 Recap 2 3 Top-down Parsing predictive parsers 4 Suggested reading: https://en.wikipedia.org/wiki/LL_parser Top-down Parsing contd.. Also called


slide-1
SLIDE 1

1

CS406: Compilers

Spring 2020 Week 5: Parsers, AST, and Semantic Routines

slide-2
SLIDE 2

2

Recap

slide-3
SLIDE 3

3

slide-4
SLIDE 4

4

Suggested reading: https://en.wikipedia.org/wiki/LL_parser

Top-down Parsing – predictive parsers

slide-5
SLIDE 5

5

Top-down Parsing – contd..

  • Also called recursive-descent parsing
  • Equivalent to finding the left-derivation for an

input string

– Recall: expand the leftmost non-terminal in a parse tree – Expand the parse tree in pre-order i.e. identify parent nodes before children

slide-6
SLIDE 6

6

Top-down Parsing

1) S -> F 2) S -> (S + F) 3) F -> a

  • Table-driven (Parse Table) approach doesn’t require

backtracking string: (a+a)

( ) a + $ S 2

  • 1
  • F
  • 3
  • string’: (a+a)$

Assume that the table is given. But how do we construct such a table?

slide-7
SLIDE 7

7

slide-8
SLIDE 8

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

10

Computing Parse-Table

1) S -> ABc$ 2) A -> xaA 3) A -> yaA 4) A -> c 5) B -> b 6) B -> λ

x y a b c $ S 1 1 1 A 2 3 4 B 5 6

first (S) = {x, y, c} first (A) = {x, y, c} first(B) = {b, λ} follow (S) = {} follow (A) = {b, c} follow(B) = {c} P(1) = {x,y,c} P(2) = {x} P(3) = {y} P(4) = {c} P(5) = {b} P(6) = {c}

slide-11
SLIDE 11

11

Parsing using stack-based model (non-recursive) of a predictive parser

slide-12
SLIDE 12

12

Computing Parse-Table

string: xacc$ S xacc$ Predict(1) S->ABc$ ABc$ xacc$ Predict(2) A->xaA xaABc$ xacc$ match(x) aABc$ acc$ match(a) ABc$ cc$ Predict(4) A->c cBc$ cc$ match(c) Bc$ c$ Predict(6) B->λ c$ c$ match(c) c$ c$ Done! Stack* Remaining Input Action

* Stack top is on the left-side (first character) of the column

slide-13
SLIDE 13

13

Identifying LL(1) Grammar

  • What we saw was an example of LL(1) Parser
  • Not all Grammars are LL(1)

A Grammar is LL(1) iff for a production A -> α | β, where α and β are distinct:

1. For no terminal a do both α and β derive strings beginning with a 2. At most one of α and β can derive an empty string 3. If then α does not derive any string beginning with a terminal in Follow(A). If then does not derive any string beginning with a terminal in Follow(A)

slide-14
SLIDE 14

14

slide-15
SLIDE 15

15

Example (Left Factoring)

slide-16
SLIDE 16

16

A -> A α | β A -> βA’ A’ -> αA’ | λ

Eliminating Left Recursion

slide-17
SLIDE 17

17

slide-18
SLIDE 18

18

slide-19
SLIDE 19

19

slide-20
SLIDE 20

20

slide-21
SLIDE 21

21

slide-22
SLIDE 22

22

slide-23
SLIDE 23

23

Example:

E -> E + T | T T -> T * F | F F -> (E) | id

String: id*id

Demo

slide-24
SLIDE 24

24

  • Basic idea: put tokens on a stack until an entire production

is found

  • shift tokens onto the stack. At any step, keep the set of

productions that could generate the read-in token

  • reduce the RHS of recognized productions to the

corresponding non-terminal on the LHS of the production. Replace the RHS tokens on the stack with the LHS non- terminal.

  • Issues:
slide-25
SLIDE 25

25

slide-26
SLIDE 26

26

slide-27
SLIDE 27

27

slide-28
SLIDE 28

28

slide-29
SLIDE 29

29

slide-30
SLIDE 30

30

slide-31
SLIDE 31

31

Abstract Syntax Trees

  • Parsing recognizes a production from the

grammar based on a sequence of tokens received from Lexer

  • Rest of the compiler needs more info: a

structural representation of the program construct

  • Abstract Syntax Tree or AST
slide-32
SLIDE 32

32

Abstract Syntax Trees

  • Are like parse trees but ignore certain details
  • Example:

E -> E + E | (E) | int String: 1 + (2 + 3)

Demo

slide-33
SLIDE 33

33

Semantic Actions for Expressions

slide-34
SLIDE 34

34

Review

  • Scanners
  • Detect the presence of illegal tokens
  • Parsers
  • Detect an ill-formed program
  • Semantic actions
  • Last phase in the front-end of a compiler
  • Detect all other errors

What are these kind of errors?

slide-35
SLIDE 35

35

What we cannot express using CFGs

  • Examples:
  • Identifiers declared before their use (scope)
  • Types in an expression must be consistent
  • Number of formal and actual parameters of a

function must match

  • Reserved keywords cannot be used as identifiers
  • etc.

Depends on the language..

slide-36
SLIDE 36

36

abstract syntax tree

slide-37
SLIDE 37

37

Scope

  • Scope of an identifier is the part of the program

where the identifier is accessible

  • Multiple scopes for same identifier name

possible

  • Static vs. Dynamic scope

exercise: what are the different scopes in Micro?

slide-38
SLIDE 38

38

Types

  • Static vs. Dynamic
  • Type checking
  • Type inference
slide-39
SLIDE 39

39

in Λ

slide-40
SLIDE 40

40

Expressions Example

x + y + 5

slide-41
SLIDE 41

41

Expressions Example

x + y + 5

identifier “x”

slide-42
SLIDE 42

42

Expressions Example

x + y + 5

identifier “x” identifier “y”

slide-43
SLIDE 43

43

Expressions Example

x + y + 5

identifier “x” identifier “y” binary_op

  • perator: +
slide-44
SLIDE 44

44

Expressions Example

x + y + 5

identifier “x” identifier “y” binary_op

  • perator: +

literal “5”

slide-45
SLIDE 45

45

Expressions Example

x + y + 5

identifier “x” identifier “y” binary_op

  • perator: +

literal “5” binary_op

  • perator: +
slide-46
SLIDE 46

46

  • Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D.Ullman:

Compilers: Principles, Techniques, and Tools, 2/E, AddisonWesley 2007

– Chapter 4 (4.5, 4.6 (introduction)). Chapter 5 (5.3), Chapter 6 (6.1)

  • Fisher and LeBlanc: Crafting a Compiler with C

– Chapter 8 (Sections 8.1 to 8.3), Chapter 9 (9.1, 9.2.1 – 9.2.3)

Suggested Reading