Flex: A fast Lexical Analyzer Generator - PowerPoint PPT Presentation

About This Presentation
Title:

Flex: A fast Lexical Analyzer Generator

Description:

... source file, Flex generates an output C source code file ... Flex Source File (sample.l) Flex Compiler (Flex) Lexical Analyzer Code (lex.yy.c) C Compiler ... – PowerPoint PPT presentation

Number of Views:1056
Avg rating:3.0/5.0
Slides: 11
Provided by: spndurg
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Flex: A fast Lexical Analyzer Generator


1
Flex A fast Lexical Analyzer Generator
  • CSE470 Spring 2000
  • Updated by Prasad

2
Motivation
  • Use Flex to perform Text Processing
  • In structured programming two tasks occur
  • repeatedly
  • Lexical Analysis Dividing the input into
    meaningful units. For a C program the units are
    variables, constants, keywords, operators,
    punctuation etc. These units also called as
    tokens. (Use Flex)
  • Parsing Involves finding the relationship
    between input tokens. For a C program, one needs
    to identify valid expressions, statements,
    blocks, procedures etc. (Use Yacc or Bison)

3
An Example
  • Recognizing all keywords of a Language. Probably
    also want to identify and remove comments.
  • / The following loop computes exponent of
  • number /
  • int i, number, result
  • result 1
  • for (i0 iltpower i)
  • result result number

4
About Flex
  • Flex Fast Lexical Analyzer Generator.
  • Produces Lexical Analyzers in a fast and easy
    manner.
  • Given a Flex source file, Flex generates an
    output C source code file lex.yy.c which defines
    the scanning routine yylex().
  • Flex source file (say sample.l) should contain
    rules for identifying tokens in the input.

5
Flex Source File (sample.l)
Flex Compiler (Flex)
Lexical Analyzer Code (lex.yy.c)
C Compiler
Lexical Analyzer executable
Input Text File
Output Tokens
Parser
6
Flex Source
  • Consists of three sections definitions, rules
    and user-defined routines.

Declaration Section Definitions
section Rules Section User routines Section
7
Rules
  • Format
  • ltregular-expressiongt ltactionsgt
  • When a Lexical Analyzer is run (or the scanning
    routine yylex() is called) it analyzes the input
    for the occurrences of text patterns that match
    the regular expressions. When ever it finds one
    it executes the corresponding action.
  • Flex stores the matched text in a global string
    variable called yytext.

8
Examples
  • 0-9 printf(An integer s \n, yytext)
  • a-za-z0-9 printf(An identifier s \n,
    yytext)
  • if then begin end function printf(A
    Keyword s \n, yytext)

9
Example to count lines
  • int linecount 0
  • Digit 0-9
  • Identifier a-zA-Za-zA-Z0-9
  • Identifier printf(s This is an
    identifier\n, yytext)
  • \n printf(The line number is
    d\n, linecount)
  • \t /Ignore spaces /
  • . printf(Unrecognized
    character\n)
  • main()
  • yylex()

10
Example to remove comments
  • x comment
  • "/" BEGIN(comment)
  • ltcommentgt\n / eat anything that's not a
    '' /
  • ltcommentgt""/\n / eat up ''s not
    followed by '/'s /
  • ltcommentgt\n
  • ltcommentgt"""/" BEGIN(0)
Write a Comment
User Comments (0)
About PowerShow.com