| advertise add site services publishers database health videos | ![]() | about toolbar stats live show health store more stuff JOIN/LOGIN |
Tools, Buy Tools, Tool Suppliers, Automotive Tools, thetrainingstationinc.com | Dental Supply | Dental Instruments | Dental Tools | Sales Agent Program britesources.com |
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".
[edit] Structure of a lex fileThe 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
[edit] Example of a lex fileThe 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 [edit] Using Lex with other programming tools[edit] Using Lex with YaccLex 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. [edit] Lex and makemake is a utility that can be used to maintain programs involving lex. Make assumes that a file that has an extension of [edit] References
[edit] See also |
| ↑ top of page ↑ | about thumbshots |