Docsity

Evaluation Methods, Attributes - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:5-90
L
Le
ec
ct
tu
ur
re
e
3
32
2
Evaluation Methods
A number of ways can be used to evaluate the attributes. When using Dynamic method,
the compiler application builds the parse tree and then builds the dependence graph. A
topological sort of the graph is carried out and attributes are evaluated or defined in
topological order. In rule-based (or treewalk) methodology, the attribute rules are
analyzed at compiler-generation time. A fixed (static) ordering is determined and the
nodes in the dependency graph are evaluated this order. In oblivious (passes, dataflow)
methodology, the attribute rules and parse tree are ignored. A convenient order is picked
at compiler design time and used.
Attribute grammars have not achieved widespread use due to a number of problems. For
example: non-local computation, traversing parse tree, storage management for short-
lived attributes and lack of high-quality inexpensive tools. However, a variation of
attribute grammars and evaluation schemes is used in many compilers. This variation is
called ad-hoc analysis.
In rule-based evaluators, a sequence of actions is associated with grammar productions.
Organizing actions required for context-sensitive analysis around structure of the
grammar leads to powerful, albeit ad-hoc, approach which is used on most parsers. A
snippet of code (action) is associated with each production that executes at parse time
In top-down parsers, the snippet is added to the appropriate parsing routine. In a bottom-
up shift-reduce parsers, the actions are performed each time the parser performs a
reduction. Here the LR(1) skeleton parser indicating the place where the snippet is
executed.
stack.push(dummy); stack.push(0);
done = false; token = scanner.next();
while (!done) {
s = stack.top();
if( Action[s,token] == โ€œreduce Aโ†’ฮฒโ€) {
invoke the code snippet
stack.pop(2ร—|ฮฒ|);
s = stack.top();
stack.push(A);
stack.push(Goto[s,A]);
}
else if( Action[s,token] == โ€œshift iโ€){
stack.push(token); stack.push(i);
token = scanner.next();
}
}
pf2

Partial preview of the text

Download Evaluation Methods, Attributes - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:5-

Le Leccttuurree 3 322

Evaluation Methods

A number of ways can be used to evaluate the attributes. When using Dynamic method , the compiler application builds the parse tree and then builds the dependence graph. A topological sort of the graph is carried out and attributes are evaluated or defined in topological order. In rule-based (or treewalk) methodology, the attribute rules are analyzed at compiler-generation time. A fixed (static) ordering is determined and the nodes in the dependency graph are evaluated this order. In oblivious (passes, dataflow) methodology, the attribute rules and parse tree are ignored. A convenient order is picked at compiler design time and used.

Attribute grammars have not achieved widespread use due to a number of problems. For example: non-local computation, traversing parse tree, storage management for short- lived attributes and lack of high-quality inexpensive tools. However, a variation of attribute grammars and evaluation schemes is used in many compilers. This variation is called ad-hoc analysis.

In rule-based evaluators, a sequence of actions is associated with grammar productions. Organizing actions required for context-sensitive analysis around structure of the grammar leads to powerful, albeit ad-hoc, approach which is used on most parsers. A snippet of code ( action ) is associated with each production that executes at parse time In top-down parsers, the snippet is added to the appropriate parsing routine. In a bottom- up shift-reduce parsers, the actions are performed each time the parser performs a reduction. Here the LR(1) skeleton parser indicating the place where the snippet is executed.

stack.push(dummy); stack.push(0); done = false; token = scanner.next(); while (!done) { s = stack.top(); if( Action[s,token] == โ€œ reduce Aโ†’ฮฒโ€) { invoke the code snippet stack.pop(2ร—|ฮฒ|); s = stack.top(); stack.push(A); stack.push(Goto[s,A]); } else if( Action[s,token] == โ€œ shift iโ€){ stack.push(token); stack.push(i); token = scanner.next(); } }

Sohail Aslam Compiler Construction Notes Set:5-

The following table shows the code snippets for the SBN example.

Productions Code snippet Number โ†’ Sign List (^) Number.val โ† โ€“ Sign.val ร— List.val Sign โ†’ + Sign.val โ† 1 Sign โ†’ โ€“ Sign.val โ† โ€“ List โ†’ Bit List.val โ† Bit.val List 0 โ†’ List 1 Bit (^) List 0 .val โ† 2 ร—List 1 .val + Bit.val Bit โ†’ 0 Bit.val โ† 0 Bit โ†’ 1 Bit.val โ† 1