Home  

Random  

Nearby  



Log in  



Settings  



Donate  



About Wikipedia  

Disclaimers  



Wikipedia





Flex (lexical analyser generator)





Article  

Talk  



Language  

Watch  

Edit  





Flex (fast lexical analyzer generator) is a free and open-source software alternative to lex.[2] It is a computer program that generates lexical analyzers (also known as "scanners" or "lexers").[3][4] It is frequently used as the lex implementation together with Berkeley Yacc parser generatoronBSD-derived operating systems (as both lex and yacc are part of POSIX),[5][6][7] or together with GNU bison (a version of yacc) in *BSD ports[8] and in Linux distributions. Unlike Bison, flex is not part of the GNU Project and is not released under the GNU General Public License,[9] although a manual for Flex was produced and published by the Free Software Foundation.[10]

flex
Developer(s)Vern Paxson
Initial releasearound 1987; 37 years ago (1987)[1]
Stable release

2.6.4 / May 6, 2017; 7 years ago (2017-05-06)

Repository
Operating systemUnix-like
TypeLexical analyzer generator
LicenseBSD license
Websitegithub.com/westes/flex

History

edit

Flex was written in C around 1987[1]byVern Paxson, with the help of many ideas and much inspiration from Van Jacobson. Original version by Jef Poskanzer. The fast table representation is a partial implementation of a design done by Van Jacobson. The implementation was done by Kevin Gong and Vern Paxson.[11]

Example lexical analyzer

edit

This is an example of a Flex scanner for the instructional programming language PL/0.

The tokens recognized are: '+', '-', '*', '/', '=', '(', ')', ',', ';', '.', ':=', '<', '<=', '<>', '>', '>='; numbers: 0-9 {0-9}; identifiers: a-zA-Z {a-zA-Z0-9} and keywords: begin, call, const, do, end, if, odd, procedure, then, var, while.

%{
#include "y.tab.h"
%}

digit         [0-9]
letter        [a-zA-Z]

%%
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"."                  { return PERIOD;     }
":="                 { return BECOMES;    }
"="                  { return EQL;        }
"<>"                 { return NEQ;        }
"<"                  { return LSS;        }
">"                  { return GTR;        }
"<="                 { return LEQ;        }
">="                 { return GEQ;        }
"begin"              { return BEGINSYM;   }
"call"               { return CALLSYM;    }
"const"              { return CONSTSYM;   }
"do"                 { return DOSYM;      }
"end"                { return ENDSYM;     }
"if"                 { return IFSYM;      }
"odd"                { return ODDSYM;     }
"procedure"          { return PROCSYM;    }
"then"               { return THENSYM;    }
"var"                { return VARSYM;     }
"while"              { return WHILESYM;   }
{letter}({letter}|{digit})* {
                       yylval.id = strdup(yytext);
                       return IDENT;      }
{digit}+             { yylval.num = atoi(yytext);
                       return NUMBER;     }
[ \t\n\r]            /* skip whitespace */
.                    { printf("Unknown character [%c]\n",yytext[0]);
                       return UNKNOWN;    }
%%

int yywrap(void){return 1;}

Internals

edit

These programs perform character parsing and tokenizing via the use of a deterministic finite automaton (DFA). A DFA is a theoretical machine accepting regular languages. These machines are a subset of the collection of Turing machines. DFAs are equivalent to read-only right moving Turing machines. The syntax is based on the use of regular expressions. See also nondeterministic finite automaton.

Issues

edit

Time complexity

edit

A Flex lexical analyzer usually has time complexity   in the length of the input. That is, it performs a constant number of operations for each input symbol. This constant is quite low: GCC generates 12 instructions for the DFA match loop.[citation needed] Note that the constant is independent of the length of the token, the length of the regular expression and the size of the DFA.

However, using the REJECT macro in a scanner with the potential to match extremely long tokens can cause Flex to generate a scanner with non-linear performance. This feature is optional. In this case, the programmer has explicitly told Flex to "go back and try again" after it has already matched some input. This will cause the DFA to backtrack to find other accept states. The REJECT feature is not enabled by default, and because of its performance implications its use is discouraged in the Flex manual.[12]

Reentrancy

edit

