词条 | Terminal and nonterminal symbols |
释义 |
In computer science, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a formal grammar. Terminal symbols are the elementary symbols of the language defined by a formal grammar. Nonterminal symbols (or syntactic variables) are replaced by groups of terminal symbols according to the production rules. The terminals and nonterminals of a particular grammar are two disjoint sets. Terminal symbolsTerminal symbols are literal symbols which may appear in the outputs of the production rules of a formal grammar and which cannot be changed using the rules of the grammar. Applying the rules recursively to a source string of symbols will usually terminate in a final output string consisting only of terminal symbols. Consider a grammar defined by two rules. Using pictoric marks interacting with each other:
Here Nonterminal symbolsNonterminal symbols are those symbols which can be replaced. They may also be called simply syntactic variables. A formal grammar includes a start symbol, a designated member of the set of nonterminals from which all the strings in the language may be derived by successive applications of the production rules. In fact, the language defined by a grammar is precisely the set of terminal strings that can be so derived. Context-free grammars are those grammars in which the left-hand side of each production rule consists of only a single nonterminal symbol. This restriction is non-trivial; not all languages can be generated by context-free grammars. Those that can are called context-free languages. These are exactly the languages that can be recognized by a non-deterministic push down automaton. Context-free languages are the theoretical basis for the syntax of most programming languages. Production rulesA grammar is defined by production rules (or just 'productions') that specify which symbols may replace which other symbols; these rules may be used to generate strings, or to parse them. Each such rule has a head, or left-hand side, which consists of the string that may be replaced, and a body, or right-hand side, which consists of a string that may replace it. Rules are often written in the form head → body; e.g., the rule a → b specifies that a can be replaced by b. In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s,[1][2] a grammar G consists of the following components:
where is the Kleene star operator and denotes set union, so represents zero or more symbols, and means one nonterminal symbol. That is, each production rule maps from one string of symbols to another, where the first string contains at least one nonterminal symbol. In the case that the body consists solely of the empty string—i.e., that it contains no symbols at all—it may be denoted with a special notation (often , or ) in order to avoid confusion.
A grammar is formally defined as the ordered quadruple . Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature.[3][4] ExampleFor instance, the following represents an integer (which may be signed) expressed in a variant of Backus–Naur form: In this example, the symbols (-,0,1,2,3,4,5,6,7,8,9) are terminal symbols and Another example is: S -> cAd A -> a | ab In this example, the symbols a,b,c,d are terminal symbols and S,A are nonterminal symbols. See also
References1. ^{{Cite journal | author = Chomsky, Noam | title = Three Models for the Description of Language | journal = IRE Transactions on Information Theory | volume = 2 | issue = 2 | pages = 113–123 | year = 1956 | doi = 10.1109/TIT.1956.1056813}} 2. ^{{Cite book | author = Chomsky, Noam | title = Syntactic Structures | publisher = Mouton | location = The Hague | year = 1957}} 3. ^{{cite book|first=Seymour|last=Ginsburg|authorlink=Seymour Ginsburg|title=Algebraic and automata theoretic properties of formal languages|publisher=North-Holland|pages=8–9|year=1975|isbn=0-7204-2506-9}} 4. ^{{cite book|last=Harrison|first=Michael A.|authorlink=Michael A. Harrison|title=Introduction to Formal Language Theory|year=1978|pages=13|isbn=0-201-02955-3|location=Reading, Mass.|publisher=Addison-Wesley Publishing Company}} 2 : Formal languages|Pattern matching |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。