Share on Facebook Share on Twitter Email
Answers.com

lex

 
Dictionary: lex   (lĕks) pronunciation
n., pl., le·ges ('jēz').
Law.

[Latin lēx, lēg-.]


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics

(Yet Another Compiler Compiler) A Unix compiler used to create a compiler. The operators, variables and constants of the program are typically defined in C using lex (LEXical analyzer), which converts them into preprocessed, machine-readable tokens for yacc. The grammar of the new language is written in C and compiled in yacc. The combination of lex (define elements) and yacc (define actions) creates a new compiler. For more information, visit www.epaperpress.com. See bison.

Download Computer Desktop Encyclopedia to your iPhone/iTouch

Thesaurus: lex
Top

noun

    The formal product of a legislative or judicial body: act, assize, bill1, enactment, law, legislation, measure, statute. See law.

This entry contains information applicable to United States law only.

[Latin, Law.] In medieval jurisprudence, a body or collection of various laws peculiar to a given nation or people; not a code in the modern sense, but an aggregation or collection of laws not codified or systematized. Also, a similar collection of laws relating to a general subject, and not peculiar to any one people.

In modern U.S. and English jurisprudence this term signifies a system or body of laws, written or unwritten, applicable to a particular case or question regarded as local or unique to a particular state, country, or jurisdiction.

Wikipedia: Lex (software)
Top

In computer science, lex is a program that generates lexical analyzers ("scanners" or "lexers").[1] Lex is commonly used with the yacc parser generator. Lex, originally written by Eric Schmidt and Mike Lesk, is the standard lexical analyzer generator on many Unix systems, and a tool exhibiting its behavior is specified as part of the POSIX standard.

Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

Though traditionally proprietary software, versions of Lex based on the original AT&T code are available as open source, as part of systems such as OpenSolaris and Plan 9 from Bell Labs. Another popular open source version of Lex is Flex, the "fast lexical analyzer".

Contents

Structure of a lex file

The structure of a lex file is intentionally similar to that of a yacc file; files are divided up into three sections, separated by lines that contain only two percent signs, as follows:

Definition section
%%
Rules section
%%
C code section
  • The definition section is the place to define macros and to import header files written in C. It is also possible to write any C code here, which will be copied verbatim into the generated source file.
  • The rules section is the most important section; it associates patterns with C statements. Patterns are simply regular expressions. When the lexer sees some text in the input matching a given pattern, it executes the associated C code. This is the basis of how lex operates.
  • The C code section contains C statements and functions that are copied verbatim to the generated source file. These statements presumably contain code called by the rules in the rules section. In large programs it is more convenient to place this code in a separate file and link it in at compile time.

Example of a lex file

The following is an example lex file for the flex version of lex. It recognizes strings of numbers (integers) in the input, and simply prints them out.

/*** Definition section ***/

%{
/* C code to be copied verbatim */
#include <stdio.h>
%}

/* This tells flex to read only one input file */
%option noyywrap

%%
    /*** Rules section ***/

    /* [0-9]+ matches a string of one or more digits */
[0-9]+  {
            /* yytext is a string containing the matched text. */
            printf("Saw an integer: %s\n", yytext);
        }

.       {   /* Ignore all other characters. */   }

%%
/*** C Code section ***/

int main(void)
{
    /* Call the lexer, then quit. */
    yylex();
    return 0;
}

If this input is given to flex, it will be converted into a C file, lex.yy.c. This can be compiled into an executable which matches and outputs strings of integers. For example, given the input:

abc123z.!&*2ghj6

the program will print:

Saw an integer: 123
Saw an integer: 2
Saw an integer: 6

Using Lex with other programming tools

Using Lex with Yacc

Lex and Yacc (a parser generator) are commonly used together. Yacc uses a formal grammar to parse an input stream, something which Lex cannot do using simple regular expressions (Lex is limited to simple finite state automata). However, Yacc cannot read from a simple input stream - it requires a series of tokens. Lex is often used to provide Yacc with these tokens.

Lex and make

make is a utility that can be used to maintain programs involving lex. Make assumes that a file that has an extension of .l is a lex source file. The make internal macro LFLAGS can be used to specify lex options to be invoked automatically by make.[2]

References

  1. ^ Levine, John R; Mason, Tony; Brown, Doug (1992). LEX & YACC (2 ed.). O'Reilly. pp. 1-2. ISBN 1-56592-000-7. http://books.google.co.in/books?id=YrzpxNYegEkC&printsec=frontcover#PPA1,M1. 
  2. ^ "make", The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition (The IEEE and The Open Group), 2004, http://www.opengroup.org/onlinepubs/009695399/utilities/make.html 

See also


Shopping: lex
Top
 
 

 

Copyrights:

Dictionary. The American Heritage® Dictionary of the English Language, Fourth Edition Copyright © 2007, 2000 by Houghton Mifflin Company. Updated in 2009. Published by Houghton Mifflin Company. All rights reserved.  Read more
Computer Desktop Encyclopedia. THIS COPYRIGHTED DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2009 Computer Language Company Inc.  All rights reserved.  Read more
Thesaurus. Roget's II: The New Thesaurus, Third Edition by the Editors of the American Heritage® Dictionary Copyright © 1995 by Houghton Mifflin Company. Published by Houghton Mifflin Company. All rights reserved.  Read more
Law Encyclopedia. West's Encyclopedia of American Law. Copyright © 1998 by The Gale Group, Inc. All rights reserved.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Lex (software)" Read more