Flex With C Getting Started with "Compilation Principle" is not strange to Flex. Flex - Fast Lexical Analyzer Generator, this is the definition of GNU, it means that flex is FAST Lex, but is it better than the Lex of AT & T? I do not know. First, let's start our first example, we need to count how many lines have a document, how many characters. But I don't want to write a function yourself, so good, let Flex help me write this, this is a C example, and the example of C is easy to find online. C is slightly troublesome. Because even in the latest version of Flex, C Standrad is also in the development, so because of the problem of Namespace STD, it will encounter some trouble when compiling. First, use acmkdir to create a directory, named Test, acmkdir, please refer to my previous article "Develop C Applications with GNU Development Tool Suite" (http://www.blogcn.com/user5/snnn/blog / 4919807.html). Then write Test.L% {Int num_lines = 0, num_chars = 0;%} %% / n Num_Lines; Num_Chars; Num_Chars; %% This file is divided into three parts front The variable declarations, statements, etc., or directly "#include" something "in the middle of the C / C format, or directly" #include "Something" in the middle, %% is the mode definition, use the regular expression Match the left pattern, if you match, execute the statement on the right. Finally, you can also write your own C / C function after %%, such as, you can put a main () {} and generate it with Flex Flex - Test.l Note, parameter - represents the source file (lex.yy.cc) in the C format, not a C file (lex.yy.c) find a dean of a dean to help me write code, go to drink How is the cup of coffee? Still calculated, I found it when I got up, it generated a 31734 byte source file only with a few MS, if this file is going to write, don't open Lex .y.cc to see? More than 1500 lines, still count, believe it, it will not be wrong.
Then create a CPP file, this is my test.cpp / *! / File test.cpp * / brief Test.cpp * / date 30 nov 2004 22:05:31 * / author snnn119 @ hotmail .. Com * // *** Copyright (c) 2004 Cyper Sun
Below I simply introduce these two classes ---------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------------------ Class FlexLexer: This is an abstract class const char * yytext () returns the text of the most max, the Equiva - LENT OF YYTEXT. Returns the last match, YYTEXTINT YYLENG () Returns the length of the mostleng () Returns the length of the mostleng () Returns the length of the mostley () Returng. Returns the length of the most recently match, YYLENGINT LINENO () under the C format Const Returns The Current Input Line Number (See% Option Yylineno), or 1 IF% Option Yylineno WAS NOT USED. Returns the last match line number, or 1, if you close this in Option. Yylineno under the C format and some member function, such as yy_create_buffer (), yy_flush_buffer (), yy_delete_buffer (), yyrestart (), etc., please read the source code. (sorry, there is no doc any more). --------------------------------------- -------------------------------------------------- ------------------------- Class YYFLEXLEXER: Inherited from FlexLexeryyflexleXer (istream * arg_yyin = 0, ostream * arg_youth = 0 This is its constructor, Instead of an ISTREAM * arg_yyn instead of the previous YYIN work, OSTREAM * arg_yyout is output. This means that its input and output can also be a variety of diverse, file, cin, etc. if you don't specify, the default input and The output stream is still CIN and COUTVIRTUAL INT YYLEX () to analyze the input, defined in