Flex with C ++ Getting Started

xiaoxiao2021-03-06  42

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 **** this program is free software; you can redistribute it and / or modify ** it under the Terms of the gnu general public License as published by ** the free software Foundation; Either Version 2 of the license, or ** (at Your option) Any Later Version. **** This Program Is Distributed in The Hope That IT Will Be Useful, ** But WITHOUT ANY WARRANTY;.. without even the implied warranty of ** MERCHANTABILITY or FITNESS fOR A PARTICULAR PURPOSE See the ** GNU General Public License for more details ******* / # if HAVE_CONFIG_H # include # Endif # include #include extern int num_lines, num_chars; int main (int Argc, char ** argv) {if (argc == 1) {Printf ("usage:% s File] ", argv [0]); return 1;} ifstream * PMYFILE = New ifstream (); PMYFILE-> Open (argv [1], ios :: out); if (PMYFILE-> Fail ()) {Printf ("Error While Open THE File"; Return 1;} FlexLexer * Lexer = New YYFLEXLEXER (PMYFILE); while (lexer-> yylex ()! = 0); Printf ("Total Lines:% D / NTOTAL CHARS:% D / N ", Num_Lines, Num_Chars; delete pmyfile; delete lexer; return 0;} FlexLexer is an abstract base class that is defined in .

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 , implement Virtual Void Lexeroutput (const char * buf, int size) in lex.yy.cc, intend The program is written to the output stream.

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

New Post(0)