

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Sohail Aslam Compiler Construction Notes Set:5-
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