Lex/flex notes

Source: Internet
Author: User

Lex's matching strategy:

1. Determine the selected word by the longest matching principle

2. If a string can be matched by a number of regular matches, the first match is preceded by the normal type.

Lex source program: Lex source program must be written according to the Lex language specification, the core of which is a set of lexical rules (regular type). In general, a Lex source program is divided into three parts and three parts separated by a symbol.

Defining segments

%%

Paragraph of the statute of words

%%

Auxiliary function segment

The variables and functions commonly used in the Lex source program:

Yyin and Yyout: This is the input and output file pointers that are defined in Lex itself. These two variables indicate where the lexical parser generated by Lex obtains input and output. Default: Keyboard input, screen output.

Yytext and Yyleng: This is also a variable that has been defined in Lex, and it can be used directly.

Yytext: Pointer to the currently recognized lexical unit (word text) Yyleng: The length of the current lexical unit.

A predefined macro in the Echo:lex can appear in the action, which is equivalent to fprintf (Yyout, "%s", Yytext), which is the output of the currently matched lexical unit.

Yylex (): The parser driver, generated by the Lex translator, must contain this function within the LEX.YY.C.

Yywrap (): The lexical analyzer calls Yywrap () when it encounters the end of the file to decide what to do next: if Yywrap () returns 0, the scan continues to return 1, returning the 0 tag at the end of the report file.

1. Compile the Lex source program command with the Lex translator (assuming FILENAME.L is the lex source program name): Flex FILENAME.L

2. Compile the C source program generated by the Lex translator with the GCC compiler (the C source program name generated by the Lex translator is fixed to lex.yy.c): gcc [-o outfile] Lex.yy.c–lfl

Where-LFL is a library function that links flex, the library function may contain standard functions similar to the Yywrap class. -O outfile is an optional compilation option that names the executable program generated by the compilation as outfile, and if the compilation option is not written, the executable program named A.exe (which is actually a.out under Linux) is generated by default.

3. The main function of the call to the lexical parser Yylex () can be written in the auxiliary functions section of the lex source program, or in other C files. If the main function is written in main.c, the compile-time link will need to be compiled with LEX.YY.C, that is, the compile link command is: gcc [-o outfile] lex.yy.c MAIN.C–LFL

1%{2 /***********3 Sexy Lexy4 ***********/5%}6%%7a+b*a {printf ("1%s\n", Yytext);}8(AB) +c? {printf ("2%s\n", Yytext);}9AA {printf ("3%s\n", Yytext);}Ten(a|b) *c {printf ("4%s\n", Yytext);} One%% A intMain () { -Yyin =stdin; -     returnYylex (); the}

Ababcbacaabaababaa
2ababc
4bac
1aaba
2abab
1aa

Lex/flex notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.