[ Pobierz całość w formacie PDF ]
lex
&
yacc
John R. Levine
Tony Mason
Doug Brown
O'Reilly
&
Associates, Inc.
103
Morris Street, Suite
A
Sebastopol,
CA
95472
/ex
&
yacc
by
John
R.
Levine, Tony Mason and Doug Brown
Copyright
O
1990, 1992 O'Reilly
&
Associates, Inc. All rights reserved.
Printed in the United States of America.
Editoc
Dale Dougherty
Printing History:
May 1990:
First Edition.
January 1991
:
Minor corrections.
October 1992:
Second Edition. Major revisions.
Nutshell Handbook and the Nutshell Handbook logo are registered trademarks of O'Reilly
&
Associates, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and O'Reilly and
Associates, Inc. was aware of a trademark claim, the designations have been printed in caps
or initial caps.
While every precaution has been taken in the preparation of this book, the publisher
assumes no responsibility for errors or omissions, or for damages resulting from the use of
the information contained herein.
This book is printed on acid-free paper
with
50%
recycled content, 10-15% post-consumer
waste. O'Reilly
&
Associates
is
comrnirted
to using paper with the highest recycled
content available consistent with high quality.
ISBN: 1-56592-000-7
Table
of
Contents
Preface
..
............................
xvii
What's New in the Second Edition
............................
xvii
...
..............................................................................
Scope of This
Book
xviil
.......................................................................
Availability of Lex and Yacc
xx
........................................................................................
Sample Programs
xx
........................................................
Conventions Used in This Handbook
xxi
...................................................................................
Acknowledgments
xxii
I: Lex
and
Yacc
I
............................................................................
The Simplest Lex Program
..
.....................................
3
......................................................................................
2
Recognizing Words with Lex
...............................
Symbol Tables
9
....................................................................................................
Grammars
13
............................................................
Parser-Lexer Communication
14
The Parts of Speech Lexer
15
......................................................................................
...............................................................
A Yacc Parser
17
...............................................................................
The Rules Section
18
................................................................................
Running Lex and Yacc
21
.......................................................................
Lex
vs Hand-written Lexers
22
.....................................................................................................
Exercises
25
2:
Using
Lex
27
....................................................................................
Regular Expressions
28
Examples of Regular Expressions
30
..........................................................................
.....................................................
A Word Counting Program
32
Parsing a Command Line
............................................................................
38
..........................................................................................
Start States
42
A
C Source Code Analyzer
......................................
..
.
45
..................................................................................................
Summary
48
....................................................................................................
Exercises
49
.
3:
Using Yacc
51
Grammars
.................................................................................................
51
Recursive Rules
..................................................................................
53
Shift/Reduce Parsing
..
53
What Yacc Cannot Parse
..............................................................
55
..............................................................................................
.................................
.............................................
A Yacc Parser
56
The Definition Section 56
...............................................................................
......................................................................
The Rules Section
56
................................................................
Symbol Values and Actions
57
The Lexer
...................................................................................................
58
............................................
Compiling and Running a Simple Parser 57
.......................................................
Arithmetic Expressions and Ambiguity
60
...................................................
When Not to Use Precedence Rules
63
Variables and Typed Tokens 64
Symbol Values and %union
......................
...
................................
65
.............................................................................................
.....................................................................
Symbol Tables
67
..................................................................
Functions and Reserved Words
71
................................................
Reserved Words in the Symbol Table
72
.................................
Interchangeable Function and Variable Names
76
Building Parsers with Make 77
.....................................................................................................
......................................................................
Summary
78
....................................................................................................
Exercises
78
4:
A
Menu Generation Language
81
...................................................................................
Developing the MGL 83
........................................................................................
Building the MGL
72
.......................................................................................
Initialization
95
Screen Processing 77
...............................................................................................
....................................................................................
Termination
100
.....................................................................................
Sample MGL Code
102
Exercises
................................................................................................
107
5:
Parsing SQL
109
........................................................................
A Quick Overview
of
SQL
109
Relational Data Bases
........................
..
..........................................
110
Manipulating Relations
..................................................................
111
....................................
Three Ways to Use SQL
........................
...
112
....................................................
...
114
.....................................
............
The Syntax Checker
......................
The Lexer
...........................
..
..
114
Overview of the MGL 81
...................................................................................
[ Pobierz całość w formacie PDF ]