Access the NEW Basecamp Support Portal

Lex (software)

« Back to Glossary Index

Open source and Versions
– Some versions of Lex are now open-source.
– Open-source versions of Lex are distributed with open-source operating systems like OpenSolaris and Plan 9 from Bell Labs.
– The popular open-source version of Lex is called flex.
– Flex is not derived from proprietary coding.
– Flex is known as the fast lexical analyzer.

Structure of a Lex file
– Lex files are divided into three sections: definitions, rules, and C code.
– The definitions section defines macros and imports header files written in C.
– C code can be written in the definitions section and will be copied into the generated source file.
– The rules section associates regular expression patterns with C statements.
– When the lexer sees text matching a pattern, it executes the associated C code.

Example of a Lex file
– An example Lex file for the flex version of Lex recognizes strings of numbers and prints them out.
– The file has a definitions section, a rules section, and a C code section.
– The rules section uses regular expressions to match strings of digits.
– The C code section contains the main function that calls the lexer.
– The input given to flex will be converted into a C file called lex.yy.c.

Using Lex with other programming tools
– Lex can be used with parser generators like Yacc or Bison.
– Lex limits rules to those describable by regular expressions.
– Parser generators use a formal grammar to parse an input stream.
– Lex is often used to produce a token-stream for parser generators.
– Scannerless parsing refers to parsing the input character-stream directly, without a distinct lexer.

Lex and make
– Make is a utility used to maintain programs involving Lex.
– Make assumes that a file with the extension ‘.l’ is a Lex source file.
– The make internal macro LFLAGS can be used to specify Lex options for make.
– Lex can be integrated into the make workflow for automatic compilation.
– Make simplifies the build process for Lex programs.

Lex (software) (Wikipedia)

Lex is a computer program that generates lexical analyzers ("scanners" or "lexers").

Original author(s)Mike Lesk, Eric Schmidt
Initial release1975; 49 years ago (1975)
Written inC
Operating systemUnix, Unix-like, Plan 9
LicensePlan 9: MIT License

Lex is commonly used with the yacc parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975, is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard.

Lex reads an input stream specifying the lexical analyzer and writes source code which implements the lexical analyzer in the C programming language.

In addition to C, some old versions of Lex could generate a lexer in Ratfor.

« Back to Glossary Index

Request an article

Please let us know what you were looking for and our team will not only create the article but we'll also email you to let you know as soon as it's been published.
Most articles take 1-2 business days to research, write, and publish.
Content/Article Request Form

Submit your RFP

We can't wait to read about your project. Use the form below to submit your RFP!
Request for Proposal

Contact and Business Information

Provide details about how we can contact you and your business.

Quote Request Details

Provide some information about why you'd like a quote.