请输入您要查询的百科知识:

 

词条 Lex (software)
释义

  1. Open source

  2. Structure of a Lex file

  3. Example of a Lex file

  4. Using Lex with other programming tools

     Using Lex with parser generators  Lex and make 

  5. See also

  6. References

  7. External links

{{Infobox Software
| name = Lex
| logo =
| screenshot =
| screenshot size =
| caption =
| author = Mike Lesk, Eric Schmidt
| developer =
| released = {{Start date and age|1975}}
| latest release version =
| latest release date =
| operating system = Unix and Unix-like
| genre = Command
| license =
| website =
}}Lex is a computer program that generates lexical analyzers ("scanners" or "lexers").[1][2]

Lex is commonly used with the yacc parser generator. Lex, originally written by Mike Lesk and Eric Schmidt[3] and described in 1975,[4][5] is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard.{{Citation needed|date=July 2010}}

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

Open source

Though originally distributed as proprietary software, some versions of Lex are now open source. Open source versions of Lex, based on the original AT&T code are now distributed as a part of open source operating systems such as OpenSolaris and Plan 9 from Bell Labs.{{clarify|date=February 2017|reason=This sentence needs to be more clear as it. seems to be more confusing than being informative}} One popular open source version of Lex, called flex, or the "fast lexical analyzer", is not derived from proprietary coding.

Structure of a Lex file

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

  • The definition section defines macros and imports 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 associates regular expression patterns with C statements. When the lexer sees text in the input matching a given pattern, it will execute the associated C code.
  • 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 linked 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 (positive integers) in the input, and simply prints them out.

/*** Definition section ***/

%{

/* C code to be copied verbatim */

  1. include

%}

/* This tells flex to read only one input file */

%option noyywrap

%%

[0-9]+ {

            /* yytext is a string containing the matched text. */            printf("Saw an integer: %s\", 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, {{nowrap | lex.yy.c}}. This can be compiled into an executable which matches and outputs strings of integers. For example, given the input:

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 parser generators

Lex and parser generators, such as Yacc or Bison, are commonly used together. Parser generators use 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). {{Clarify|reason=mixes together grammars, regular expressions and FSMs into a confusing result|date=May 2012}}

It is typically preferable to have a (Yacc-generated, say) parser be fed a token-stream as input, rather than having it consume the input character-stream directly. Lex is often used to produce such a token-stream.

Scannerless parsing refers to parsing the input character-stream directly, without a distinct lexer.

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.[6]

See also

  • Flex lexical analyser
  • Yacc
  • Ragel
  • PLY (Python Lex-Yacc)
  • Comparison of parser generators

References

1. ^{{cite book |last1=Levine |first1=John R. | authorlink1=John R. Levine |last2=Mason |first2=Tony |last3=Brown |first3=Doug |title=lex & yacc |pages=1–2 |publisher=O'Reilly |year=1992 |edition=2 |isbn=1-56592-000-7 |url=https://books.google.com/books?id=YrzpxNYegEkC&printsec=frontcover#PPA1,M1}}
2. ^{{Cite book | last=Levine | first=John | authorlink=John Levine | title =flex & bison | publisher=O'Reilly Media | date=August 2009 | page=304 | url=http://oreilly.com/catalog/9780596155988 | isbn=978-0-596-15597-1}}
3. ^{{cite web|first1=M.E. |last1=Lesk |first2=E. |last2=Schmidt |title=Lex – A Lexical Analyzer Generator|url=http://dinosaur.compilertools.net/lex/index.html|accessdate=August 16, 2010}}
4. ^{{cite web|url=http://epaperpress.com/lexandyacc/download/lex.pdf |title=Lex – A Lexical Analyzer Generator |first1=M.E. |last1=Lesk |first2=E. |last2=Schmidt |date=July 21, 1975| work=UNIX TIME-SHARING SYSTEM:UNIX PROGRAMMER’S MANUAL, Seventh Edition, Volume 2B |publisher=bell-labs.com|accessdate= Dec 20, 2011}}
5. ^{{cite journal |last1=Lesk |first1=M.E. |date=October 1975 |title=Lex – A Lexical Analyzer Generator |journal=Comp. Sci. Tech. Rep. No. 39 |location=Murray Hill, New Jersey |publisher=Bell Laboratories}}
6. ^{{Cite journal |url=http://www.opengroup.org/onlinepubs/009695399/utilities/make.html |title=make |journal=The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition |publisher=The IEEE and The Open Group |year=2004}}

External links

  • Using Flex and Bison at Macworld.com
{{Unix commands}}

5 : Compiling tools|Parser generators|Unix programming tools|Unix SUS2008 utilities|Finite automata

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/17 19:06:20