Synopses & Reviews
If you need to parse or process text data in Linux or Unix, this useful book explains how to use flex and bison to solve your problems quickly. flex & bison is the long-awaited sequel to the classic O'Reilly book, lex & yacc. In the nearly two decades since the original book was published, the flex and bison utilities have proven to be more reliable and more powerful than the original Unix tools.
flex & bison covers the same core functionality vital to Linux and Unix program development, along with several important new topics. You'll find revised tutorials for novices and references for advanced users, as well as an explanation of each utility's basic usage and simple, standalone applications you can create with them. With flex & bison, you'll discover the wide range of uses these flexible tools offer.
- Address syntax crunching that regular expressions tools can't handle
- Build compilers and interpreters, and handle a wide range of text processing functions
- Interpret code, configuration files, or any other structured format
- Learn key programming techniques, including abstract syntax trees and symbol tables
- Implement a full SQL grammar-with complete sample code
- Use new features such as pure (reentrant) lexers and parsers, powerful GLR parsers, and interfaces to C++
This updated classic teaches Unix and Linux developers how to solve specific problems by processing text data, using two key Unix utilities -- flex and bison. Since 1992, the previous edition of this book has taught programmers how to use the lex and yacc Unix utilities in program development. Now, the tools have shifted to flex and bison, and this new edition shows programmers how to use these utilities to parse or process text data, whether it's code, configuration files, or any other structured format. flex & bison addresses a wide range of text processing functions, as well as syntax crunching that regular expressions tools can't handle. The book also offers expanded coverage of key programming techniques, including syntax trees and symbol tables, and helps programmers build compilers and interpreters.
This updated classic teaches UNIX and Linux developers how to solve specific problems by processing text data, using two key UNIX utilities--Flex and Bison. Covers key programming techniques, including syntax trees and symbol tables. 300 pp
About the Author
John Levine, founder of Taughannock Networks, writes, speaks, and consults on e-mail, the Internet, and other computer topics. He has written over 20 technical books, and is the co-author of O'Reilly's lex & yacc, 2nd Edition and qmail. He's also deeply involved in Internet e-mail in general and spam issues in particular as co-chair of the Internet Research Task Force's Anti-Spam Research Group (http://asrg.sp.am ) and a board member of the Coalition Against Unsolicited Commercial E-mail (http://www.cauce.org.)He lives and works in the tiny village of Trumansburg NY (http://www.trumansburg.ny.us) where he reports that being the municipal sewer commissioner was a much cleaner job than dealing with spammers.
Table of Contents
Preface; Scope of This Book; Conventions Used in This Book; Getting Flex and Bison; This Book's Example Files; Using Code Examples; Safari® Books Online; How to Contact Us; Acknowledgments; Chapter 1: Introducing Flex and Bison; 1.1 Lexical Analysis and Parsing; 1.2 Regular Expressions and Scanning; 1.3 Grammars and Parsing; 1.4 Ambiguous Grammars: Not Quite; 1.5 Adding a Few More Rules; 1.6 Flex and Bison vs. Handwritten Scanners and Parsers; 1.7 Exercises; Chapter 2: Using Flex; 2.1 Regular Expressions; 2.2 File I/O in Flex Scanners; 2.3 Reading Several Files; 2.4 The I/O Structure of a Flex Scanner; 2.5 Start States and Nested Input Files; 2.6 Symbol Tables and a Concordance Generator; 2.7 C Language Cross-Reference; 2.8 Exercises; Chapter 3: Using Bison; 3.1 How a Bison Parser Matches Its Input; 3.2 Shift/Reduce Parsing; 3.3 A Bison Parser; 3.4 Abstract Syntax Trees; 3.5 An Improved Calculator That Creates ASTs; 3.6 Shift/Reduce Conflicts and Operator Precedence; 3.7 An Advanced Calculator; 3.8 Using the Advanced Calculator; 3.9 Exercises; Chapter 4: Parsing SQL; 4.1 A Quick Overview of SQL; 4.2 Manipulating Relations; 4.3 Three Ways to Use SQL; 4.4 SQL to RPN; 4.5 The Lexer; 4.6 The Parser; 4.7 The Makefile for the SQL Parser; 4.8 Exercises; Chapter 5: A Reference for Flex Specifications; 5.1 Structure of a Flex Specification; 5.2 BEGIN; 5.3 C++ Scanners; 5.4 Context Sensitivity; 5.5 Definitions (Substitutions); 5.6 ECHO; 5.7 Input Management; 5.8 Flex Library; 5.9 Interactive and Batch Scanners; 5.10 Line Numbers and yylineno; 5.11 Literal Block; 5.12 Multiple Lexers in One Program; 5.13 Options When Building a Scanner; 5.14 Portability of Flex Lexers; 5.15 Reentrant Scanners; 5.16 Regular Expression Syntax; 5.17 REJECT; 5.18 Returning Values from yylex(); 5.19 Start States; 5.20 unput(); 5.21 yyinput() yyunput(); 5.22 yyleng; 5.23 yyless(); 5.24 yylex() and YY_DECL; 5.25 yymore(); 5.26 yyrestart(); 5.27 yy_scan_string and yy_scan_buffer; 5.28 YY_USER_ACTION; 5.29 yywrap(); Chapter 6: A Reference for Bison Specifications; 6.1 Structure of a Bison Grammar; 6.2 Actions; 6.3 Ambiguity and Conflicts; 6.4 Bugs in Bison Programs; 6.5 C++ Parsers; 6.6 %code Blocks; 6.7 End Marker; 6.8 Error Token and Error Recovery; 6.9 Inherited Attributes ($0); 6.10 %initial-action; 6.11 Lexical Feedback; 6.12 Literal Block; 6.13 Literal Tokens; 6.14 Locations; 6.15 %parse-param; 6.16 Portability of Bison Parsers; 6.17 Precedence and Associativity Declarations; 6.18 Recursive Rules; 6.19 Rules; 6.20 Special Characters; 6.21 %start Declaration; 6.22 Symbol Values; 6.23 Tokens; 6.24 Variant and Multiple Grammars; 6.25 Multiple Parsers; 6.26 y.output Files; 6.27 Bison Library; 6.28 YYABORT; 6.29 YYACCEPT; 6.30 YYBACKUP; 6.31 yyclearin; 6.32 yydebug and YYDEBUG; 6.33 yyerrok; 6.34 YYERROR; 6.35 yyerror(); 6.36 yyparse(); 6.37 YYRECOVERING(); Chapter 7: Ambiguities and Conflicts; 7.1 The Pointer Model and Conflicts; 7.2 Kinds of Conflicts; 7.3 Parser States; 7.4 Contents of name.output; 7.5 Reduce/Reduce Conflicts; 7.6 Shift/Reduce Conflicts; 7.7 Review of Conflicts in name.output; 7.8 Common Examples of Conflicts; 7.9 How Do You Fix the Conflict?; 7.10 Summary; 7.11 Exercises; Chapter 8: Error Reporting and Recovery; 8.1 Error Reporting; 8.2 Locations; 8.3 Error Recovery; 8.4 Bison Error Recovery; 8.5 Compiler Error Recovery; 8.6 Exercises; Chapter 9: Advanced Flex and Bison; 9.1 Pure Scanners and Parsers; 9.2 GLR Parsing; 9.3 C++ Parsers; 9.4 Exercises; SQL Parser Grammar and Cross-Reference; Glossary; Colophon;