home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / yacc / ss4 < prev    next >
Encoding:
Text File  |  1975-06-26  |  14.5 KB  |  431 lines

  1. .SH
  2. Section 4: Ambiguity, Conflicts, and Precedence
  3. .PP
  4. A set of grammar rules is
  5. .ul
  6. ambiguous
  7. if there is some input string which can be structured in two or more different ways.
  8. For example, the grammar rule
  9. .DS
  10. expr :  expr \'\-\' expr ;
  11. .DE
  12. is a natural way of expressing the fact that one way of forming an arithmetic expression
  13. is to put two other expressions together with a minus sign between them.
  14. Unfortunately, this grammar rule does not
  15. completely specify the way that all complex inputs
  16. should be structured.
  17. For example, if we have input of the form
  18. .DS
  19. expr \- expr \- expr
  20. .DE
  21. the rule would permit us to treat this input either as
  22. .DS
  23. ( expr \- expr ) \- expr
  24. .DE
  25. or as
  26. .DS
  27. expr \- ( expr \- expr )
  28. .DE
  29. (We speak of the first as
  30. .ul
  31. left association
  32. of operators, and the second as
  33. .ul
  34. right association).
  35. .PP
  36. Yacc detects such ambiguities when it is attempting to build the parser.
  37. It is instructive to consider the problem that confronts the parser when it is
  38. given an input such as
  39. .DS
  40. expr \- expr \- expr
  41. .DE
  42. When the parser has read the second expr, the input which it has seen:
  43. .DS
  44. expr \- expr
  45. .DE
  46. matches the right side of the grammar rule above.
  47. One valid thing for the parser to do is to
  48. .ul
  49. reduce
  50. the input it has seen by applying this rule;
  51. after applying the rule, it would have reduced the input it had already seen to expr (the left side of the rule).
  52. It could then read the final part of the input:
  53. .DS
  54. \- expr
  55. .DE
  56. and again reduce by the rule.
  57. We see that the effect of this is to take the left associative interpretation.
  58. .PP
  59. Alternatively, when the parser has seen
  60. .DS
  61. expr \- expr
  62. .DE
  63. it could defer the immediate application of the rule, and continue reading
  64. (the technical term is
  65. .ul
  66. shifting)
  67. the input until it had seen
  68. .DS
  69. expr \- expr \- expr
  70. .DE
  71. It could then apply the grammar rule to the rightmost three symbols, reducing them to expr and leaving
  72. .DS
  73. expr \- expr
  74. .DE
  75. Now it can reduce by the rule again; the effect is to
  76. take the right associative interpretation.
  77. Thus, having read
  78. .DS
  79. expr \- expr
  80. .DE
  81. the parser can do two legal things, a shift or a reduction, and has no way of
  82. deciding between them.
  83. We refer to this as a
  84. .ul
  85. shift/reduce conflict.
  86. It may also happen that the parser has a choice of two legal reductions;
  87. this is called a
  88. .ul
  89. reduce/reduce conflict.
  90. .PP
  91. When there are shift/reduce or reduce/reduce conflicts, Yacc still produces a parser.
  92. It does this by selecting one of the valid steps wherever it has a choice.
  93. A rule which describes which choice to make in a given situation is called
  94. a
  95. .ul
  96. disambiguating rule.
  97. .PP
  98. Yacc has two disambiguating rules which are invoked by default,
  99. in the absence of any user directives to the contrary:
  100. .IP 1.
  101. In a shift/reduce conflict, the default is to do the shift.
  102. .IP 2.
  103. In a reduce/reduce conflict, the default is to reduce by the
  104. .ul
  105. earlier
  106. grammar rule (in the input sequence).
  107. .PP
  108. Rule 1 implies that reductions are deferred whenever there is a choice,
  109. in favor of shifts.
  110. Rule 2 gives the user rather crude control over the behavior of the parser
  111. in this situation, but the proper use of reduce/reduce conflicts is still a black art, and is
  112. properly considered an advanced topic.
  113. .PP
  114. Conflicts may arise because of mistakes in input or logic, or because the grammar rules, while consistent,
  115. require a more complex parser than Yacc can construct.
  116. In these cases, the application of disambiguating rules is inappropriate,
  117. and leads to a parser which is in error.
  118. For this reason, Yacc
  119. always reports the number of shift/reduce and reduce/reduce conflicts which were resolved by Rule 1 and Rule 2.
  120. .PP
  121. In general, whenever it is possible to apply disambiguating rules to produce a correct parser, it is also
  122. possible to rewrite the grammar rules so that the same inputs are read, but there are no
  123. conflicts.
  124. For this reason, most previous systems like Yacc
  125. have considered conflicts to be fatal errors.
  126. Our experience has suggested that this rewriting is somewhat unnatural to do,
  127. and produces slower parsers; thus, Yacc will produce parsers even in the presence of conflicts.
  128. .PP
  129. As an example of the power of disambiguating rules, consider a fragment from a programming
  130. language involving an
  131. ``if-then-else'' construction:
  132. .DS
  133. stat :    IF \'(\' cond \')\' stat |
  134.     IF \'(\' cond \')\' stat ELSE stat ;
  135. .DE
  136. Here, we consider IF and ELSE to be tokens, cond to be a nonterminal symbol describing
  137. conditional (logical) expressions, and
  138. stat to be a nonterminal symbol describing statements.
  139. In the following, we shall refer to these two rules as the
  140. .ul
  141. simple-if
  142. rule and the
  143. .ul
  144. if-else
  145. rule, respectively.
  146. .PP
  147. These two rules form an ambiguous construction, since input of the form
  148. .DS
  149. IF ( C1 ) IF ( C2 ) S1 ELSE S2
  150. .DE
  151. can be structured according to these rules in two ways:
  152. .DS
  153. IF ( C1 ) {
  154.     IF ( C2 ) S1
  155. }
  156. ELSE S2
  157. .DE
  158. or
  159. .DS
  160. IF ( C1 ) {
  161.     IF ( C2 ) S1
  162.     ELSE S2
  163. }
  164. .DE
  165. The second interpretation is the one given in most programming languages
  166. which have this construct.
  167. Each ELSE is associated with the last preceding ``un-ELSE'd'' IF.
  168. In this example, consider the situation where the parser has seen
  169. .DS
  170. IF ( C1 ) IF ( C2 ) S1
  171. .DE
  172. and is looking at the ELSE.
  173. It can immediately
  174. .ul
  175. reduce
  176. by the simple-if rule to get
  177. .DS
  178. IF ( C1 ) stat
  179. .DE
  180. and then read the remaining input,
  181. .DS
  182. ELSE S2
  183. .DE
  184. and reduce
  185. .DS
  186. IF ( C1 ) stat ELSE S2
  187. .DE
  188. by the if-else rule.
  189. This leads to the first of the above groupings of the input.
  190. .PP
  191. On the other hand, we may
  192. .ul
  193. shift
  194. the ELSE and read S2, and then reduce the right hand portion of
  195. .a
  196. .DS
  197. IF ( C1 ) IF ( C2 ) S1 ELSE S2
  198. .DE
  199. by the if-else rule to get
  200. .DS
  201. IF ( C1 ) stat
  202. .DE
  203. which can be reduced by the simple-if rule.
  204. This leads to the second of the above groupings of the input, which
  205. is usually desired.
  206. .PP
  207. Once again the parser can do two valid things \- we have a shift/reduce conflict.
  208. The application of disambiguating rule 1 tells the parser to shift in this case,
  209. which leads to the desired grouping.
  210. .PP
  211. Notice that this shift/reduce conflict arises only when there is a particular current input symbol,
  212. ELSE, and particular inputs already seen, such as
  213. .DS
  214. IF ( C1 ) IF ( C2 ) S1
  215. .DE
  216. In general, there may be many conflicts, and each one
  217. will be associated with an input symbol and
  218. a set of previously read inputs.
  219. The previously read inputs are characterized by the
  220. .ul
  221. state
  222. of the parser, which is assigned a nonnegative integer.
  223. The number of states in the parser is typically two to five times the number of grammar
  224. rules.
  225. .PP
  226. When Yacc is invoked with the verbose
  227. (\-v) option (see Appendix B), it produces a file of user output
  228. which includes a description of the states in the parser.
  229. For example, the output corresponding to the above
  230. example might be:
  231. .DS L
  232. 23: shift/reduce Conflict (Shift 45, Reduce 18) on ELSE
  233.  
  234. State 23
  235.  
  236.     stat : IF ( cond ) stat\_
  237.     stat : IF ( cond ) stat\_ELSE stat
  238.  
  239.     ELSE    shift 45
  240.     .        reduce 18
  241.  
  242. .DE
  243. The first line describes the conflict, giving the state and the input symbol.
  244. The state title follows, and a brief description of the grammar
  245. rules which are active in this state.
  246. The underline ``\_'' describes the
  247. portions of the grammar rules which have been seen.
  248. Thus in the example, in state 23 we have seen input which corresponds
  249. to
  250. .DS
  251. IF ( cond ) stat
  252. .DE
  253. and the two grammar rules shown are active at this time.
  254. The actions possible are, if the input symbol is ELSE, we may shift into state
  255. 45.
  256. In this state, we should find as part of the description a line of the form
  257. .DS
  258. stat : IF ( cond ) stat ELSE\_stat
  259. .DE
  260. because in this state we will have read and shifted the ELSE.
  261. Back in state 23, the alternative action, described by ``.'',
  262. is to be done if the input symbol is not mentioned explicitly in the above actions; thus,
  263. in this case, if the input symbol is not ELSE, we should reduce by grammar rule 18,
  264. which is presumably
  265. .DS
  266. stat : IF \'(\' cond \')\' stat
  267. .DE
  268. Notice that the numbers following ``shift'' commands refer to other states,
  269. while the numbers following ``reduce'' commands refer to grammar
  270. rule numbers.
  271. In most states, there will be only one reduce action possible in the
  272. state, and this will always be the default command.
  273. The user who encounters unexpected shift/reduce conflicts will probably want to
  274. look at the verbose output to decide whether the default actions are appropriate.
  275. In really tough cases, the user might need to know more about
  276. the behavior and construction of the parser than can be covered here;
  277. in this case, a reference such as [1]
  278. might be consulted; the services of a local guru might also be appropriate.
  279. .PP
  280. There is one common situation
  281. where the rules given above for resolving conflicts are not sufficient;
  282. this is in the area of arithmetic expressions.
  283. Most of the commonly used constructions for arithmetic expressions can be naturally
  284. described by the notion of
  285. .ul
  286. precedence
  287. levels for operators, together with information about left
  288. or right associativity.
  289. It turns out that ambiguous grammars with appropriate disambiguating rules
  290. can be used to create parsers which are faster and easier to
  291. write than parsers constructed from unambiguous grammars.
  292. The basic notion is to write grammar rules
  293. of the form
  294. .DS
  295. expr : expr OP expr
  296. .DE
  297. and
  298. .DS
  299. expr : UNARY expr
  300. .DE
  301. for all binary and unary operators desired.
  302. This creates a very ambiguous grammar, with many parsing conflicts.
  303. As disambiguating rules, the user specifies the precedence, or binding
  304. strength, of all the operators, and the associativity
  305. of the binary operators.
  306. This information is sufficient to allow Yacc to resolve the parsing conflicts
  307. in accordance with these rules, and construct a parser which realizes the desired
  308. precedences and associativities.
  309. .PP
  310. The precedences and associativities are attached to tokens in the declarations section.
  311. This is done by a series of lines beginning with a Yacc keyword: %left, %right,
  312. or %nonassoc, followed by a list of tokens.
  313. All of the tokens on the same line are assumed to have the same precedence level
  314. and associativity; the lines are listed in
  315. order of increasing precedence or binding strength.
  316. Thus,
  317. .DS
  318. %left \'+\' \'\-\'
  319. %left \'*\' \'/\'
  320. .DE
  321. describes the precedence and associativity of the four arithmetic operators.
  322. Plus and minus are left associative, and have lower precedence than
  323. star and slash, which are also left associative.
  324. The keyword %right is used to describe right associative operators,
  325. and the keyword %nonassoc is used to describe operators, like
  326. the operator .LT. in Fortran, which may not associate with themselves; thus,
  327. .DS
  328. A .LT. B .LT. C
  329. .DE
  330. is illegal in Fortran, and such an operator would be described with the keyword
  331. %nonassoc in Yacc.
  332. As an example of the behavior of these declarations, the description
  333. .DS
  334. %right \'=\'
  335. %left \'+\' \'\-\'
  336. %left \'*\' \'/\'
  337.  
  338. %%
  339.  
  340. expr :
  341.     expr \'=\' expr |
  342.     expr \'+\' expr |
  343.     expr \'\-\' expr |
  344.     expr \'*\' expr |
  345.     expr \'/\' expr |
  346.     NAME ;
  347. .DE
  348. might be used to structure the input
  349. .DS
  350. a = b = c*d \- e \- f*g
  351. .DE
  352. as follows:
  353. .DS
  354. a = ( b = ( ((c*d)\-e) \- (f*g) ) )
  355. .DE
  356. When this mechanism is used,
  357. unary operators must, in general, be given a precedence.
  358. An interesting situation arises when we have a unary operator and a binary operator
  359. which have the same symbolic representation, but different precedences.
  360. An example is unary and binary \'\-\'; frequently, unary minus is given the same
  361. strength as multiplication, or even higher, while binary minus has a lower strength than
  362. multiplication.
  363. We can indicate this situation by use of
  364. another keyword, %prec, to change the precedence level associated with a particular grammar rule.
  365. %prec appears immediately after the body of the grammar rule, before the action or closing semicolon,
  366. and is followed by a token name or literal; it
  367. causes the precedence of the grammar rule to become that of the token name or literal.
  368. Thus, to make unary minus have the same precedence as multiplication, we might write:
  369. .DS
  370. %left \'+\' \'\-\'
  371. %left \'*\' \'/\'
  372.  
  373. %%
  374.  
  375. expr :
  376.     expr \'+\' expr |
  377.     expr \'\-\' expr |
  378.     expr \'*\' expr |
  379.     expr \'/\' expr |
  380.     \'\-\' expr %prec \'*\' |
  381.     NAME ;
  382. .DE
  383. .PP
  384. Notice that the precedences which are described
  385. by %left, %right, and %nonassoc are independent of the declarations of token names
  386. by %token.
  387. A symbol can be declared by %token, and, later in the declarations section,
  388. be given a precedence and associativity by one of the above methods.
  389. It is true, however, that names which are given a precedence or associativity are
  390. also declared to be token names, and so in general do not need to be
  391. declared by %token, although it does not hurt to do so.
  392. .PP
  393. As we mentioned above, the precedences and associativities are used by Yacc to
  394. resolve parsing conflicts; they give rise to disambiguating rules.
  395. Formally, the rules work as follows:
  396. .IP 1.
  397. The precedences and associativities are recorded for those tokens and literals
  398. which have them.
  399. .IP 2.
  400. A precedence and associativity is associated with each grammar rule; it is the precedence
  401. and associativity of the last token or literal in the body of the rule.
  402. If the %prec construction is used, it overrides this default.
  403. Notice that some grammar rules may have no precedence and associativity associated with them.
  404. .IP 3.
  405. When there is a reduce/reduce conflict, or there is a shift/reduce conflict
  406. and either the input symbol or the grammar rule, or both, has no precedence and associativity
  407. associated with it, then the two disambiguating rules given at the beginning of the section are used,
  408. and the conflicts are reported.
  409. .IP 4.
  410. If there is a shift/reduce conflict, and both the grammar rule and the input character
  411. have precedence and associativity associated with them, then the conflict is resolved
  412. in favor of the action (shift or reduce) associated with the higher precedence.
  413. If the precedences are the same, then the associativity is used; left
  414. associative implies reduce, right associative implies shift, and nonassociating
  415. implies error.
  416. .PP
  417. There are a number of points worth making
  418. about this use of disambiguation.
  419. There is no reporting of conflicts which are resolved by this mechanism,
  420. and these conflicts are not counted in the number of shift/reduce and reduce/reduce
  421. conflicts found in the grammar.
  422. This means that occasionally mistakes in the specification of precedences
  423. disguise errors in the input grammar; it is a good idea to be sparing
  424. with precedences, and use them in an essentially ``cookbook'' fashion,
  425. until some experience has been gained.
  426. Frequently, not enough operators or precedences have been specified;
  427. this leads to a number of messages about shift/reduce or reduce/reduce conflicts.
  428. The cure is usually to specify more precedences, or use the %prec mechanism, or both.
  429. It is generally good to examine the verbose output file to ensure that
  430. the conflicts which are being reported can be validly resolved by precedence.
  431.