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

📄 ss0

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
字号:
.SHSection 0: Introduction.PPYacc provides a general tool for imposing structure on the input to a computer program.The Yacc user prepares aspecification of the input process; this includes ruleswhich describe the input structure, code which is to be invoked when thesestructures are recognized, and a low-level routine to do thebasic input.Yacc then produces a subroutine to do the input procedure;this subroutine, called a.Iparser,.Rcalls the user-supplied low-level input routine(called the.Ilexical analyzer).Rto pick up the basic items(called.Itokens).Rfrom the input stream.These tokens are organized according to the input structure rules,called.Igrammar rules;.Rwhen one of these rules has been recognized,then the user code supplied for this rule, called an.Iaction,.Ris invoked; actions have the ability to return values andmake use of the values of other actions..PPThe heart of the input specification is a collection of grammar rules.Each rule describes an allowable structure and gives it a name.For example, one grammar rule might be.DSdate  :  month\_name  day  \',\'  year   ;.DEHere, date, month\_name, day, and year represent structures of interest in the input process;presumably, month\_name, day, and year are defined elsewhere.The comma ``,'' is quoted by single quotes; this implies that thecomma is to appear literally in the input.The colon and semicolon merely serve as punctuation in the rule, and haveno significance in controlling the input.Thus, with proper definitions, the input.DSJuly  4, 1776.DEmight be matched by the above rule..PPAs we mentioned above, an important part of the input process is carried out by thelexical analyzer.This user routine reads the true input stream, recognizing thosestructures which are more conveniently or moreefficiently recognized directly, and communicates these recognized tokensto the parser.For historical reasons, the name of a structure recognized by the lexical analyzer is called a.Iterminal symbol.Rname, while the name of a structure recognized by the parser is called a.Inonterminal symbol.Rname.To avoid the obvious confusion of terminology, we shall usually refer toterminal symbol names as.Itoken names..R.PPThere is considerable leeway in deciding whether to recognize structures by the lexicalanalyzer or by a grammar rule.Thus, in the above example it would be possible to have other rules of the form.DSmonth\_name  :  \'J\' \'a\' \'n\'   ;month\_name  :  \'F\' \'e\' \'b\'   ;         . . .month\_name  :  \'D\' \'e\' \'c\'   ;.DEHere, the lexical analyzer would only need to recognize individual letters, andmonth\_name would be a nonterminal symbol.Rules of this sort tend to be a bit wasteful of time and space, and mayeven restrict the power of the input process(although they are easy to write).For a more efficient input process, the lexical analyzer itself mightrecognize the month names,and return an indication that amonth\_name was seen; in this case, month\_name would be a token..PPLiteral characters, such as ``,'', must also be passed through the lexicalanalyzer, and are considered tokens..PPAs an example of the flexibility of the grammar rule approach, we might add to the abovespecifications the rule.DSdate  :  month \'/\' day \'/\' year   ;.DEand thus optionally allow the form.DS7/4/1776.DEas a synonym for.DSJuly 4, 1776.DEIn most cases, this new rule could be ``slipped in'' to a working system with minimal effort,and a very small chance of disrupting existing input..PPFrequently, the input being read does not conform to thespecifications due to errors in theinput.The parsers produced by Yacc havethe very desirable property that they will detect theseinput errors at the earliestplace at which this can be done with aleft-to-right scan;thus, not only is the chance of reading and computing with badinput data substantially reduced, but the bad data can usually be quickly found.Error handling facilities,entered as part of the input specifications, frequentlypermit the reentry of bad data,or the continuation of the input process after skipping over the bad data..PPIn some cases, Yacc fails to produce a parser when given a set ofspecifications.For example, the specifications may be self contradictory, or they mayrequire a more powerful recognition mechanism than that available to Yacc.The former cases probably represent true design errors;the latter casescan often be correctedby makingthe lexical analyzermore powerful, or by rewriting some of the grammar rules.The class of specifications which Yacc can handle compares very favorablywith other systems of this type; moreover, theconstructions which are difficult for Yacc to handle arealso frequently difficult for human beings to handle.Some users have reported that the discipline of formulating validYacc specifications for their input revealed errors ofconception or design early in the program development..PPThe next several sections describe thebasic process of preparing a Yacc specification;Section 1 describes the preparation of grammar rules,Section 2 the preparation of the user supplied actions associated with these rules,and Section 3 the preparation of lexical analyzers.In Section 4, we discuss thediagnostics produced when Yaccis unable to produce a parserfrom the given specifications.This section also describes a simple, frequently useful mechanism forhandling operator precedences.Section 5 discusses error detection and recovery.Sections 6C and 6R discuss the operating environment and special featuresof the subroutines which Yacc produces in C and Ratfor, respectively.Section 7 gives some hints whichmay lead to better designed, more efficient,and clearer specifications.Finally, Section 8 has a brief summary.Appendix A has a brief example, and Appendix B tells how to run Yacc onthe UNIX operating system.Appendix C has a brief description of mechanisms and syntaxwhich are no longer actively supported, but whichare provided for historical continuity with older versions of Yacc.

⌨️ 快捷键说明

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