📄 lex.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>lex</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_001_014_1080"> </a>NAME</h4><blockquote>lex - generate programs for lexical tasks (<b>DEVELOPMENT</b>)</blockquote><h4><a name = "tag_001_014_1081"> </a>SYNOPSIS</h4><blockquote><pre><code>lex -c<b>[</b>-t<b>][ </b>-n| -v<b>][</b><i>file</i>...<b>]</b></code></pre></blockquote><h4><a name = "tag_001_014_1082"> </a>DESCRIPTION</h4><blockquote>The<i>lex</i>utility generates C programs to be used in lexical processing of characterinput, and that can be used as an interface to<i><a href="yacc.html">yacc</a></i>.The C programs are generated from<i>lex</i>source code and conform to the ISO C standard.Usually, the<i>lex</i>utility writes the program it generates to the file<b>lex.yy.c</b>;the state of this file is unspecified if<i>lex</i>exits with a non-zero exit status.See the EXTENDED DESCRIPTION section for a complete description of the<i>lex</i>input language.</blockquote><h4><a name = "tag_001_014_1083"> </a>OPTIONS</h4><blockquote>The<i>lex</i>utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b> </a> .<p>The following options are supported:<dl compact><dt><b>-c</b><dd>Indicate C-language action (default option).<dt><b>-n</b><dd>Suppress the summary of statistics usually written with the<b>-v</b>option.If no table sizes are specified in the<i>lex</i>source code and the<b>-v</b>option is not specified, then<b>-n</b>is implied.<dt><b>-t</b><dd>Write the resulting program to standard output instead of<b>lex.yy.c</b>.<dt><b>-v</b><dd>Write a summary of<i>lex</i>statistics to the standard output.(See the discussion of<i>lex</i>table sizes in<xref href=lexdefs><a href="#tag_001_014_1092_001">Definitions in lex</a></xref>.)If the<b>-t</b>option is specified and<b>-n</b>is not specified, this report will be written to standard error.If table sizes are specified in the<i>lex</i>source code,and if the<b>-n</b>option is not specified, the<b>-v</b>option may be enabled.</dl></blockquote><h4><a name = "tag_001_014_1084"> </a>OPERANDS</h4><blockquote>The following operand is supported:<dl compact><dt><i>file</i><dd>A pathname of an input file.If more than one such<i>file</i>is specified, all files will beconcatenated to produce a single<i>lex</i>program.If no<i>file</i>operands are specified,or if a<i>file</i>operand is "-", the standard input will be used.</dl></blockquote><h4><a name = "tag_001_014_1085"> </a>STDIN</h4><blockquote>The standard input will be used if no<i>file</i>operands are specified,or if a<i>file</i>operand is "-".See<b>INPUT FILES</b>.</blockquote><h4><a name = "tag_001_014_1086"> </a>INPUT FILES</h4><blockquote>The input files must be text files containing<i>lex</i>source code, as described in the EXTENDED DESCRIPTION section.</blockquote><h4><a name = "tag_001_014_1087"> </a>ENVIRONMENT VARIABLES</h4><blockquote>If this variable is not set to the POSIX locale, the results are unspecified.The following environment variables affect the execution of<i>lex</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_COLLATE</i><dd>Determine the locale for thebehaviour of ranges, equivalence classesand multi-character collating elementswithin regular expressions.If this variable is not set to the POSIX locale, the results are unspecified.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text data ascharacters (for example, single- as opposed to multi-byte charactersin arguments and input files), andthe behaviour of character classes within regular expressions.If this variable is not set to the POSIX locale, the results are unspecified.<dt><i>LC_MESSAGES</i><dd>Determine the locale that should be used to affectthe format and contents of diagnosticmessages written to standard error.<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i></dl></blockquote><h4><a name = "tag_001_014_1088"> </a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_1089"> </a>STDOUT</h4><blockquote>If the<b>-t</b>option is specified,the text file of C source code output of<i>lex</i>will be written to standard output.<p>If the<b>-t</b>option is not specified:<ol><p><li>Implementation-dependent informational,error and warning messages concerning the contents of<i>lex</i>source code inputwill be written to either the standard output or standard error.<p><li>If the<b>-v</b>option is specifiedand the<b>-n</b>option is not specified,<i>lex</i>statisticswill also be written to either the standard output or standard error,in an implementation-dependent format.These statistics may also be generated iftable sizes are specified with a"%"operator in the<i>Definitions</i>section (see the EXTENDED DESCRIPTION section),as long as the<b>-n</b>option is not specified.<p></ol></blockquote><h4><a name = "tag_001_014_1090"> </a>STDERR</h4><blockquote>If the<b>-t</b>option is specified,implementation-dependent informational,error and warning messages concerning the contents of<i>lex</i>source code input will be written to the standard error.<p>If the<b>-t</b>option is not specified:<ol><p><li>Implementation-dependent informational,error and warning messages concerning the contents of<i>lex</i>source code inputwill be written to either the standard output or standard error.<p><li>If the<b>-v</b>option is specifiedand the<b>-n</b>option is not specified,<i>lex</i>statisticswill also be written to either the standard output or standard error,in an implementation-dependent format.These statistics may also be generated iftable sizes are specified with a"%"operator in the<i>Definitions</i>section (see the EXTENDED DESCRIPTION section),as long as the<b>-n</b>option is not specified.<p></ol></blockquote><h4><a name = "tag_001_014_1091"> </a>OUTPUT FILES</h4><blockquote>A text file containing C source code will be written to<b>lex.yy.c</b>,or to the standard output if the<b>-t</b>option is present.<br></blockquote><h4><a name = "tag_001_014_1092"> </a>EXTENDED DESCRIPTION</h4><blockquote>Each input file contains<i>lex</i>source code,which is a table of regular expressions with correspondingactions in the form ofC program fragments.<p>When<b>lex.yy.c</b>is compiled and linked with the<i>lex</i>library (usingthe <b>-l l</b> operand with<i><a href="c89.html">c89</a></i>or<i><a href="cc.html">cc</a></i>),the resulting program reads characterinput from the standard input and partitions it intostrings that match the given expressions.<p>When an expression is matched,these actions will occur:<ul><p><li>The input string that was matched is left in<i>yytext</i>as a null-terminated string;<i>yytext</i>is either an external character arrayor a pointer to a character string.As explained in<xref href=lexdefs><a href="#tag_001_014_1092_001">Definitions in lex</a></xref>,the type can be explicitly selected using the<b>%array</b>or<b>%pointer</b>declarations, but the default is implementation-dependent.<p><li>The external<b>int</b><i>yyleng</i>is set to the length of the matching string.<p><li>The expression's correspondingprogram fragment, or action, is executed.<p></ul><p>During pattern matching,<i>lex</i>searches the set of patternsfor the single longest possible match.Among rules that matchthe same number of characters, the rule given firstwill be chosen.<p>The general format of<i>lex</i>source is:<pre><dl compact><dt> <dd><i>Definitions</i><b>%%</b><i>Rules</i><b>%%</b><i>User Subroutines</i></dl></pre><p>The first<b>%%</b>is required to mark the beginning of the rules(regular expressions and actions);the second<b>%%</b>is required only if user subroutines follow.<p>Any line in the<i>Definitions</i>section beginning with ablank characterwill be assumed to be a C programfragment and will be copied to the external definition area of the<b>lex.yy.c</b>file.Similarly, anything in the<i>Definitions</i>section included between delimiter lines containing only<b>%{</b>and<b>%}</b>will also be copied unchanged to the externaldefinition area of the<b>lex.yy.c</b>file.<p>Any such input (beginning with ablank characteror within<b>%{</b>and<b>%}</b>delimiter lines) appearing at the beginning of the<i>Rules</i>section before any rules are specified will be written to<b>lex.yy.c</b>after the declarations of variables for the<i>yylex()</i>function and before the first line of code in<i>yylex()</i>.Thus, user variables local to<i>yylex()</i>can be declared here,as well as application code to execute upon entry to<i>yylex()</i>.<p>The action taken by<i>lex</i>when encountering any inputbeginning with ablank characteror within<b>%{</b>and<b>%}</b>delimiter lines appearing in the<i>Rules</i>section but coming after one ormore rules is undefined.The presence of such input mayresult in an erroneous definition of the<i>yylex()</i>function.<h5><a name = "tag_001_014_1092_001"> </a>Definitions in lex</h5><xref type="5" name="lexdefs"></xref><i>Definitions</i>appear before the first<b>%%</b>delimiter.Any line in this section not contained between<b>%{</b>and<b>%}</b>lines and not beginning with ablank characteris assumed to define a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -