Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax - - PowerPoint PPT Presentation

syntax directed analysis
SMART_READER_LITE
LIVE PREVIEW

Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax - - PowerPoint PPT Presentation

Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax Directed Analysis Syntax-Directed Definitions Generalizes a context-free grammar Each grammar symbol has an associated set of attributes Synthesized attributes


slide-1
SLIDE 1

Syntax Directed Analysis

Chapter 5

Compiler Construction Syntax Directed Analysis

1

slide-2
SLIDE 2

Syntax-Directed Definitions

  • Generalizes a context-free grammar
  • Each grammar symbol has an associated set of attributes

– Synthesized attributes – Inherited attributes

  • The node for a grammar symbol in a parse tree holds information

(attributes)

Compiler Construction Syntax Directed Analysis

2

slide-3
SLIDE 3

Syntax-directed Translation

  • Syntax-directed: Augment the productions of a CFG with semantic

rules or actions These rules and/or actions define the values of attributes and how to compute them

  • The augmented grammar is called an attribute grammar
  • Often a language specification provides the CFG for a language and an

English language description of the semantics A compiler writer must deduce an attribute grammar from the English description

Compiler Construction Syntax Directed Analysis

3

slide-4
SLIDE 4

Attributes

  • Value can be

– string – number – type – etc.

  • Values in parse tree node depend on the grammar production

responsible for that node

Compiler Construction Syntax Directed Analysis

4

slide-5
SLIDE 5

Example: Attributes of an Identifier

  • Name (lexeme from scanner)
  • Type

– Basic type: int, float, etc. – Array

∗ Dimension ∗ Type

– Structured type (struct, class):

∗ Field name ∗ Type of fi eld

– Procedure (function, method, etc.):

∗ Number and types of parameters ∗ Return type ∗ Size of stack frame

  • Scope

Compiler Construction Syntax Directed Analysis

5

slide-6
SLIDE 6

Synthesized vs. Inherited Attributes

  • Synthesized

– Computed from attributes of child nodes

  • Inherited

– Computed from attributes of sibling nodes and parent node

= position + initial * 60 rate

Compiler Construction Syntax Directed Analysis

6

slide-7
SLIDE 7

Semantic Rules

A semantic rule can be associated with a production: Production Semantic Rule

E → E1 +T E.val ← E1.val +T.val E → T E.val ← T.val T → T1 ∗F T.val ← T1.val ×F.val T → F T.val ← F.val F → (E) F.val ← E.val F → number F.val ← number.val

Compiler Construction Syntax Directed Analysis

7

slide-8
SLIDE 8

Example: Declarations

Productions:

decl → type id list; id list → id , id list | id type → τ1 | ... | τn

Semantic Rules:

decl → type id list; {id list.tval ← type.tval} id list → id , id list1 | id {id.tval ← id list.tval; id list1.tval ← id list.tval}

decl type id_list ; id id_list id_list τ id id id_list id

...

Compiler Construction Syntax Directed Analysis

8

slide-9
SLIDE 9

Dependencies

  • Semantic rules set up dependencies among attributes
  • These dependencies can be represented by a directed graph

+ initial * 60 position = rate

Compiler Construction Syntax Directed Analysis

9

slide-10
SLIDE 10

Annotated Parse Tree

  • Each node of the parse tree has attributes
  • Such a parse tree is said to be annotated or decorated

Compiler Construction Syntax Directed Analysis

10

slide-11
SLIDE 11

Syntax-Directed Definition

Each production

A → α

has an associated a set of semantic rules of the form

b = f(c1,c2,c3,...,ck)

Compiler Construction Syntax Directed Analysis

11

slide-12
SLIDE 12

Synthesized Attribute

In a production of the form

A → α

an associated semantic rule has the form

b = f(c1,c2,...,ck)

  • b is a synthesized attribute of A
  • c1,c2,...,ck are attributes belonging to the grammar symbols of the

production

Compiler Construction Syntax Directed Analysis

12

slide-13
SLIDE 13

Inherited Attribute

In a production of the form

A → α

an associated semantic rule has the form

b = f(c1,c2,...,ck)

  • b is an inherited attribute of one of the grammar symbols on the right

side of the production

  • c1,c2,...,ck are attributes belonging to the grammar symbols of the

production

Compiler Construction Syntax Directed Analysis

13

slide-14
SLIDE 14

Attributed Evaluation

  • Unrestricted definition

– No restrictions on attribute dependency – Perform a topological sort on the attribute dependency graph and evaluate in topological order – Expensive to evaluate

  • S-attributed definition

– Synthesized attributes only – Attributes may be evaluated bottom-up – Evaluated very efficiently

  • L-attributed definition

– Permits both synthesized and some inherited attributes Inherited attributes restricted to left siblings – Attributes may be evaluated depth-first, left to right

Any L-attributed definition may be converted to an S-attributed definition

Compiler Construction Syntax Directed Analysis

14

slide-15
SLIDE 15

S-attributed Definition

  • A syntax-directed definition that uses synthesized attributes exclusively
  • S stands for synthesized attributes
  • Attributes are computed from the bottom up
  • An LR parser can be adapted to implement an S-attributed definition
  • See example on Pages 281-282
  • Do a postorder traversal of the syntax tree to evaluate the attributes

Compiler Construction Syntax Directed Analysis

15

slide-16
SLIDE 16

S-attributed Evaluation

  • All semantic actions follow the right-hand side of a grammar rule
  • Yacc uses a value stack that parallel’s the LALR parser’s state stack

