⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ss7

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
字号:
.SHSection 7. Hints for Preparing Specifications.PPThis section contains miscellaneous hints on preparing efficient, easy to change,and clear specifications.The individual subsections are, more or less,independent; the reader seeing Yacc for the firsttime may well find thatthis entire section could be omitted..SHInput Style.PPIt is difficult toinput rules with substantial actionsand still have a readable specification file.The following style hints owe much to Brian Kernighan,and are officially endorsed by the author..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.Indent rule bodies by one tab stop, and action bodies by two tab 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..SHCommon Actions.PPWhen several grammar rules have the same action, the user might well wish toprovide only one code sequence.A simple, general mechanism is, of course, to use subroutine calls.It is also possible to put a label on the first statement of an action,and let other actions be simply a goto to thislabel.Thus, if the user had a routine which built trees,he might wish to have only one call to it, as follows:.DSexpr :	expr \'+\' expr =	{  binary:		$$ = btree( $1, $2, $3 );	} |	expr \'\-\' expr =	{		goto binary;	} |	expr \'*\' expr =	{		goto binary;	} ;.DE.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.DSsequence :	item |	sequence item ;.DENotice that, in 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..PPIf the user were to write these rules right recursively, such as.DSsequence :	item |	item sequence ;.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..PPThe user should also consider whether a sequence with zeroelements has any meaning, and if so, consider writingthe sequence specification with an empty rule:.DSsequence :	| /* empty */	sequence 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.Experience suggests that permitting empty sequencesleads to increased generality, which frequently is not evident at thetime the rule is first written.There are cases, however, when the Yacc algorithm can fail whensuch a change is made.In effect, conflicts might arise when Yacc is asked to decidewhich empty sequence it has seen, when it hasn\'t seen enough toknow!Nevertheless,this principle is still worth following wherever possible..SHLexical Tie-ins.PPFrequently, there are lexical decisions which depend on thepresence of various constructions in the specification.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 these situations isto create a global flag which isexamined by the lexical analyzer, and set by actions.For example, consider a situation where we have a program whichconsists of 0 or more declarations, followed by 0 or more statements.We declare a flag called ``dflag'', which is 1 during declarations, and 0 duringstatements.We may do this as follows:.DS%{	int dflag ;%}%%program :	decls  stats ;decls :	= /* empty */	{		dflag = 1;	} |	decls declaration ;stats :	= /* empty */	{		dflag = 0;	} |	stats statement ;	. . .  other rules . . ..DEThe flag dflag is now set to zero 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.Frequently, however, this single token exception does notaffect the lexical scan required..PPClearly, this kind of ``backdoor'' approach can be elaborated onto a noxious degree.Nevertheless, it represents a way of doing some thingsthat are difficult, if not impossible, todo otherwise..SHBundling.PPBundling is a technique for collecting together various character stringsso that they can be output at some later time.It is derived from a feature of the same name in the compiler/compiler TMG [6]..PPBundling has two components \- a nice user interface,and a clever implementation trick.They will be discussed in that order..PPThe user interface consists of two routines, ``bundle'' and ``bprint''..DSbundle( a1, a2, . . ., an ).DEaccepts a variable number of arguments which are either character strings or bundles, andreturns a bundle,whose value will be the concatenation of the values of a1, . . ., an..DSbprint( b ).DEaccepts a bundle as argument and outputs its value..PPFor example, suppose that we wish to read arithmetic expressions, and outputfunction calls to routines called ``add'', ``sub'',``mul'', ``div'', and ``assign''.Thus, we wish to translate.DSa = b \- c*d.DEinto.DSassign(a,sub(b,mul(c,d))).DE.PPA Yacc specification file which does this is given in Appendix D; this includesan implementation of the bundle and bprintroutines.A rule and action of the form.DSexpr:	expr \'+\' expr =	{		$$ = bundle( "add(", $1, ",", $3, ")" );	}.DEcauses the returned value ofexpr to be come a bundle, whose value is thecharacter string containing the desired function call.Each NAME token has a value which is a pointer to theactual name which has been read.Finally, when the entire input line has been readand the value has been bundled,the value is written outand the bundles and namesare cleared, in preparation for the next input line..PPBundles are implemented as arrays of pointers, terminated by a zero pointer.Each pointer either points to a bundle or to a character string.There is an array, called.ulbundle space,which contains all the bundles..PPThe implementation trick is to check the values of the pointers in bundles \-if the pointer points into bundle space, it is assumed to point to a bundle;otherwise it is assumed to point to a character string..PPThe treatment of functions with a variable number of arguments, like bundle,is likely to differ from one implementation of C to another..PPIn general, one may wish to have a simple storage allocator whichallocates and frees bundles,in order to handle situations where it is not appropriate to completelyclear all of bundle space at one time..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,since it is difficult to pass the required information to the lexical analyzerwhich tells 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, and onewill probably be supported eventually.Until this day comes, I suggest that the keywords be.ulreserved;that is, be forbidden for use as variable names.There are powerful stylistic reasons for preferring this, anyway(he said weakly . . . )..SHNon-integer Values.PPFrequently, the user wishes to have values which arebigger than integers;again, this is an area where Yacc does not make the job as easy as it might,and some additional support is likely.Nevertheless, at the cost of writing a storage manager,the user can return pointers or indices to blocks of storagebig enough to contain the full values desired..SHPrevious Work.PPThere have been many previous applications of Yacc.The user who is contemplating a big application might wellfind that others have developed relevant techniques,or even portions of grammars.Yacc specifications appear to be easier to change thanthe equivalent computer programs, so that the ``prior art'' is morerelevant here as well.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -