Flex2.5 User Manual (1)

zhaozj2021-02-17  99

Flex2.5 User Manual (1)

name:

Flex - Fast Lexical Analyzer Generator

Summary:

Flex [-BCDFHILNPSTVWBFILTV78 ? -C [AeffMR] -ooutput -pprefix -sskeleton] [--help --version] [filename ...]

table of Contents:

This manual will introduce Flex, a tool for generating a program that matches a text-based pattern.

This manual includes two parts: guidelines and reference:

1. Flex profile

2. Simple example

3. Flex input file format

4. mode

Flex uses an extension formal expression

5. How to entered how to match

The rule determines what needs to match

6. action

Used to specify operations performed after mode matching

7. Scanner generation

Detail of the lexical scanner generated by Flex;

How to control the input source;

8. START CONDitions

Introduce the context concept into the generated scanner

Manage Micro Scanner (Mini-Scanners)

9. Multiple input buffers (Multiple Input Buffers)

How to use multiple input sources;

How to make a string scan instead of file scanning

10. End-of-file rules)

Special rules for matching input

11. Other related macro definitions

Summary of macros available in action

12. Users can use

Summary of the value used in the action

13. And YACC interface

Connect the Flex Scanner and YACC Syntax Analyzer

14. Option

Flex command line option and "% option" instruction

15. Considering

How to make your scanner quickly as possible

16. Generate a C scanner

Generate the function of C scanner class (experimental)

17. Not compatibility with LEX and POSIX

The difference between Flex and AT & T Lex and POSIX LEX standards

18. diagnosis

The error generated by Flex (or Flex) is not obvious

19. file

Flex uses files

20. Flex's shortcomings and its bugs

Learn about the problem of flex

twenty one. See

Other documents and related tools

twenty two. Author

Contact information

Flex profile

Flex is a tool for generating a scanner, the scanner identifies the words in the text. Flex reads from a given file or reads from the standard input (when there is no given file), the description of the scanner to generate is described. The format of this description is a pair of regular expressions and C code, called rules. The Output of Flex is a C source program named lex.yy.c, defined a function called YyleX () in lex.yy.c. Lex..y.c can be compiled and linked to the Flex library using the -LFL link option to generate executable files. Execute the file, it analyzes its input, check whether a regular expression is met, as long as it finds one, the corresponding C code will be performed.

Simple example

First introduce some simple example to demonstrate how to use Flex. The following FLEX input specification defines a scanner, and whenever it encounters the string "username", the user's login name is required to replace the string.

%%

UserName Printf ("% s", getLogin ()); By default, any text that matches the scanner is copied to the output, so the scanner's role is to copy its input content to the output, Extend it when you encounter "Username". In this example, there is only one rule in the input. "UserName" is a mode and "Printf" is the action corresponding to this mode. "%%" indicates the beginning of the rule.

Looking at another example:

INT NUM_LINES = 0, Num_Chars = 0;

%%

/ N Num_Lines; Num_Chars;

Num_Chars;

%%

Main ()

{

Yylex ();

Printf ("# of line =% d, # of chars =% d / n",

Num_lines, num_chars;

}

The number of characters and rows in this scanner statistical input (except for the final report statistics, it does not produce other outputs). The first line declares that two global variables, "num_lines" and "num_chars", yylex () and main () defined after the second "%%" can access these two global variables. The scanner has two rules, one matching a wrap ("/ n") If matching, the Num_Lines and Num_Chars are colluded, and another rule matches any character other than the wrapper (by regular expression "."). An example of a slightly complex point:

/ * Scanner for a Toy Pascal-Like Language * /

% {

/ * NEED THIS for the call to atof () BELOW * /

#include

%}

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 * /

("Unrecognized Character:% S / N", YYTEXT);

%%

Main (Argc, Argv)

Int argc;

Char ** argv;

{

Argv, --Argc; / * Skip overprogram name * /

IF (argc> 0)

YYIN = FOPEN (Argv [0], "R"); ELSE

YYIN = stdin;

Yylex ();

}

This example is a prototype for scanners for class Pascal language, which identifies different types of markers (token) and can report what it see.

The next section will explain the details of this example.

转载请注明原文地址:https://www.9cbs.com/read-31470.html

New Post(0)