---
title: "flex基础小记"
author: "pf_miles"
date: "2008-12-09T20:13:29Z"
tags: ["Access", "Flex"]
slug: "flex"
draft: false
lastmod: "2008-12-11T22:38:29Z"
id: 289671
url: "https://pf-miles.iteye.com/blog/289671"
---

The \`flex' input file consists of three sections, separated by a line containing only \`%%'. \[code="c"] definitions %% rules %% user code \[/code] \[color=blue]Some patterns:\[/color] \`\\123' the character with octal value 123 \`\\x2a' the character with hexadecimal value 2a \`(?r-s:pattern)' apply option \`r' and omit option \`s' while interpreting pattern. Options may be zero or more of the characters \`i', \`s', or \`x'. \`(?# comment )' omit everything within \`()'. The first \`)' character encountered ends the pattern. It is not possible to for the comment to contain a \`)' character. The comment may span lines.\\ \`r/s' an \`r' but only if it is followed by an \`s'. \`r' an \`r', but only in start condition \`s' \`r' same, but in any of start conditions \`s1', \`s2', or \`s3'. \`&lt;&gt;' an end-of-file. \`&lt;&gt;' an end-of-file when in start condition \`s1' or \`s2' \[color=blue]character class expressions:\[/color] \[code="c"] \[:alnum:] \[:alpha:] \[:blank:] \[:cntrl:] \[:digit:] \[:graph:] \[:lower:] \[:print:] \[:punct:] \[:space:] \[:upper:] \[:xdigit:] \[:^alnum:] \[:^alpha:] \[:^blank:] \[:^cntrl:] \[:^digit:] \[:^graph:] \[:^lower:] \[:^print:] \[:^punct:] \[:^space:] \[:^upper:] \[:^xdigit:] \[/code] The \`{-}' operator computes the difference of two character classes, '{+}' does a union operation. 匹配方式是“最大长度优先”，长度一样时写在前面的优先。 每当匹配到一个token时，这个token能在全局char型指针'\[color=red]yytext\[/color]'中被找到，它的长度可以从全局int '\[color=red]yyleng\[/color]'里读到；这个token对应的action也在这个时候被执行；token之后的文本被继续扫描。 \[color=blue]actions special directives:\[/color] \`ECHO' copies yytext to the scanner's output. \`BEGIN' followed by the name of a start condition places the scanner in the corresponding start condition \`REJECT' directs the scanner to proceed on to the "second best" rule which matched the input (or a prefix of the input). \`yymore()' tells the scanner that the next time it matches a rule, the corresponding token should be \_appended_ onto the current value of \`yytext' rather than replacing it. \`yyless(n)' returns all but the first \`n' characters of the current token back to the input stream, where they will be rescanned when the scanner looks for the next match. \`yytext' and \`yyleng' are adjusted appropriately \`input()' reads the next character from the input stream. \`YY\_FLUSH\_BUFFER()' flushes the scanner's internal buffer \`yyterminate()' can be used in lieu of a return statement in an action. \[color=blue]Start Conditions:\[/color] \`%s' or '%x' represents "inclusive" start conditions or "exclusive" start conditions. \`BEGIN(INITIAL)' is equivalent to \`BEGIN(0)' We can access the current start condition using the integer-valued \`YY\_START' macro, and Flex provides \`YYSTATE' as an alias for \`YY\_START' \[color=blue]Multiple Input Buffers:\[/color] -- Function: YY\_BUFFER\_STATE \[color=darkred]yy\_create\_buffer\[/color] ( FILE \*file, int size ) which takes a \`FILE' pointer and a size and creates a buffer associated with the given file and large enough to hold \`size' characters (when in doubt, use \`YY\_BUF\_SIZE' for the size). -- Function: void \[color=darkred]yy\_switch\_to\_buffer\[/color] ( YY\_BUFFER\_STATE new\_buffer ) The above function switches the scanner's input buffer so subsequent tokens will come from \`new\_buffer'. -- Function: void \[color=darkred]yy\_delete\_buffer\[/color] ( YY\_BUFFER\_STATE buffer ) is used to reclaim the storage associated with a buffer. -- Function: void \[color=darkred]yypush\_buffer\_state\[/color] ( YY\_BUFFER\_STATE buffer ) This function pushes the new buffer state onto an internal stack. -- Function: void \[color=darkred]yypop\_buffer\_state\[/color] ( ) This function removes the current state from the top of the stack, and deletes it by calling \`yy\_delete\_buffer'. -- Function: void \[color=darkred]yy\_flush\_buffer\[/color] ( YY\_BUFFER\_STATE buffer ) This function discards the buffer's contents -- Function: YY\_BUFFER\_STATE \[color=darkred]yy\_new\_buffer\[/color] ( FILE \*file, int size ) is an alias for \`yy\_create\_buffer()' \`YY\_CURRENT\_BUFFER' macro returns a \`YY\_BUFFER\_STATE' handle to the current buffer. It should not be used as an lvalue. -- Function: YY\_BUFFER\_STATE \[color=darkred]yy\_scan\_string\[/color] ( const char \*str ) scans a NUL-terminated string. -- Function: YY\_BUFFER\_STATE \[color=darkred]yy\_scan\_bytes\[/color] ( const char \*bytes, int len ) scans \`len' bytes (including possibly \`NUL's) starting at location \`bytes'. Note that both of above two functions create and scan a \_copy_ of the string or bytes.You can avoid the copy by using: -- Function: YY\_BUFFER\_STATE \[color=darkred]yy\_scan\_buffer\[/color] (char \*base, yy\_size\_t size) the last two bytes of which \_must_ be \`YY\_END\_OF\_BUFFER\_CHAR' (ASCII NUL). These last two bytes are not scanned; thus, scanning consists of \`base\[0]' through \`base\[size-2]', inclusive. -- Data type: yy\_size\_t is an integral type to which you can cast an integer expression reflecting the size of the buffer.