xref: /third_party/ltp/tools/sparse/sparse-src/README (revision f08c3bdf)
1f08c3bdfSopenharmony_ci
2f08c3bdfSopenharmony_ci  sparse (spärs), adj,., spars-er, spars-est.
3f08c3bdfSopenharmony_ci	1. thinly scattered or distributed; "a sparse population"
4f08c3bdfSopenharmony_ci	2. thin; not thick or dense: "sparse hair"
5f08c3bdfSopenharmony_ci	3. scanty; meager.
6f08c3bdfSopenharmony_ci	4. semantic parse
7f08c3bdfSopenharmony_ci  	[ from Latin: spars(us) scattered, past participle of
8f08c3bdfSopenharmony_ci	  spargere 'to sparge' ]
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci	Antonym: abundant
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ciSparse is a semantic parser of source files: it's neither a compiler
13f08c3bdfSopenharmony_ci(although it could be used as a front-end for one) nor is it a
14f08c3bdfSopenharmony_cipreprocessor (although it contains as a part of it a preprocessing
15f08c3bdfSopenharmony_ciphase). 
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ciIt is meant to be a small - and simple - library.  Scanty and meager,
18f08c3bdfSopenharmony_ciand partly because of that easy to use.  It has one mission in life:
19f08c3bdfSopenharmony_cicreate a semantic parse tree for some arbitrary user for further
20f08c3bdfSopenharmony_cianalysis.  It's not a tokenizer, nor is it some generic context-free
21f08c3bdfSopenharmony_ciparser.  In fact, context (semantics) is what it's all about - figuring
22f08c3bdfSopenharmony_ciout not just what the grouping of tokens are, but what the _types_ are
23f08c3bdfSopenharmony_cithat the grouping implies.
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ciAnd no, it doesn't use lex and yacc (or flex and bison).  In my personal
26f08c3bdfSopenharmony_ciopinion, the result of using lex/yacc tends to end up just having to
27f08c3bdfSopenharmony_cifight the assumptions the tools make. 
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ciThe parsing is done in five phases:
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci - full-file tokenization
32f08c3bdfSopenharmony_ci - pre-processing (which can cause another tokenization phase of another
33f08c3bdfSopenharmony_ci   file)
34f08c3bdfSopenharmony_ci - semantic parsing.
35f08c3bdfSopenharmony_ci - lazy type evaluation
36f08c3bdfSopenharmony_ci - inline function expansion and tree simplification
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ciNote the "full file" part. Partly for efficiency, but mostly for ease of
39f08c3bdfSopenharmony_ciuse, there are no "partial results". The library completely parses one
40f08c3bdfSopenharmony_ciwhole source file, and builds up the _complete_ parse tree in memory.
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ciAlso note the "lazy" in the type evaluation.  The semantic parsing
43f08c3bdfSopenharmony_ciitself will know which symbols are typedefines (required for parsing C
44f08c3bdfSopenharmony_cicorrectly), but it will not have calculated what the details of the
45f08c3bdfSopenharmony_cidifferent types are.  That will be done only on demand, as the back-end
46f08c3bdfSopenharmony_cirequires the information. 
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ciThis means that a user of the library will literally just need to do
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci  struct string_list *filelist = NULL;
51f08c3bdfSopenharmony_ci  char *file;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci  action(sparse_initialize(argc, argv, filelist));
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci  FOR_EACH_PTR(filelist, file) {
56f08c3bdfSopenharmony_ci    action(sparse(file));
57f08c3bdfSopenharmony_ci  } END_FOR_EACH_PTR(file);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ciand he is now done - having a full C parse of the file he opened.  The
60f08c3bdfSopenharmony_cilibrary doesn't need any more setup, and once done does not impose any
61f08c3bdfSopenharmony_cimore requirements.  The user is free to do whatever he wants with the
62f08c3bdfSopenharmony_ciparse tree that got built up, and needs not worry about the library ever
63f08c3bdfSopenharmony_ciagain.  There is no extra state, there are no parser callbacks, there is
64f08c3bdfSopenharmony_cionly the parse tree that is described by the header files. The action
65f08c3bdfSopenharmony_cifuntion takes a pointer to a symbol_list and does whatever it likes with it.
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ciThe library also contains (as an example user) a few clients that do the
68f08c3bdfSopenharmony_cipreprocessing, parsing and type evaluation and just print out the
69f08c3bdfSopenharmony_ciresults.  These clients were done to verify and debug the library, and
70f08c3bdfSopenharmony_cialso as trivial examples of what you can do with the parse tree once it
71f08c3bdfSopenharmony_ciis formed, so that users can see how the tree is organized. 
72