Java Reference
In-Depth Information
1 Start
Digs ans
$
call
print
( ans . val )
2 Digs up
Digs below d next
up . val below . val × below . base + next
up . base below . base
3
|
SetBase basespec
up . base basespec
up . val
0
4 Setbase n → λ
n
10
5
|
xd num
n num
x
d
d
d
d
$
(a)
(b)
Figure 7.8: (a) Grammar that avoids global variables; (b) Parse tree
reorganized to facilitate bottom-up attribute propagation.
7.2.4 Aggressive Grammar Restructuring
Global variables are easily introduced into semantic actions, but there are good
reasons to avoid using them:
A grammar's rules are often invoked recursively during parsing, and the
use of global variables can introduce unwanted interactions as semantic
actions are applied (see Exercise 12).
Global variables can make semantic actions di
cult to write and main-
tain, as any semantic action could read or write a global variable. More-
over, proper initialization and reinitialization of global variables can be
problematic.
Global variables may require setting or resetting
A more robust solution attempts to restructure the parse tree so that infor-
mation flows where it is needed for semantic actions. The following steps
are suggested as a mechanism for obtaining a grammar more appropriate for
bottom-up, syntax-directed translation:
1. Sketch the parse tree that would allow bottom-up synthesis and transla-
tion, without use of global variables.
 
Search WWH ::




Custom Search