/
Announcements Working in pairs is only allowed for programming assignments and not for Announcements Working in pairs is only allowed for programming assignments and not for

Announcements Working in pairs is only allowed for programming assignments and not for - PowerPoint Presentation

accouther
accouther . @accouther
Follow
349 views
Uploaded On 2020-06-23

Announcements Working in pairs is only allowed for programming assignments and not for - PPT Presentation

H3 has been posted 1 Syntax Directed Translation 2 CFGs so Far CFGs for Language Definition The CFGs weve discussed can generatedefine languages of valid strings So far we start by building a parse tree and ID: 784879

int trans term dlist trans int dlist term ast intnode factor expr decl tree type translation parse rules sdt

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Announcements Working in pairs is only a..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Announcements

Working in pairs is only allowed for programming assignments and not for homework problemsH3 has been posted

1

Slide2

Syntax Directed Translation

2

Slide3

CFGs so Far

CFGs for Language DefinitionThe CFGs we’ve discussed can generate/define languages of valid strings

So far, we

start

by building a parse tree and

end

with some valid string

CFGs for Language RecognitionStart with a string and end with a parse tree for it

3

Slide4

CFGs for Parsing

Language Recognition isn’t enough for a parserWe also want to translate the sequence

Parsing is a special case of

Syntax-Directed Translation

Translate a sequence of tokens into a sequence of actions

4

Slide5

Syntax Directed Translation

Augment CFG rules with translation rules (at least 1 per production)Define translation of LHS nonterminal as function ofConstants

RHS nonterminal translations

RHS terminal value

Assign rules bottom-up

5

Slide6

SDT Example

CFG

Rules

B ->

0

B

.trans

= 0 | 1

B

.trans

= 1

|

B

0

B

.trans = B2.trans * 2 | B 1 B.trans = B2.trans * 2 + 1

Input string10110

B

B

B

B

B

1

0

1

1

0

1

2

5

11

22

Translation is the value of the input

6

Slide7

SDT Example 2: Declarations

CFG

Rules

DList

ε

DList.trans = “”

|

DList

Decl

DList.trans

=

Decl.trans + “ “ + DList2.transDecl → Type id Decl.trans = id.

valueType → int

| bool

Input stringint xx;bool yy;

DList

DList

DList

Decl

Decl

“”

“xx ”

yy

xx ”

Translation is a String of ids

Type

id

ε

bool

Type

id

int

“xx”

yy

7

Slide8

Exercise Time

Only add declarations of type int to the output String.

Augment the previous grammar:

8

CFG

Rules

DList

ε

DList.

trans

= “”

|

DList

Decl

DList.trans

=

Decl.trans

+ “ “ + DList2.transDecl → Type

id ; Decl.trans = id

.valueType → int

| bool

Different nonterms canhave different types

Rules can have conditionals

Slide9

SDT Example 2b:

ints only

CFG

Rules

DList

→ ε DList.trans = “”

|

Decl

DList

DList.trans

=

Decl.trans + “ “ + DList2.transDecl → Type id ; if (Type.trans)

{Decl.trans = id.

value} else {Decl.trans = “”}Type →

int Type.trans = true

| bool Type.trans

= falseInput stringi

nt xx;bool yy;

DList

DList

DList

Decl

Decl

“”

“xx ”

“ xx ”

Translation is a String of

int

ids

only

Type

id

ε

bool

Type

id

int

“xx”

“”

9

false

true

Different

nonterms

can

h

ave different types

Rules can have conditionals

Slide10

SDT for Parsing

In the previous examples, the SDT process assigned different types to the translation:Example 1: tokenized stream to an integer valueExample 2: tokenized stream to a (java)

String

For parsing, we’ll go from tokens to an Abstract-Syntax Tree (AST)

10

Slide11

Abstract Syntax Trees

A condensed form of the parse treeOperators at internal nodes (not leaves)

Chains of productions are collapsed

Syntactic details omitted

11

+

i

ntlit

(2)

Expr

Term

Term

*

Factor

i

ntlit

(8)

Factor

Expr

Term

Factor

i

ntlit

(5)

(

Expr

)

Term

Factor

int

(

5)

add

int

(

2

)

mult

int

(8)

int

(

5)

add

int

(

2

)

mult

int

(8)

Parse Tree

Example: (5+2)*8

