|
lex is a program that generates lexical analyzers ("scanners" or "lexers"). 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 Unix systems, and is included in the POSIX standard. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The terms computer program, software program, applications program, system software, or just program are used to refer to either an executable program by both lay people and computer programmers or the collection of source code from which an executable program is created (eg, compiled). ...
Lexical analysis is the processing of an input sequence of characters (such as the source code of a computer program) to produce, as output, a sequence of symbols called lexical tokens, or just tokens. For example, lexers for many programming languages convert the character sequence 123 abc into two tokens...
Yacc is a piece of computer software that serves as the standard parser generator on Unix systems. ...
A compiler-compiler or parser generator is a utility for generating the source code of a parser, interpreter or compiler from an annotated language description in the form of a grammar (usually in BNF) plus code that is associated with each of the rules of the grammar that should be...
Dr. Eric E. Schmidt is Chairman and CEO of Google Inc. ...
Mike Lesk is a computer program writer who created the lex programming tool, a program for Unix computers, with Eric E. Schmidt. ...
Unix or UNIX is a computer operating system originally developed in the 1960s and 1970s by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, and Douglas McIlroy. ...
To meet Wikipedias quality standards, this article or section may require cleanup. ...
In computing, the term stream is used in a number of ways, in all cases referring to a succession of data elements made available over time. ...
Source code (commonly just source or code) is any series of statements written in some human-readable computer programming language. ...
Wikibooks has a book on the topic of C Programming The C programming language (often, just C) is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ...
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". Open source refers to projects that are open to the public and which draw on other projects that are freely available to the general public. ...
OpenSolaris is an open source project created by Sun Microsystems to build a developer community around the Solaris Operating System technology. ...
To meet Wikipedias quality standards, this article or section may require cleanup. ...
Open source refers to projects that are open to the public and which draw on other projects that are freely available to the general public. ...
The Flex lexical analyser generator is a free software alternative to Lex. ...
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.
A macro (short for macrodefinition[citation needed]) in computer science is an abstraction, that defines how a certain input pattern is replaced by an output pattern according to a defined set of rules. ...
This article or section may be confusing or unclear for some readers, and should be edited to rectify this. ...
Wikibooks has a book on the topic of C Programming The C programming language (often, just C) is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ...
The term statement can have several meanings: In programming, a statement is an instruction to execute something that will not return a value. ...
In computing, a regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. ...
In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ...
A diagram of the operation of a typical multi-language, multi-target compiler. ...
Example flex 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. The Flex lexical analyser generator is a free software alternative to Lex. ...
/*** 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: %sn", 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 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. Yacc is a piece of computer software that serves as the standard parser generator on Unix systems. ...
In computer science a formal grammar is an abstract structure that describes a formal language precisely, i. ...
In computing, a regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. ...
Fig. ...
See also |