flex (fast lexical analyzer generator) is a free software alternative to lex. It is frequently used with the free Bisonparser generator. Flex was originally written in C by Vern Paxson around 1987.
A similar lexical scanner for C++ is flex++, which is included as part of the flex package.
Flex is a non-GNU project, but the GNU project developed the manual for Flex.
Example lexical analyzer
This is an example of a scanner (written in C) for the instructional programming language PL/0.
External variables used:FILE *source /* The source file */int cur_line, cur_col, err_line, err_col /* For error reporting */int num /* Last number read stored here, for the parser */char id [] /* Last identifier read stored here, for the parser */Hashtab *keywords /* List of keywords */
External routines called:error(const char msg [] ) /* Report an error */Hashtab *create_htab(int estimate) /* Create a lookup table */int enter_htab(Hashtab *ht, char name [] , void *data) /* Add an entry to a lookup table */Entry *find_htab(Hashtab *ht, char *s) /* Find an entry in a lookup table */void *get_htab_data(Entry *entry) /* Returns data from a lookup table */FILE *fopen(char fn [] , char mode [] ) /* Opens a file for reading */fgetc(FILE *stream) /* Read the next character from a stream */ungetc(int ch, FILE *stream) /* Put-back a character onto a stream */isdigit(int ch), isalpha(int ch), isalnum(int ch) /* Character classification */
External types:Symbol /* An enumerated type of all the symbols in the PL/0 language */Hashtab /* Represents a lookup table */Entry /* Represents an entry in the lookup table */
Scanning is started by calling init_scan, passing the name of the source file. If the source file is successfully opened, the parser calls getsym repeatedly to return successive symbols from the source file.
The heart of the scanner, getsym, should be straightforward. First, whitespace is skipped. Then the retrieved character is classified. If the character represents a multiple-character symbol, additional processing must be done. Numbers are converted to internal form, and identifiers are checked to see if they represent a keyword.
int read_ch(void) { int ch = fgetc(source); cur_col++; if (ch = '
') { cur_line++; cur_col = 0; } return ch;}
Flex — may refer to: * Flexible electrical cableIn computing*Adobe Flex, technologies for developing rich internet applications *Flex, a family of automatic test equipment produced by Teradyne *Flex lexical analyser, a free software alternative to Lex… … Wikipedia
Lex programming tool — In computer science, lex is a program that generates lexical analyzers ( scanners or lexers ). Lex is commonly used with the yacc parser generator. Lex, originally written by Eric Schmidt and Mike Lesk, is the standard lexical analyzer generator… … Wikipedia
Yacc — The computer program yacc is a parser generator developed by Stephen C. Johnson at AT T for the Unix operating system. The name is an acronym for Yet Another Compiler Compiler. It generates a parser (the part of a compiler that tries to make… … Wikipedia
List of open source software packages — This is a list of open source software packages: computer software licensed under an open source license. Software that fits the Free software definition may be more appropriately called free software; the GNU project in particular objects to… … Wikipedia
FleXML — is an XML transformation language originally developed by Kristofer Rose. It allows a programmer to specify actions in C programming language or C++, and associate those actions with element definitions in an XML DTD. It is similar in philosophy… … Wikipedia
List of free and open source software packages — This article is about software free to be modified and distributed. For examples of software free in the monetary sense, see List of freeware. This is a list of free and open source software packages: computer software licensed under free… … Wikipedia
Quex — Infobox Software name = quex developer = Frank Rene Schäfer latest release version = 0.23.8 latest release date = March 6, 2008 operating system = Cross platform genre = Lexical analyzer generator license = LGPL (with military use exclusion)… … Wikipedia
GNU bison — Infobox Software name = GNU Bison developer = The GNU Project latest release version = 2.3 latest release date = June 5, 2006 operating system = Cross platform genre = Parser generator license = GPL website = [http://www.gnu.org/software/bison/… … Wikipedia
Compilateur — Pour les articles homonymes, voir Compilation. Un compilateur est un programme informatique qui traduit un langage (appelé le langage source) en un autre (le langage cible), généralement dans le but de créer un exécutable. Un compilateur sert le… … Wikipédia en Français
GNU Bison — Pour les articles homonymes, voir Bison (homonymie). GNU Bison … Wikipédia en Français