You are on page 1of 2

1/3/2015

FLEXTutorial

FLEXTutorial
LanGao
[Home][WhatisaScanner?][HowtouseFLEX?][Practice]
[Resources]

WhatisaScanner?
Thescanner performs lexicalanalysis of a certain program (in our case, the Simple
program). It reads the source program as a sequence of characters and recognizes
"larger"textualunitscalledtokens.Forexample,ifthesourceprogramscontainsthe
characters
VARics142:INTEGER//variabledeclaration
thescannerwouldproducethetokens
VARID(ics142)COLONID(INTEGER)SEMICOLON
tobeprocessedinlaterphasesofthecompiler.Notethatthescannerdiscardswhite
spaceandcommentsbetweenthetokens,i.e.theyare"filtered"andnotpassedonto
laterphases.Examplesofnontokensaretabs,linefeeds,carriagereturns,etc.

HowtouseFLEX?
FLEX (Fast LEXical analyzer generator) is a tool for generating scanners. In stead of
writing a scanner from scratch, you only need to identify the vocabulary of a certain
language(e.g.Simple),write a specification of patterns using regular expressions (e.g.
DIGIT[09]), and FLEX will construct a scanner for you. FLEX is generally used in the
mannerdepictedhere:

First, FLEX reads a specification of a scanner either from an input file *.lex, or from
standard input, and it generates as output a C source file lex.yy.c. Then, lex.yy.c is
compiledandlinked with the "lfl" library to produce an executable a.out. Finally, a.out
analyzesitsinputstreamandtransformsitintoasequenceoftokens.
*.lex is in the form of pairs of regular expressions and C code. (sample1.lex,
sample2.lex)
lex.yy.cdefinesaroutineyylex()thatusesthespecificationtorecognizetokens.
a.outisactuallythescanner!

Practice
GetfamiliarwithFLEX
1. Trysample*.lex
http://alumni.cs.ucr.edu/~lgao/teaching/flex.html

1/2

1/3/2015

FLEXTutorial

2. CommandSequence:
flexsample*.lex
gcclex.yy.clfl
./a.out
Understandtheinputfile
1. Format:
definitions
%%
rules
%%
usercode
2. Thedefinitionssection:"namedefinition"
Therulessection:"patternaction"
Theusercodesection:"yylex()routine"
3. Trytoanswerquestionslistedinthesamplefiles
Writeascannerfor32bithexadecimalnumbers.Hereistheanswer...

Resources
1. FLEXhomepage
2. FLEXmanual
3. manflex

http://alumni.cs.ucr.edu/~lgao/teaching/flex.html

2/2

You might also like