Left associativity of operators yields 3, while right associativity yields 7. How do the grammar rules express
associativity?
A production like this is left-associative:
expression -> term | expression add_op term
A production like this is right-associative:
expression -> term | term add_op expression
The significant difference is that the recursion (where an expression is part of an expression) is on the left
in the first case, and on the right in the second case.
Using the left-associative production to parse (9 - 4 - 2) results in this parse tree:
( 9 - 4 - 2 ) expression
/ | \
/ | \
(9 - 4) - 2 expression add_op term
CHAP. 4] SOFTWARE 63
Using the right-associative production to parse the same expression results in this tree:
( 9 - 4 - 2 ) expression
/ | \
/ | \
9 - (4 - 2) term add_op expression
The result is 3 in the left-associative grammar, and 7 in the right-associative grammar.
SUMMARY
The machine instruction sets themselves constituted the first generation programming languages.
Programs were conceived as sequences of machine operations, and programmers worked directly with the
hardware, often entering code in ones and zeros directly through the front panel switches. Assembly
languages, using mnemonic character strings to represent machine instructions, made up the second
generation of programming languages.
Pages:
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182