词条 | Lex (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 sourceThough 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 fileThe 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
Example of a Lex fileThe 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. %{ /* C code to be copied verbatim */
%} /* 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 the program will print: Saw an integer: 123 Saw an integer: 2 Saw an integer: 6 Using Lex with other programming toolsUsing Lex with parser generatorsLex 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 makemake 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
References1. ^{{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
5 : Compiling tools|Parser generators|Unix programming tools|Unix SUS2008 utilities|Finite automata |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。