– Values of synthesized attributes are stored on the stack – For a reduction:

∗ Pop attributes of children off the stack ∗ Compute synthesized attributes for this rule ∗ Push computed synthesized attributes on to the stack

Compiler Construction Syntax Directed Analysis

16

slide-17
SLIDE 17

S-attributed Evaluation: Yacc Example

Expression: NUMBER { $$ = new NumberExpressionNode( yylval.token_info.lexeme, yylval.token_info.line_number); } | Name LPAREN ArgList RPAREN { $$ = new CallExpressionNode( (NameNode *) $1, (ArgListNode *) $3); } | Expression SumOp Expression %prec PLUS { $$ = new SumOpExpressionNode( (ExpressionNode *) $1, (SumOpNode *) $2, (ExpressionNode *) $3); } | Expression ProductOp Expression %prec TIMES { $$ = new ProductOpExpressionNode( (ExpressionNode *) $1, (ProductOpNode *) $2, (ExpressionNode *) $3); } ;

Compiler Construction Syntax Directed Analysis

17

slide-18
SLIDE 18

Inherited Attributes

  • Convenient for expressing the dependence of a programming language

construct on the context in which it appears

  • It is always possible to rewrite a syntax-directed definition to use only

synthesized attributes, but it is often more convenient to use inherited attributes

  • See example on Page 283

Compiler Construction Syntax Directed Analysis

18

slide-19
SLIDE 19

L-attributed Grammars

Given a production of the form

A → X1X2...Xn

an inherited attribute of Xj for 1 ≤ j ≤ n is a function of

  • 1. the attributes of the grammar symbols X1,X2,...,Xj−1 to the left of Xj and
  • 2. the inherited attributes of left side non-terminal A
  • S-attributed grammars are a subset of these
  • Semantic actions amy be placed anywhere on the right-hand side of a

production

  • Evaluate with a depth-first traversal

Compiler Construction Syntax Directed Analysis

19

slide-20
SLIDE 20

Code Generation from L-attributes

Grammar rule:

while stmt → while ( expr

) stmt

L-attributed grammar rule:

while stmt → while ( { fprintf(fout, "__while_top%u:\n", label++); } expr

)

{ fprintf(fout, "lw $a1 %s\n", temp);

fprintf(fout, "beqz $a1 __end_while%u\n", label); }

stmt { fprintf(fout, "j __while_top%u\n", label);

fprintf(fout, "__end_while%u:\n", label); }

Compiler Construction Syntax Directed Analysis

20

slide-21
SLIDE 21

Converting from L-attributed to S-attributed Definition

Recall that

  • In an S-attributed definition the semantic action(s) must appear after all

the grammar symbols on the right-hand side of a production

  • In an L-attributed definition the semantic action(s) may appear

anywhere on the right-hand side of a production

  • Replace a rule such as

A → B {print("Make A")} C

with two rules

A → B D C D → ǫ {print("Make A")}

Compiler Construction Syntax Directed Analysis

21

slide-22
SLIDE 22

Evaluation of Attributes

When you visit a node evaluate its inherited attributes preorder and its synthesized attributes postorder AttributeEvaluate(Node v) { for each child w of v from left to right do { Evaluate the inherited attributes of w; AttributeEvaluate(w);

}

Evaluate the synthesized attributes of v;

}

In general, inherited attributes are difficult to deal with, especially for bottom-up parsers

Compiler Construction Syntax Directed Analysis

22

slide-23
SLIDE 23

Intermediate Representations

  • Useful for syntax-directed translation
  • Decouples translation from parsing
  • Grammars often do not accurately reflect natural program structure
  • An abstract syntax tree (AST) is an example

Compiler Construction Syntax Directed Analysis

23

slide-24
SLIDE 24

Abstract Syntax Tree

A parse tree with non-essential information removed

E id ( ) + * * + E E E E E id id id id id

(a + b) * c Parse Tree AST

Compiler Construction Syntax Directed Analysis

24

slide-25
SLIDE 25

AST vs. Parse Tree

  • Operators are promoted from leaves to internal nodes
  • Chains of single productions are collapsed
  • Syntactic details like parentheses, semi-colons, and commas are
  • mitted
  • Subtree lists are flattened

Compiler Construction Syntax Directed Analysis

25

slide-26
SLIDE 26

AST Construction: Operator Promotion

Eliminate operator leaf nodes and promote them to interior nodes

E * * E E E E

Compiler Construction Syntax Directed Analysis

26

slide-27
SLIDE 27

AST Construction: Collapse Child Chains

Collapse chains of single children, replacing the chains with their leaf nodes

+ E id + E E E id id id

Compiler Construction Syntax Directed Analysis

27

slide-28
SLIDE 28

AST Construction: Remove Parsing Aids

Eliminate productions that aided syntactic parsing but which have no semantic meaning

+ E ( ) + a E E b a b

Compiler Construction Syntax Directed Analysis

28

slide-29
SLIDE 29

AST Construction: Flatten Subtrees

  • If useful, flatten subtrees that represent lists to a subtree with n children
  • Might not want to do this for statement list

arglist arg arg arg a b c arglist arg arglist arglist arglist arg arg ∋ a b c

  • Original production: arglist → arg∗
  • Modified production for Yacc: arglist → arglist arg | ǫ

Compiler Construction Syntax Directed Analysis

29

slide-30
SLIDE 30

AST Construction

The AST can be built from the parse tree or can be built directly by the parser

Compiler Construction Syntax Directed Analysis

30