⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flex.texinfo

📁 早期freebsd实现
💻 TEXINFO
📖 第 1 页 / 共 5 页
字号:
\input texinfo       @c                    -*- Texinfo -*-@setfilename flex.info@ifinfo@formatSTART-INFO-DIR-ENTRY* flex: (flex).			Fast lexical analyzer generator.END-INFO-DIR-ENTRY@end format@end ifinfo@synindex ky cp@c@c This file documents ``flex'', the fast lexical analyzer generator.@c@c This text is based on the file ``flexdoc.1'' in the UCB flex@c distribution.  The text may be freely distributed under the terms of@c the UCB license; see file COPYING.@c@c $Id: flex.texinfo,v 1.5 1992/05/29 20:40:09 pesch Exp $@setchapternewpage odd@settitle Using @code{flex}@titlepage@finalout@c @smallbook@c @cropmarks@title Using @code{flex}@subtitle A Fast Lexical Analyzer Generator@sp 1@subtitle 26 May 1990---Version 2.3@sp 4@quotationThis product includes software developed by the University ofCalifornia, Berkeley and its contributors.@end quotation@author Vern Paxson@page@tex\def\$#1${{#1}} % Kluge: collect RCS revision info without $...$\xdef\manvers{\$Revision: 1.5 $} % For use in headers, footers too{\parskip=0pt \hfill {\it Using flex} \par \hfill \manvers\par \hfill\TeX{}info \texinfoversion\par\hfill Original text: {\tt vern@@ee.lbl.gov}\par \hfill Texinfo conversion:{\tt pesch@@cygnus.com}\par}@end tex@vskip 0pt plus 1filllCopyright (c) 1990 The Regents of the University of California.All rights reserved.This code is derived from software contributed to Berkeley byVern Paxson.The United States Government has rights in this work pursuantto contract no. DE-AC03-76SF00098 between the United StatesDepartment of Energy and the University of California.Redistribution and use in source and binary forms are permittedprovided that: (1) source distributions retain this entirecopyright notice and comment, and (2) distributions includingbinaries display the following acknowledgement:  ``This productincludes software developed by the University of California,Berkeley and its contributors'' in the documentation or othermaterials provided with the distribution and in all advertisingmaterials mentioning features or use of this software.  Neither thename of the University nor the names of its contributors may beused to endorse or promote products derived from this softwarewithout specific prior written permission.THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE.@end titlepage@ifinfo@node Top@top FLEX---fast lexical analyzer generator@quotationThis product includes software developed by the University ofCalifornia, Berkeley and its contributors.@end quotation@menu* Introduction::        An Overview of @code{flex}, with Examples* Files::               Input and Output Files* Invoking::            Command-line Options* Performance::         Performance Considerations* Incompatibilities::   Incompatibilities with @code{lex} and @sc{posix}* Diagnostics::         Diagnostic Messages* Bugs::                Deficiencies and Bugs* Acknowledgements::    Contributors to flex@end menu@end ifinfo@node Introduction@chapter An Overview of @code{flex}, with Examples@code{flex} is a tool for generating scanners: programs which recognizelexical patterns in text.  @code{flex} reads the given input files (orits standard input if no file names are given) for a description of thescanner to generate.  The description is in the form of pairs of regularexpressions and C code, called @dfn{rules}.  @code{flex} generates asoutput a C source file, @file{lex.yy.c}, which defines a routine@code{yylex}. Compile and link this file with the @samp{-lfl} library toproduce an executable.  When the executable runs, it analyzes its inputfor occurrences of the regular expressions.  Whenever it finds one, itexecutes the corresponding C code.Some simple examples follow, to give you the flavor of using@code{flex}.@menu* Text-Substitution::   Trivial Text-Substitution* Counter::             Count Lines and Characters* Toy::                 Simplified Pascal-like Language@end menu@node Text-Substitution@section Text-Substitution ScannerThe following @code{flex} input specifies a scanner which,whenever it encounters the string @samp{username}, will replace itwith the user's login name:@example%%username    printf( "%s", getlogin() );@end exampleBy default, any text not matched by a @code{flex} scanner is copied tothe output, so the net effect of this scanner is to copy its input fileto its output with each occurrence of @samp{username} expanded.  In thisinput, there is just one rule.  @samp{username} is the pattern and the@code{printf} is the action.  The @samp{%%} marks the beginning of therules.@node Counter@section A Scanner to Count Lines and CharactersHere's another simple example:@example    int num_lines = 0, num_chars = 0;%%\n    ++num_lines; ++num_chars;.     ++num_chars;%%main()    @{    yylex();    printf( "# of lines = %d, # of chars = %d\n",            num_lines, num_chars );    @}@end exampleThis scanner counts the number of characters and the  numberof  lines in its input (it produces no output other than thefinal report on the counts).  The first  line  declares  twoglobals,  @code{num_lines}  and @code{num_chars}, which are accessibleboth inside @code{yylex} and in the @code{main} routine declared afterthe  second  @samp{%%}.  There are two rules, one which matches anewline (@samp{\n}) and increments both the line  count  and  thecharacter  count,  and one which matches any character otherthan a newline (indicated by the @samp{.} regular expression).@node Toy@section Simplified Pascal-like Language ScannerA somewhat more complicated example:@smallexample/* scanner for a toy Pascal-like language */%@{/* need this for the call to atof() below */#include <math.h>%@}DIGIT    [0-9]ID       [a-z][a-z0-9]*%%@{DIGIT@}+    @{            printf( "An integer: %s (%d)\n", yytext,                    atoi( yytext ) );            @}@{DIGIT@}+"."@{DIGIT@}*        @{            printf( "A float: %s (%g)\n", yytext,                    atof( yytext ) );            @}if|then|begin|end|procedure|function        @{            printf( "A keyword: %s\n", yytext );            @}@{ID@}        printf( "An identifier: %s\n", yytext );"+"|"-"|"*"|"/"   printf( "An operator: %s\n", yytext );"@{"[^@}\n]*"@}"     /* eat up one-line comments */[ \t\n]+          /* eat up whitespace */.           printf( "Unrecognized character: %s\n", yytext );%%main( argc, argv )int argc;char **argv;    @{    ++argv, --argc;  /* skip over program name */    if ( argc > 0 )            yyin = fopen( argv[0], "r" );    else            yyin = stdin;    yylex();    @}@end smallexampleThis is the beginnings of a simple scanner for a language like Pascal.It identifies different types of tokens and reports on what it has seen.The details of this example are explained in the following chapters.@node Files@chapter Input and Output Files@code{flex}'s actions are specified by definitions (which may includeembedded C code) in one or more input files.  The primaryoutput file is @file{lex.yy.c}.  You can also use some of thecommand-line options to get diagnostic output(@pxref{Invoking,,Command-line options}).This chapter gives the details of how to structure your input todefine the scanner you need.@menu* Input Format::        Format of the Input File* Scanner::             The Generated Scanner* Start::               Start Conditions* Multiple Input::      Multiple Input Buffers* EOF::                 End-of-File Rules* Misc::                Miscellaneous Macros* Parsers::             Interfacing with Parser Generators* Translation::         Translation Table@end menu@node Input Format@section Format of the Input FileThe @code{flex} input file consists of three sections, separated by aline with just @samp{%%} in it:@example@var{definitions}%%@var{rules}%%@var{user code}@end exampleThe @var{definitions} section contains declarations of simple namedefinitions to simplify the scanner specification, and declarations ofstart conditions, which are explained in a later section.@noindentName definitions have the form:@example@var{name} @var{definition}@end exampleThe @var{name} is a word beginning with a letter or an underscore(@samp{_}) followed by zero or more letters, digits, @samp{_}, or@samp{-} (dash).  The definition is taken to begin at the firstnon-whitespace character following the name, and continuing to the endof the line.  The definition can subsequently be referred to using@samp{@{@var{name}@}}, which will expand to @samp{(@var{definition})}.For example,@exampleDIGIT    [0-9]ID       [a-z][a-z0-9]*@end exampledefines @samp{DIGIT} to be a regular expression which matches a singledigit, and @samp{ID} to be a regular expression which matches a letterfollowed by zero or more letters or digits.  A subsequent reference to@example@{DIGIT@}+"."@{DIGIT@}*@end example@noindentis identical to@example([0-9])+"."([0-9])*@end example@noindentand matches one or more digits followed by a @samp{.} followed by zeroor more digits.The rules section of the @code{flex} input contains a series of rules ofthe form:@example@var{pattern}   @var{action}@end example@noindentwhere the @var{pattern} must be unindented and the @var{action} mustbegin on the same line.See below for a further description of patterns and actions.Finally, the user code section is simply copied to @file{lex.yy.c}verbatim.  It is used for companion routines which call or are called bythe scanner.  The presence of this section is optional; if it ismissing, the second @samp{%%} in the input file may be skipped, too.In the definitions and rules sections, any indented text or textenclosed in @samp{%@{} and @samp{%@}} is copied verbatim to the output(with the @samp{%@{@}} removed).  The @samp{%@{@}} must appearunindented on lines by themselves.In the rules section, any indented or @samp{%@{@}} text appearing beforethe first rule may be used to declare variables which are local to thescanning routine and (after the declarations) code which is to beexecuted whenever the scanning routine is entered.  Other indented or@samp{%@{@}} text in the rule section is still copied to the output, butits meaning is not well defined and it may well cause compile-timeerrors (this feature is present for @sc{posix} compliance; see below forother such features).In the definitions section, an unindented comment (i.e., a linebeginning with @samp{/*}) is also copied verbatim to the output up tothe next @samp{*/}.  Also, any line in the definitions section beginningwith @samp{#} is ignored, though this style of comment is deprecated andmay go away in the future.@menu* Patterns::    Patterns in the input* Matching::    How the input is matched* Actions::     Actions@end menu@node Patterns@subsection Patterns in the InputThe patterns in the input are written using an extended set of regularexpressions.  These are:@table @code@item @var{x}match the character @samp{@var{x}}@item .any character except newline@item [xyz]a ``character class''; in this case, the pattern matches either an@samp{x}, a @samp{y}, or a @samp{z}@item [abj-oZ]a ``character class'' with a range in it; matches an @samp{a}, a@samp{b}, any letter from @samp{j} through @samp{o}, or a @samp{Z}@item [^A-Z]a ``negated character class'', i.e., any character but those in theclass.  In this case, any character @emph{except} an uppercase letter.@item [^A-Z\n]any character @emph{except} an uppercase letter or a newline

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -