Initial Commit
[packages] / xemacs-packages / semantic / doc / overview.texi
1 @ignore
2 @node Overview
3 @chapter Overview
4 @c This 'ignore' section fools texinfo-all-menus-update into creating
5 @c proper menus for this chapter.
6 @end ignore
7
8 This chapter gives the overview of @semantic{} and its goals.
9
10 With Emacs, regular expressions (and syntax tables) are the basis of
11 identifying components in a programming language source for purposes
12 such as color highlighting.  This approach has proved is usefulness,
13 but have limitations.
14
15 @semantic{} provides a new intrastructure that goes far beyond text
16 analysis based on regular expressions.
17
18 @semantic{} uses @dfn{parsers} to analyze programming language
19 sources.  For languages that can be described using a context-free
20 grammar, parsers can be based on the grammar of the language.  Or they
21 can be @dfn{external parsers} implemented using any means.  This
22 allows the use of a regular expression parser for non-regular
23 languages, or external programs for speed.
24
25 @semantic{} provides extensive tools to help support a new language.
26 An original @acronym{LL} parser, and a Bison-like @acronym{LALR}
27 parser are included.  So, for a regular language, all that the
28 developer needs to do is write a grammar file along with appropriate
29 semantic rules.
30
31 @semantic{} allows an uniform representation of language components,
32 and provides a common @acronym{API} so that programmers can develop
33 applications that work for all languages.  The distribution includes
34 good set of tools and examples for the application writers, that
35 demonstrate the usefulness of @semantic{}.
36
37 The following diagram illustrates the benefits of using @semantic{}:
38
39 @table @strong
40 @item Please Note:
41 The words in all-capital are those that @semantic{} itself provides.
42 Others are current or future languages or applications that are not
43 distributed along with @semantic{}.
44 @end table
45
46 @example
47                                                              Applications
48                                                                  and
49                                                               Utilities
50                                                                 -------
51                                                                /       \
52                +---------------+    +--------+    +--------+
53          C --->| C      PARSER |--->|        |    |        |
54                +---------------+    |        |    |        |
55                +---------------+    | COMMON |    | COMMON |<--- SPEEDBAR
56       Java --->| JAVA   PARSER |--->|        |    |        |
57                +---------------+    | PARSE  |    | PARSE  |<--- SENATOR
58                +---------------+    |        |    |        |
59     Python --->| PYTHON PARSER |--->| TREE   |    | TREE   |<--- DOCUMENT
60                +---------------+    |        |    |        |
61                +---------------+    | FORMAT |    | API    |<--- SEMANTICDB
62     Scheme --->| SCHEME PARSER |--->|        |    |        |
63                +---------------+    |        |    |        |<--- jdee
64                +---------------+    |        |    |        |
65    Texinfo --->| TEXI.  PARSER |--->|        |    |        |<--- ecb
66                +---------------+    |        |    |        |
67
68                     ...                ...           ...         ...
69
70                +---------------+    |        |    |        |<--- app. 1
71    Lang. A --->| A      Parser |--->|        |    |        |
72                +---------------+    |        |    |        |<--- app. 2
73                +---------------+    |        |    |        |
74    Lang. B --->| B      Parser |--->|        |    |        |<--- app. 3
75                +---------------+    |        |    |        |
76
77                      ...        ...     ...          ...       ...
78
79                +---------------+    |        |    |        |
80    Lang. Y --->| Y      Parser |--->|        |    |        |<--- app. ?
81                +---------------+    |        |    |        |
82                +---------------+    |        |    |        |<--- app. ?
83    Lang. Z --->| Z      Parser |--->|        |    |        |
84                +---------------+    +--------+    +--------+
85 @end example
86
87 @ignore
88 This is from the Overview chapter of the original semantic.texi.
89
90 Semantic is a tool primarily for the Emacs-Lisp programmer.
91 However, it comes with ``applications'' that non-programmer might
92 find useful.
93 This chapter is mostly for the benefit of these non-programmers
94 as it gives brief descriptions of basic concepts such as
95 grammars, parsers, compiler-compilers, parse-tree, etc.
96
97 @cindex grammar
98 The grammar of a natural language defines rules by which valid phrases
99 and sentences can be composed using words, the fundamental units with
100 which all sentences are created.
101 @cindex context-free grammar
102 In a similar fashion, a ``context-free grammar'' defines the rules by which
103 programs can be composed using the fundamental units of the language,
104 i.e., numbers, symbols, punctuations, etc.
105 Context-free grammars are often specified in a well-known form called
106 @cindex Backus-Naur Form
107 @cindex BNF
108 Backus-Naur Form, BNF for short.
109 This is a systematic way of representing context-free grammars
110 such that programs can read files with grammars written in BNF
111 and generate code for ``parser'' of that language.
112 @cindex yacc
113 @cindex compiler-compiler
114 YACC (Yet Another Compiler Compiler) is one such program that has been
115 part of UNIX operating systems since the 1970's.
116 YACC is pronounced the same as ``yak'', the long-haired ox found in Asia.
117 The parser generated by YACC is usually a C program.
118 @cindex bison
119 @uref{http://www.gnu.org/software/bison/bison.html , Bison}
120 is also a ``compiler compiler'' that takes BNF grammars and produces
121 parsers in C language.
122 The difference between YACC and Bison is that Bison is
123 @cindex free software
124 @uref{http://www.gnu.org/philosophy/free-sw.html , free software}
125 and upward-compatible with YACC.
126 It also comes with an excellent manual.
127
128 Semantic is similar in spirit to YACC and Bison.
129 @cindex bovinator
130 Semantic, however, is referred to as a @dfn{bovinator} rather than
131 as a parser, because it is a lesser cousin of YACC and Bison.
132 It is lesser in that it does not perform a full parse
133 like YACC or Bison.
134 @cindex bovination
135 Instead, it @dfn{bovinates}.
136 ``Bovination'' refers to partial parsing which
137 @cindex parse tree
138 creates @dfn{parse trees} of only the top most
139 expressions rather than parsing every nested expression.
140 This is sufficient for the purposes for which semantic was designed.
141 Semantic is meant to be used within Emacs for providing
142 editor-related features such as code browsers and translators rather
143 than for compiling which requires far more complex and complete parsers.
144 Semantic is not designed to be able to create full parse trees.
145
146 @cindex parser
147 One key benefit of semantic is that it creates parse trees
148 @cindex bovine tree
149 (perhaps the term @dfn{bovine tree} may be more accurate)
150 with the same structure regardless of the type of language involved.
151 Higher level applications written to work with bovine trees
152 will then work with any language for which the grammar is available.
153 For example, a code browser written today that supports C, C++, and
154 Java may work without any change on other languages that do not even
155 exist yet.
156 All one has to do is to write the BNF specification for the new language.
157 The rest of the work is done by semantic.
158 For certain languages, it is hard if not impossible to specify the syntax
159 of the language in BNF form, e.g.,
160 @uref{http://www.texinfo.org ,texinfo}
161 and other document oriented languages.
162 Semantic provides a parser for texinfo nevertheless.
163 Instead of BNF grammar, texinfo files are ``parsed'' using
164 @ref{Regexps,regular-expressions,regular-expressions,emacs}.
165
166 Semantic comes with grammars for these languages:
167
168 @itemize @bullet
169 @item C
170 @item Emacs-Lisp
171 @item java
172 @item makefile
173 @item scheme
174 @end itemize
175
176 Several tools employing semantic that provide user observable features
177 are listed in @ref{Tools} section.
178
179 @end ignore
180
181 @menu
182 * Semantic Components::         
183 @end menu
184
185 @node Semantic Components
186 @section Semantic Components
187
188 This chapter gives an overview of major components of @semantic{} and
189 how they interact with each other to perform its job.
190
191 The first step of parsing is to break up the input file into its
192 fundamental components.  This step is called lexical analysis.  The
193 output of the lexical analyzer is a list of tokens that make up the
194 file.
195
196 @example
197         syntax table, keywords list, and options
198                          |
199                          |
200                          v
201     input file  ---->  Lexer   ----> token stream
202 @end example
203
204 The next step is the parsing shown below.
205
206 @example
207                     parser tables
208                          |
209                          v
210     token stream --->  Parser  ----> parse tree
211 @end example
212
213 The end result, the parse tree, is created based on the parser tables,
214 which are in the internal representation of the language grammar used by
215 @semantic{}.
216
217 The @semantic{} database provides caching of the parse trees by saving them
218 into files named @file{semantic.cache} automatically when loading them
219 when appropriate instead of re-parsing.  The reason for this is to save the
220 time it takes to parse a file which could take several seconds or more
221 for large files.
222
223 Finally, @semantic{} provides an @acronym{API} for the Emacs Lisp
224 programmer to access the information in the parse tree.
225
226
227 @c  LocalWords:  API LALR