By default the scanner generated by Flex is not reentrant. This can cause serious problems for programs that use the generated scanner from different threads. To overcome this issue there are options that Flex provides in order to achieve reentrancy. A detailed description of these options can be found in the Flex manual.[13]

Usage under non-Unix environments

edit

Normally the generated scanner contains references to the unistd.h header file, which is Unix specific. To avoid generating code that includes unistd.h, %option nounistd should be used. Another issue is the call to isatty (a Unix library function), which can be found in the generated code. The %option never-interactive forces flex to generate code that does not use isatty.[14]

Using flex from other languages

edit

Flex can only generate code for C and C++. To use the scanner code generated by flex from other languages a language binding tool such as SWIG can be used.

Unicode support

edit

Flex is limited to matching 1-byte (8-bit) binary values and therefore does not support Unicode.[15] RE/flex and other alternatives do support Unicode matching.

Flex++

edit

flex++ is a similar lexical scanner for C++ which is included as part of the flex package. The generated code does not depend on any runtime or external library except for a memory allocator (malloc or a user-supplied alternative) unless the input also depends on it. This can be useful in embedded and similar situations where traditional operating systemorC runtime facilities may not be available.

The flex++ generated C++ scanner includes the header file FlexLexer.h, which defines the interfaces of the two C++ generated classes.

See also

edit

References

edit
  1. ^ a b Levine, John (August 2009). flex & bison. O'Reilly Media. p. 9. ISBN 978-0-596-15597-1. In about 1987, Vern Paxson of the Lawrence Berkeley Lab took a version of lex written in ratfor (an extended Fortran popular at the time) and translated it into C, calling it flex, for 'Fast Lexical Analyzer Generator.'
  • ^ Levine, John R.; Mason, Tony; Brown, Doug (1992). lex & yacc (2nd ed.). O'Reilly. p. 279. ISBN 1-56592-000-7. A freely available version of lex is flex.
  • ^ Levine, John R.; Mason, Tony; Brown, Doug (1992). lex & yacc (2nd ed.). O'Reilly. pp. 1–2. ISBN 1-56592-000-7.
  • ^ Levine, John (August 2009). flex & bison. O'Reilly Media. p. 304. ISBN 978-0-596-15597-1.
  • ^ OpenBSD (2015-12-11). "src/usr.bin/lex/". BSD Cross Reference. Retrieved 2015-12-26. This is flex, the fast lexical analyzer generator.
  • ^ "flex(1)". *BSD man pages.
  • ^ "yacc(1)". *BSD man pages.
  • ^ "bison-3.0.4 – GNU parser generator". OpenBSD ports. 2015-11-15. Retrieved 2015-12-26.
  • ^ Is flex GNU or not? Archived 2016-03-03 at the Wayback Machine, flex FAQ
  • ^ "Flex - a scanner generator - Table of Contents - GNU Project - Free Software Foundation (FSF)". ftp.gnu.org. Retrieved 2019-12-05.
  • ^ "Flex, version 2.5 A fast scanner generator Edition 2.5, March 1995". Retrieved 20 April 2019.
  • ^ "Performance - Lexical Analysis With Flex, for Flex 2.5.37". Flex.sourceforge.net. Archived from the original on 2014-01-27. Retrieved 2013-02-25.
  • ^ "Reentrant - Lexical Analysis With Flex, for Flex 2.5.37". Flex.sourceforge.net. Archived from the original on 2010-11-17. Retrieved 2013-02-25.
  • ^ "Code-Level And API Options - Lexical Analysis With Flex, for Flex 2.5.37". Flex.sourceforge.net. Archived from the original on 2013-03-14. Retrieved 2013-02-25.
  • ^ Tomassetti, Gabriele (2020-03-04). "Why you should not use (f)lex, yacc and bison". Strumenta. Retrieved 2022-10-26.
  • Further reading

    edit
    edit

    Retrieved from "https://en.wikipedia.org/w/index.php?title=Flex_(lexical_analyser_generator)&oldid=1123489826"
     



    Last edited on 24 November 2022, at 01:07  





    Languages

     


    Čeština
    فارسی
    Français

    Italiano
    Norsk bokmål
    Polski
    Русский
    Српски / srpski
    Українська

     

    Wikipedia


    This page was last edited on 24 November 2022, at 01:07 (UTC).

    Content is available under CC BY-SA 4.0 unless otherwise noted.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Terms of Use

    Desktop