Read more
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++
List of contents
Inhaltsverzeichnis
Chapter 1 Introducing Flex and Bison
Lexical Analysis and Parsing
Regular Expressions and Scanning
Grammars and Parsing
Ambiguous Grammars: Not Quite
Adding a Few More Rules
Flex and Bison vs. Handwritten Scanners and Parsers
Exercises
Chapter 2 Using Flex Regular Expressions
File I/O in Flex Scanners
Reading Several Files
The I/O Structure of a Flex Scanner
Start States and Nested Input Files
Symbol Tables and a Concordance Generator
C Language Cross-Reference
Exercises
Chapter 3 Using Bison
How a Bison Parser Matches Its Input
Shift/Reduce Parsing
A Bison Parser
Abstract Syntax Trees
An Improved Calculator That Creates ASTs
Shift/Reduce Conflicts and Operator Precedence
An Advanced Calculator
Using the Advanced Calculator
Exercises
Chapter 4 Parsing SQL
A Quick Overview of SQL
Manipulating Relations
Three Ways to Use SQL
SQL to RPN
The Lexer
The Parser
The Makefile for the SQL Parser
Exercises
Chapter 5 A Reference for Flex Specifications
Structure of a Flex Specification
BEGIN
C++ Scanners
Context Sensitivity
Definitions (Substitutions)
ECHO
Input Management
Flex Library
Interactive and Batch Scanners
Line Numbers and yylineno
Literal Block
Multiple Lexers in One Program
Options When Building a Scanner
Portability of Flex Lexers
Reentrant Scanners
Regular Expression Syntax
REJECT
Returning Values from yylex()
Start States
unput()
yyinput() yyunput()
yyleng
yyless()
yylex() and YY_DECL
yymore()
yyrestart()
yy_scan_string and yy_scan_buffer
YY_USER_ACTION
yywrap()
Chapter 6 A Reference for Bison Specifications
Structure of a Bison Grammar
Actions
Ambiguity and Conflicts
Bugs in Bison Programs
C++ Parsers
%code Blocks
End Marker
Error Token and Error Recovery
Inherited Attributes ($0)
%initial-action
Lexical Feedback
Literal Block
Literal Tokens
Locations
%parse-param
Portability of Bison Parsers
Precedence and Associativity Declarations
Recursive Rules
Rules
Special Characters
%start Declaration
Symbol Values Tokens
Variant and Multiple Grammars
Multiple Parsers
y.output Files
Bison Library
YYABORT
YYACCEPT
YYBACKUP
yyclearin
yydebug and YYDEBUG
yyerrok
YYERROR
yyerror()
yyparse()
YYRECOVERING()
Chapter 7 Ambiguities and Conflicts
The Pointer Model and Conflicts
Kinds of Conflicts
Parser States
Contents of name.output
Reduce/Reduce Conflicts
Shift/Reduce Conflicts
Review of Conflicts in name.output
Common Examples of Conflicts
How Do You Fix the Conflict?
Summary
Exercises
Chapter 8 Error Reporting and Recovery
Error Reporting
Locations
Error Recovery
Bison Error Recovery
Compiler Error Recovery
Exercises
Chapter 9 Advanced Flex and Bison
Pure Scanners and Parsers
GLR Parsing
C++ Parsers
Exercises
Appendix SQL Parser Grammar and Cross-Reference
Glossary
Colophon