Slide12

Exercise #2

Show the AST for:(1 + 2) * (3 + 4) * 5 + 6

12

Expr -> Expr + Term

| Term

Term -> Term * Factor

|

Factor

Factor ->

intlit

|

( Expr )

Expr -> Expr + Term

Expr1

.trans = MkPlusNode(Expr2.trans, Term.trans)

Slide13

AST for Parsing

In previous slides we did our translation in two stepsStructure the stream of tokens into a parse tree

Use the parse tree to build an abstract syntax tree, throw away the parse tree

In practice, we will combine these into 1 step

Question:

Why do we even need an AST?

More of a “logical” view of the program

Generally easier to work with

13

Slide14

AST Implementation

How do we actually represent an AST in code?

14

Slide15

ASTs in Code

Note that we’ve assumed a field-like structure in our SDT actions:

DList.

trans

=

Decl.trans + “ “ + DList2

.trans

In our parser, we’ll define classes for each type of nonterminal, and create a new nonterminal in each rule.

In the above rule we might represent

DList

as

For ASTs: when we execute an SDT rule

we construct a new node object for the RHS

propagate its fields with the fields of the LHS nodes

15

public class

DList{ public String trans;

}

Slide16

Thinking about implementing ASTs

Consider the AST for a simple language of Expressions

16

Input

1 + 2

Tokenization

intlit

plus

intlit

Parse Tree

Expr

intlit

1

plus

AST

+

1

2

Naïve AST Implementation

c

lass

PlusNode

IntNode

left;

IntNode

right;

}

Expr

Term

Term

Factor

intlit

2

Factor

c

lass

IntNode

{

int

value;

}

Slide17

Thinking about implementing ASTs

Consider AST node classes We’d like the classes to have a common inheritance tree

17

AST

+

1

2

Naïve AST Implementation

c

lass

PlusNode

{

IntNode

left;

IntNode

right;

}

c

lass

IntNode

{

int

value;

}

PlusNode

IntNode

left: IntNode

right: Naïve java AST

IntNode

i

nt

value:

IntNode

int

value:

1

2

Slide18

Thinking about implementing ASTs

Consider AST node classes We’d like the classes to have a common inheritance tree

18

AST

+

1

2

Naïve AST Implementation

c

lass

PlusNode

{

IntNode

left;

IntNode

right;

}

c

lass

IntNode

{

int

value;

}

PlusNode

ExpNode

left: ExpNode right:

Better java AST

Make these extend

ExpNode

IntNode

int

value:

IntNode

int

value:

1

2

Slide19

Implementing ASTs for Expressions

19

PlusNode

ExpNode

left:

ExpNode

right:

IntNode

v

alue:

IntNode

v

alue:

1

2

CFG

Expr

->

Expr

+ Term

|

Term

Term -> Term * Factor

| Factor

Factor ->

intlit | ( Expr )

Example: 1 + 2

Expr

intlit

1

plus

Expr

Term

Term

Factor

intlit

2

Factor

Translation Rules

Expr1

.trans = new

PlusNode

(

Expr2

.trans,

T

erm

.trans

)

Expr

.trans

=

T

erm.trans

Term1

.trans = new

TimesNode

(Term2.trans,

F

actor

.trans

)

Term

.trans

=

F

actor

.trans

Factor

.trans

= new

I

ntNode

(

intlit

.value

)

Factor

.trans

=

E

xpr

.trans

Slide20

An AST for a code snippet

20

v

oid foo(

int

x,

int

y){

if (x == y){

return;

}

while ( x < y){

cout

<< “hello”;

x = x + 1;

}

}

FuncBody

if

while

return

==

x

y

return

<

x

y

print

“hello”

=

x

+

x

1

Slide21

Summary (1 of 2)

Today we learned aboutSyntax-Directed Translation (SDT)Consumes a parse tree with actions

Actions yield some result

Abstract Syntax Trees (ASTs)

The result of SDT for parsing in a compiler

Some practical examples of ASTs

21

Slide22

Summary (2 of 2)

22

Scanner

Language abstraction:

RegEx

Output: Token Stream

Tool:

JLex

Implementation: DFA walking via table

Parser

Language abstraction: CFG

Output: AST by way of Parse Tree

Tool: Java

CUP

Implementation: ???

Next time

Next week