Overview
Semantic is a tool primarily for the Emacs-Lisp programmer. However, it comes with "applications" that non-programmer might find useful. This chapter is mostly for the benefit of these non-programmers as it gives brief descriptions of basic concepts such as grammars, parsers, compiler-compilers, parse-tree, etc.
Semantic 是提供给Emacs-LispProgrammer的工具。然而,它也随着非程序员可能觉的有用的“应用”一同出现。这一章是针对这些非程序员的,它对一些基本的概念如语法,分析器,编译器-编译器,分析树等等给出了简要的描述
The grammar of a natural language defines rules by which valid phrases and sentences can be composed using words, the fundamental units with which all sentences are created. In a similar fashion, a "context-free grammar" defines the rules by which programs can be composed using the fundamental units of the language, i.e., numbers, symbols, punctuations, etc. Context-free grammars are often specified in a well-known form called Backus-Naur Form, BNF for short. This is a systematic way of representing context-free grammars such that programs can read files with grammars written in BNF and generate code for "parser" of that language. YACC (Yet Another Compiler Compiler) is one such program that has been part of UNIX operating systems since the 1970's. YACC is pronounced the same as "yak", the long-haired ox found in Asia. The parser generated by YACC is usually a C program. Bison is also a "compiler compiler" that takes BNF grammars and produces parsers in C language. The difference between YACC and Bison is that Bison is free software and upward-compatible with YACC. It also comes with an excellent manual.
自然语言的语法定义规则,从而能够使用单词组成有效的短语和句子,通过这些有效成分组成了所有的句子。与此类似,“context-free grammar”定义了规则,通过这些规则,程序可以有这门语言的基础单元构成,比如说,数字,标志符,标点等等。Context-free grammar通常使用一种著名的范式来定义,即Backus-Naur Form,简称BNF。这是一种系统的展现Context-Free grammar的途径,从而程序可以读取使用以BNF语法写成的文件并且为这门语言的分析器生成代码。YACC(Yet Another Compiler Compiler)就是一个这样的程序,自1970年代器,它便是Unix操作系统的一部分。Yacc发音为"yak",一种亚洲长发公牛。使用Yacc生成的分析器通常是一个C程序。Bison也是一个"compiler compiler"接受BNF语法,并且生成C语言写就的分析器。Yacc和Bison之间的区别在于,Bison是自由软件且对Yacc向上兼容。并且Bison伴有一份极佳的文档。
Semantic is similar in spirit to YACC and Bison. Semantic, however, is referred to as a bovinator rather than as a parser, because it is a lesser cousin of YACC and Bison. It is lesser in that it does not perform a full parse like YACC or Bison. Instead, it bovinates. "Bovination" refers to partial parsing which creates parse trees of only the top most expressions rather than parsing every nested expression. This is sufficient for the purposes for which semantic was designed. Semantic is meant to be used within Emacs for providing editor-related features such as code browsers and translators rather than for compiling which requires far more complex and complete parsers. Semantic is not designed to be able to create full parse trees.
Semantic与Yacc和Bison精神相似。但是Semantic更多的是被做为一个bovinator而不是parser, 因为他是Yacc和Bison的lesser cousion(远房?)。主要不同在于,它并不象Yacc和Bison那样完全的Parse。取而代之的,它进行Bovinate。“Bovination”是指一种部分parsing,它创建分析树,但这颗分析树仅包含最顶层的表达式而不去分析每个嵌套的表达式。对于Semantic的设计目的而言,这已经足够了。Semantic是用来在Emacs中提供编辑器相关的特征,比如说代码浏览器和翻译器,而不是为了编译,编译需要更为详细和复杂的parsers。Semantic并不是设计了用来生成完全的Parse Trees的
One key benefit of semantic is that it creates parse trees (perhaps the term bovine tree may be more accurate) with the same structure regardless of the type of language involved. Higher level applications written to work with bovine trees will then work with any language for which the grammar is available. For example, a code browser written today that supports C, C++, and Java may work without any change on other languages that do not even exist yet. All one has to do is to write the BNF specification for the new language. The rest of the work is done by semantic. For certain languages, it is hard if not impossible to specify the syntax of the language in BNF form, e.g., texinfo and other document oriented languages. Semantic provides a parser for texinfo nevertheless. Instead of BNF grammar, texinfo files are "parsed" using Regexps.
Semantic一个关键的好处在于,它创建分析树(可能使用bovine tree术语更为合适),这颗分析树有着同样的结构,而无论产生它的语言的类型是什么。更高层的程序,使用了bovine trees的就可以与所有语法可以得到提供的语言协同工作。比如,当前的一个代码浏览器,支持C,C++,和Java的,可能不需要做任何变化,即可支持当前尚未存在的语言。所有需要做的,就是写明这门新的语言的BNF specificaion。其余的工作都可以交给Semantic来做。对于特定的语言,如果不可能使用BNF范式来阐明其语法,比如说texinfo和其他面向文档的语言,工作就比较困难了。无论如何,Semantic提供了一个对texinfo的分析器。texinfo文件使用Regexps来Parse而不是使用BNF范式
Semantic comes with grammars for these languages:
C
Emacs-Lisp
java
makefile
scheme
Several tools employing semantic that provide user observable features are listed in Tools section.

