📄 ss9
字号:
.SH9: Hints for Preparing Specifications.PPThis section contains miscellaneous hints on preparing efficient, easy to change,and clear specifications.The individual subsections are more or lessindependent..SHInput Style.PPIt is difficult toprovide rules with substantial actionsand still have a readable specification file.The following style hints owe much to Brian Kernighan..IP a.Use all capital letters for token names, all lower case letters fornonterminal names.This rule comes under the heading of ``knowing who to blame whenthings go wrong.''.IP b.Put grammar rules and actions on separate lines.This allows either to be changed withoutan automatic need to change the other..IP c.Put all rules with the same left hand side together.Put the left hand side in only once, and let allfollowing rules begin with a vertical bar..IP d.Put a semicolon only after the last rule with a given left hand side,and put the semicolon on a separate line.This allows new rules to be easily added..IP e.Indent rule bodies by two tab stops, and action bodies by threetab stops..PPThe example in Appendix A is written following this style, as arethe examples in the text of this paper (where space permits).The user must make up his own mind about these stylistic questions;the central problem, however, is to make the rules visible throughthe morass of action code..SHLeft Recursion.PPThe algorithm used by the Yacc parser encourages so called ``left recursive''grammar rules: rules of the form.DSname : name rest_of_rule ;.DEThese rules frequently arise whenwriting specifications of sequences and lists:.DSlist : item | list \',\' item ;.DEand.DSseq : item | seq item ;.DEIn each of these cases, the first rulewill be reduced for the first item only, and the second rulewill be reduced for the second and all succeeding items..PPWith right recursive rules, such as.DSseq : item | item seq ;.DEthe parser would be a bit bigger, and the items would be seen, and reduced,from right to left.More seriously, an internal stack in the parserwould be in danger of overflowing if a very long sequence were read.Thus, the user should use left recursion wherever reasonable..PPIt is worth considering whether a sequence with zeroelements has any meaning, and if so, consider writingthe sequence specification with an empty rule:.DSseq : /* empty */ | seq item ;.DEOnce again, the first rule would always be reduced exactly once, before thefirst item was read,and then the second rule would be reduced once for each item read.Permitting empty sequencesoften leads to increased generality.However, conflicts might arise if Yacc is asked to decidewhich empty sequence it has seen, when it hasn't seen enough toknow!.SHLexical Tie-ins.PPSome lexical decisions depend on context.For example, the lexical analyzer might want todelete blanks normally, but not within quoted strings.Or names might be entered into a symbol table in declarations,but not in expressions..PPOne way of handling this situation isto create a global flag that isexamined by the lexical analyzer, and set by actions.For example, suppose a programconsists of 0 or more declarations, followed by 0 or more statements.Consider:.DS%{ int dflag;%} ... other declarations ...%%prog : decls stats ;decls : /* empty */ { dflag = 1; } | decls declaration ;stats : /* empty */ { dflag = 0; } | stats statement ; ... other rules ....DEThe flag.I dflagis now 0 when reading statements, and 1 when reading declarations,.ulexcept for the first token in the first statement.This token must be seen by the parser before it can tell thatthe declaration section has ended and the statements havebegun.In many cases, this single token exception does notaffect the lexical scan..PPThis kind of ``backdoor'' approach can be elaboratedto a noxious degree.Nevertheless, it represents a way of doing some thingsthat are difficult, if not impossible, todo otherwise..SHReserved Words.PPSome programming languagespermit the user touse words like ``if'', which are normally reserved,as label or variable names, provided that such use does notconflict with the legal use of these names in the programming language.This is extremely hard to do in the framework of Yacc;it is difficult to pass information to the lexical analyzertelling it ``this instance of `if' is a keyword, and that instance is a variable''.The user can make a stab at it, using themechanism described in the last subsection,but it is difficult..PPA number of ways of making this easier are under advisement.Until then, it is better that the keywords be.I reserved \|;that is, be forbidden for use as variable names.There are powerful stylistic reasons for preferring this, anyway.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -