Science Fair Projects Ideas - Lex programming tool

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Lex programming tool

LEX redirects here, LEX is also used as an abbreviation for Leading edge extensions

Lex is a program that generates lexical analyzers ("scanners"). Lex is commonly used with the yacc parser generator. Lex, originally written by Eric Schmidt and Mike Lesk, is the standard lexical analyzer on UNIX systems, and is included in the POSIX standard. A popular free version of lex is flex, a fast lexical analyzer. Lex reads an input file specifying the lexical analyzer and outputs code implementing the lexer in the C programming language.

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 parts: a definition section, a rules section, and a C code section. Sections are separated by lines that contain only two percent signs: %%

The definition section is the place to define macros using regular expressions, and also to import header files written in C.

The rules section is the most important section; it associates rules to C statements. When lex sees a pattern in its input matching a given rule, it executes the associated C code. Rules are simply regular expressions, probably containing the macros defined in the definition section.

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.

Example flex file

The following is an example input file for the flex version of lex. It recognizes strings of numbers (integers) in the input. Given the input "abc123z.!&*2ghj6", the program will print:

Saw an integer: 123
Saw an integer: 2
Saw an integer: 6
/* 
 * Example lexical analyzer for flex
 *
 * Picks out strings of digits (integers) from the input.
 */

/*** Definition section ***/

%{

/*
 * Some C code to include the C standard I/O library.
 * Everything inside the %{ %} brackets is inserted
 * verbatim into the generated file.
 */
#include <stdio.h>

%}

/* Macros;  regular expressions */
DIGIT       [0-9]
INTEGER     {DIGIT}+

/* This tells flex to read only one input file */
%option noyywrap

%%
    /*
     * Rules section 
     *
     * Comments in this section must be indented
     * so lex won't mistake them for regular expressions.
     */

{INTEGER}   {
                /*
                 * This rule prints integers from the input.
                 * yytext is a string containing the matched text.
                 */
                printf("Saw an integer: %s\n", yytext); 
            }

.           { /* Ignore all other characters. */ }

%%
/*** C Code section ***/

/*
 * The main program.
 *
 * Call the lexer. Quit when done.
 */
int main(void)
{
    /* yyin is where lex reads from. Set it to the standard input. */
    FILE *yyin = stdin;

    /* Call the lexer. */

    yylex();
    return 0;
}

See also: the flex lexical analyser

03-10-2013 05:06:04
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice