Java Reference
In-Depth Information
describedinChapters5and6;suchactions(e.g., shift and reduce for bottom-up
parsing) are concerned only with the grammar's terminal and nonterminal
symbols.
7.1.1 Semantic Actions and Values
To achieve syntax-directed translation, we insert code into the parser that
executes in concert with the parser's syntactic actions.
Semantic actions Each production can have an associated code sequence that
will execute when the production is applied. There is no imposed limit
onwhatthecodesequencecando,and the code is typically compiled
with the parser. Such code can therefore print messages, stop the parsing
activity, or manipulate the compiler's data structures.
Operations that execute in concert with productions are called semantic
actions , because they usually address compilation concerns beyond the
grammar's syntax that are related to the meaning of a program.
Semantic values When a semantic action is performed for the production
A
...X n , the semantic actions associated with the production are
given access to a set of semantic values related to the production, one
for each symbol. In a bottom-up parse, the semantic values for
→X
1
X 1
...X n
are available when A
→X 1 ...X n is applied, and the semantic actions
determine a value for A. In a top-down parse, a value for A is available
as the production is applied, and the symbols
X
...X n have values just
1
after the production is applied.
For terminal symbols, their values originate from the scanner. For exam-
ple, the syntactic token id has a specific value—the name of the associated
identifier—when a production involving id is applied. The associated
semantic actions can then reference the identifier's name, perhaps for the
purpose of generating code to load or store the value associated with the
identifier, or to enter the name in a symbol table.
For nonterminals, productions have already been applied to compute
their semantic values. The semantic action associated with a production
typically computes a value to be associated with A based on the values
already assigned to
X
...X n .
1
In automatically generatedparsers, the parser driver (Figure 6.3 onpage 185 for
bottom-up parsing) is usually responsible for executing the semantic actions.
To simplify calling conventions, the driver and the grammar's semantic actions
are written in the same programming language. Semantic actions are also
easily inserted into ad hoc parsers by specifying code sequences that execute
in concert with the parser.
 
 
Search WWH ::




Custom Search