282
pages
English
Documents
Le téléchargement nécessite un accès à la bibliothèque YouScribe Tout savoir sur nos offres
282
pages
English
Documents
Le téléchargement nécessite un accès à la bibliothèque YouScribe Tout savoir sur nos offres
Programming Utilities Guide
2550 Garcia Avenue
Mountain View, CA 94043
U.S.A.
A Sun Microsystems, Inc. BusinessÓ
1994 Sun Microsystems, Inc.
2550 Garcia Avenue, Mountain View, California 94043-1100 U.S.A.
All rights reserved. This product and related documentation are protected by copyright and distributed under licenses
restricting its use, copying, distribution, and decompilation. No part of this product or related documentation may be
reproduced in any form by any means without prior written authorization of Sun and its licensors, if any.
®Portions of this product may be derived from the UNIX and Berkeley 4.3 BSD systems, licensed from UNIX System
Laboratories, Inc., a wholly owned subsidiary of Novell, Inc., and the University of California, respectively. Third-party font
software in this product is protected by copyright and licensed from Sun’s font suppliers.
RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States Government is subject to the restrictions
set forth in DFARS 252.227-7013 (c)(1)(ii) and FAR 52.227-19.
The product described in this manual may be protected by one or more U.S. patents, foreign patents, or pending applications.
TRADEMARKS
Sun, the Sun logo, Sun Microsystems, Sun Microsystems Computer Corporation, SunSoft, the SunSoft logo, Solaris, SunOS,
OpenWindows, DeskSet, ONC, ONC+, and NFS are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S.
and certain other countries. UNIX is a registered trademark of Novell, Inc., in the United States and other countries; X/Open
Company, Ltd., is the exclusive licensor of such trademark. USL is a trademark of Novell, Inc. UnixWare is a trademark of
Novell, Inc. OPEN LOOK is a trademark of Novell, Inc. PostScript and Display PostScript are trademarks of Adobe Systems,
Inc. All other product names mentioned herein are the trademarks of their respective owners.
All SPARC trademarks, including the SCD Compliant Logo, are trademarks or registered trademarks of SPARC International,
Inc. SPARCstation, SPARCserver, SPARCengine, SPARCstorage, SPARCware, SPARCcenter, SPARCclassic, SPARCcluster,
SPARCdesign, SPARC811, SPARCprinter, UltraSPARC, microSPARC, SPARCworks, and SPARCompiler are licensed
exclusively to Sun Microsystems, Inc. Products bearing SPARC trademarks are based upon an architecture developed by Sun
Microsystems, Inc.
®The OPEN LOOK and Sun™ Graphical User Interfaces were developed by Sun Microsystems, Inc. for its users and licensees.
Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user
interfaces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical User Interface,
which license also covers Sun’s licensees who implement OPEN LOOK GUIs and otherwise comply with Sun’s written license
agreements.
X Window System is a product of the Massachusetts Institute of Technology.
THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE
PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW
EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN
THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
Please
RecycleContents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1. lex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Generating a Lexical Analyzer Program. . . . . . . . . . . . . . . . . . . 2
Writing lex Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Fundamentals of lex Rules . . . . . . . . . . . . . . . . . . . . . . . 4
Advancedlex Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
C++ Mangled Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Using lex and yacc Together 23
Automaton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Summary of Source Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2. yacc — A Compiler Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Basic Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Actions 37
iiiLexical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Parser Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Ambiguity and Conflicts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The yacc Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Hints for Preparing Specifications . . . . . . . . . . . . . . . . . . . . . . . . 64
Input Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Left Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
C++ Mangled Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Lexical Tie-Ins 66
Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Simulating error and accept in Actions. . . . . . . . . . . . . . . . . 67
Accessing Values in Enclosing Rules. . . . . . . . . . . . . . . . . . . 68
Support for Arbitrary Value Types . . . . . . . . . . . . . . . . . . . . 69
yacc Input Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
An Advanced Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3. make Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Dependency Checking: make vs. Shell Scripts . . . . . . . . . 94
Writing a Simple Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
iv Programming Utilities Guide—August 1994Basic Use of Implicit Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Processing Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Null Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Special Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Unknown Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Duplicate Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Reserved make Words. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Running Commands Silently . . . . . . . . . . . . . . . . . . . . . . . . 105
Automatic Retrieval of SCCS Files . . . . . . . . . . . . . . . . . . . . 106
Passing Parameters: Simple make Macros . . . . . . . . . . . . . 107
.KEEP_STATE and Command Dependency Checking . . . 109ATE and Hidden Dependencies . . . . . . . . . . . . 110
Displaying Information About a make Run 112
Using make to Compile Programs. . . . . . . . . . . . . . . . . . . . . . . . 114
A Simple Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Using Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Using Implicit Rules to Simplify a Makefile: Suffix Rules . 117
When to Use Explicit Target Entries vs. Implicit Rules . . . 118
Implicit Rules and Dynamic Macros . . . . . . . . . . . . . . . . . . . 119
Adding Suffix Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Pattern-Matching Rules:An Alternative to Suffix Rules . . 124
Building Object Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Libraries, Members, and Symbols . . . . . . . . . . . . . . . . . . . . . 131
Library Members and Dependency Checking . . . . . . . . . . . 132
Contents vUsing make to Maintain Libraries and Programs . . . . . . . . . . . 133
More about Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Linking with System-Supplied Libraries . . . . . . . . . . . . . . . 136
Compiling Programs for Debugging and Profiling . . . . . . . 137
Compiling Debugging and Profiling Variants . . . . . . . . . . 139
Maintaining Separate Program and Library Variants . . . . . 140
Maintaining a Directory of Header Files . . . . . . . . . . . . . . . 145
Compiling and Linking with Your Own Libraries . . . . . . . 145
Nested make Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Passing Parameters to