Introduction to Lex

Information about Introduction to Lex

Published on November 30, 2007

Author: Chyou

Source: authorstream.com

Content

Introduction to Lex:  Introduction to Lex Ying-Hung Jiang [email protected] Outlines:  Outlines Review of scanner Introduction to flex Regular Expression Cooperate with Yacc Resources Review:  Review Parser -> scanner -> source code See teacher’s slide flex:  flex flex - fast lexical analyzer generator:  flex - fast lexical analyzer generator Flex is a tool for generating scanners. Flex source is a table of regular expressions and corresponding program fragments. Generates lex.yy.c which defines a routine yylex() Format of the Input File:  Format of the Input File The flex input file consists of three sections, separated by a line with just %% in it: definitions %% rules %% user code Definitions Section:  Definitions Section The definitions section contains declarations of simple name definitions to simplify the scanner specification. Name definitions have the form: name definition Example: DIGIT [0-9] ID [a-z][a-z0-9]* Rules Section:  Rules Section The rules section of the flex input contains a series of rules of the form: pattern action Example: {ID} printf( "An identifier: %s\n", yytext ); The yytext and yylength variable. If action is empty, the matched token is discarded. Action:  Action If the action contains a ‘{‘, the action spans till the balancing ‘}‘ is found, as in C. An action consisting only of a vertical bar ('|') means "same as the action for the next rule.“ The return statement, as in C. In case no rule matches: simply copy the input to the standard output (A default rule). Precedence Problem:  Precedence Problem For example: a “<“ can be matched by “<“ and “<=“. The one matching most text has higher precedence. If two or more have the same length, the rule listed first in the flex input has higher precedence. User Code Section:  User Code Section The user code section is simply copied to lex.yy.c verbatim. The presence of this section is optional; if it is missing, the second %% in the input file may be skipped. In the definitions and rules sections, any indented text or text enclosed in %{ and %} is copied verbatim to the output (with the %{}'s removed). A Simple Example:  A Simple Example %{ int num_lines = 0, num_chars = 0; %} %% \n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); } Any Question So Far?:  Any Question So Far? Regular Expression:  Regular Expression Regular Expression (1/3):  Regular Expression (1/3) x match the character 'x' . any character (byte) except newline [xyz] a "character class"; in this case, the pattern matches either an 'x', a 'y', or a 'z' [abj-oZ] a "character class" with a range in it; matches an 'a', a 'b', any letter from 'j' through 'o', or a 'Z' [^A-Z] a "negated character class", i.e., any character but those in the class. In this case, any character EXCEPT an uppercase letter. [^A-Z\n] any character EXCEPT an uppercase letter or a newline Regular Expression (2/3):  Regular Expression (2/3) r* zero or more r's, where r is any regular expression r+ one or more r's r? zero or one r's (that is, "an optional r") r{2,5} anywhere from two to five r's r{2,} two or more r's r{4} exactly 4 r's {name} the expansion of the "name" definition (see above) "[xyz]\"foo“ the literal string: [xyz]"foo \X if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v', then the ANSI-C interpretation of \x. Otherwise, a literal 'X' (used to escape operators such as '*') Regular Expression (3/3):  Regular Expression (3/3) \0 a NUL character (ASCII code 0) \123 the character with octal value 123 \x2a the character with hexadecimal value 2a (r) match an r; parentheses are used to override precedence (see below) rs the regular expression r followed by the regular expression s; called "concatenation" r|s either an r or an s ^r an r, but only at the beginning of a line (i.e., which just starting to scan, or right after a newline has been scanned). r$ an r, but only at the end of a line (i.e., just before a newline). Equivalent to "r/\n". Cooperate with Yacc:  Cooperate with Yacc The y.tab.h file (generated by –d option). See pascal.l and pascal.y for example. Resources:  Resources Google directory of lexer and parser generators. Flex homepage: http://www.gnu.org/software/flex Lex/yacc Win32 port: http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html Above links are available on course webpage. Flex(1) Any Question?:  Any Question?

Related presentations


Other presentations created by Chyou

4 ISPS and SMDG
06. 11. 2007
0 views

4 ISPS and SMDG

EAIEWorkshop
27. 03. 2008
0 views

EAIEWorkshop

Tutorial kari
14. 03. 2008
0 views

