Lex (software)

Lex The program is used in the compiler to create scanner for the lexical analysis of source code. A lexical scanner is part of a translator.

Lex is often used in combination with Yacc, which is responsible for parsing.

Lex mid-1970s has been programmed at Bell Labs by Mike Lesk in C; the regex treatment came from Al Aho and Ken Thompson. In the summer of 1976 the program was re-implemented by the former Bell Labs intern Eric Schmidt.

Operation

So Lex can generate an analysis program, a description file must be created. In this file, called tokens are defined by regular expressions.

Here is an example of such a file:

% {     # include " y.tab.h "     extern int yylval; %}     % %     "=" {Return EQ; }     "! =" {Return NE; }     " " {Return PLUS; }     "-" {Return MINUS; }     ";" {Return SEMI COLON; }     "print" {return PRINT; }     [0-9 ] { yylval = atoi ( yytext ); return NUMBER; }     ...     # endif The resulting analysis program reads the source code of the program to be compiled and divides it into tokens. If this is not possible, there is a lexical error. The tokens are then passed to the syntactic analysis part or program of a translator.

Example

For a sample source code as

Print 15 5; are the tokens:

It is important to note that no knowledge of permitted Lex syntax. Specifically, this means that the sample code

15 print; 5 would also be transferred in the same token (but in a different order ).

510062
de