Syntax Directed Analysis
Chapter 5
Compiler Construction Syntax Directed Analysis
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
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
– string – number – type – etc.
Compiler Construction Syntax Directed Analysis
– 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
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
– No restrictions on attribute dependency – Perform a topological sort on the attribute dependency graph and evaluate in topological order – Expensive to evaluate
– Synthesized attributes only – Attributes may be evaluated bottom-up – Evaluated very efficiently
– Permits both synthesized and some inherited attributes Inherited attributes restricted to left siblings – Attributes may be evaluated depth-first, left to right
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
– 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
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
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis
arglist arg arg arg a b c arglist arg arglist arglist arglist arg arg ∋ a b c
Compiler Construction Syntax Directed Analysis
Compiler Construction Syntax Directed Analysis