Tutorial kari

ne319 tsp
05. 03. 2008
0 views

ne319 tsp

L17 projection
27. 02. 2008
0 views

L17 projection

carers
24. 02. 2008
0 views

carers

dm konf 041209Gruenwald
20. 02. 2008
0 views

dm konf 041209Gruenwald

Embedded Poster
07. 01. 2008
0 views

Embedded Poster

BioresE
07. 01. 2008
0 views

BioresE

vrst07 cfp
03. 10. 2007
0 views

vrst07 cfp

supply
04. 10. 2007
0 views

supply

tilapia
24. 10. 2007
0 views

tilapia

pest5
28. 11. 2007
0 views

pest5

Mendelian Genetics
11. 12. 2007
0 views

Mendelian Genetics

Pandemic Influenza Summit Ware
25. 10. 2007
0 views

Pandemic Influenza Summit Ware

health
25. 10. 2007
0 views

health

Lesson 20
30. 10. 2007
0 views

Lesson 20

COMMEDIA DELLARTE
01. 11. 2007
0 views

COMMEDIA DELLARTE

fulsyst2
06. 11. 2007
0 views

fulsyst2

010727 1 BAE
06. 11. 2007
0 views

010727 1 BAE

Logistics Presentation
12. 11. 2007
0 views

Logistics Presentation

ebMS Autotech07 demoslides
14. 11. 2007
0 views

ebMS Autotech07 demoslides

MEDIA BETWEEN LOVE AND HATE demo
26. 10. 2007
0 views

MEDIA BETWEEN LOVE AND HATE demo

hotel axe
14. 12. 2007
0 views

hotel axe

investigation preparation
17. 12. 2007
0 views

investigation preparation

logistics exercises 1
05. 11. 2007
0 views

logistics exercises 1

MoneyHandling12
12. 12. 2007
0 views

MoneyHandling12

tirerobot
31. 12. 2007
0 views

tirerobot

Jeffrey BeardPP presentation
27. 12. 2007
0 views

Jeffrey BeardPP presentation

946 a1 pcard reissue
06. 11. 2007
0 views

946 a1 pcard reissue

SLD 10 Setting the World Stage
25. 12. 2007
0 views

SLD 10 Setting the World Stage

Wedding Procesional
18. 12. 2007
0 views

Wedding Procesional

Tan Tarkan
01. 10. 2007
0 views

Tan Tarkan

wkshp
02. 10. 2007
0 views

wkshp

greekhowtoslides
08. 11. 2007
0 views

greekhowtoslides

08 Bridges Becquerel
28. 12. 2007
0 views

08 Bridges Becquerel

FirstYearReport
31. 10. 2007
0 views

FirstYearReport

1 iron steel overview
04. 01. 2008
0 views

1 iron steel overview

StarryM 3
13. 11. 2007
0 views

StarryM 3

assignnov 04thesaurus
05. 12. 2007
0 views

assignnov 04thesaurus

Arun Upasana Hindu
10. 12. 2007
0 views

Arun Upasana Hindu

SWSLURC090804
02. 01. 2008
0 views

SWSLURC090804

ww1 me
26. 10. 2007
0 views

ww1 me

How Onsite Systems Work
30. 10. 2007
0 views

How Onsite Systems Work

carrus
01. 11. 2007
0 views

carrus

APDIP ChiangRai Presentation
29. 11. 2007
0 views

APDIP ChiangRai Presentation

APOR Accept Flag Do Dont
05. 11. 2007
0 views

APOR Accept Flag Do Dont

efum 050928 lagoze
27. 09. 2007
0 views

efum 050928 lagoze

1391
29. 10. 2007
0 views

1391

Hurley GRB LAT05
30. 10. 2007
0 views

Hurley GRB LAT05

glast2003
31. 10. 2007
0 views

glast2003

Employment Law Bill Duda
22. 11. 2007
0 views

Employment Law Bill Duda

Metzger
20. 11. 2007
0 views

Metzger

STun
06. 11. 2007
0 views

STun

Vag
03. 10. 2007
0 views

Vag

Asakawa Taiwan2006
14. 11. 2007
0 views

Asakawa Taiwan2006

underground parties in hungary
13. 12. 2007
0 views

underground parties in hungary

36719161
01. 12. 2007
0 views

36719161