Docsity

Lexical Analysis - Part I for EECS 483 at University of Michigan, Study Guides, Projects, Research of Electrical and Electronics Engineering

Toggle sidebar
bg1
Lexical Analysis –Part I
EECS 483 – Lecture 2
University of Michigan
Monday, September 11, 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Lexical Analysis - Part I for EECS 483 at University of Michigan and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

Lexical Analysis

– Part I

EECS 483 – Lecture 2 University of Michigan Monday, September 11, 2006

  • 1 -

Announcements

Course webpage is up

» http://www.eecs.umich.edu/~mahlke/483f06 » Also link from EECS webpage, under courses

Project 1

» Available by Wednes

Simon’s office hours

» 1:30 – 3:30, Tues/Thurs » Study room, Table 2 on the first floor of CSE » Note, Tuesdays 3-3:30, the study rooms are

not available, so he will just be at one of thetables by Foobar for the last half hour

  • 3 -

Lexical Analysis Process

Lexical Analysis or Scanner

if (b == 0) a = b;

Preprocessed source code, read char by char

if

(^

b

==

0

)^

a

=

b

;

Lexical analysis

  • Transform multi-character input stream to token stream - Reduce length of program representation (remove spaces

)

  • 4 -

Tokens

Identifiers: x y11 elsex

Keywords: if else while for break

Integers: 2 1000 -

Floating-point: 2.

-0.0010 .02 1e

Symbols: + * { } ++ << < <= [ ]

Strings: “x” “He said, \”I luv EECS483\””

  • 6 -

Language

A regular expression R describes a set ofstrings of characters denoted L(R)

L(R) = the language defined by R

» L(abc) = { abc } » L(hello|goodbye) = { hello, goodbye } » L(1(0|1)*) = all binary numbers that start with

a 1

Each token can be defined using a regularexpression

  • 7 -

RE Notational Shorthand

R+ one or more strings of R: R(R*)

R?

optional R: (R|

[abcd]

one of listed characters: (a|b|c|d)

[a-z]

one character from this range:

(a|b|c|d...|z)

[^ab]

anything but one of the listed chars

[^a-z]

one character not from this range

  • 9 -

Class Problem

A. Whats the difference?

» [abc]

abc

Extend the description of real on theprevious slide to include numbers inscientific notation

y

-2.3E+17, -2.3e-17, -2.3E

  • 10 -

How to Break up Text

REs alone not enough, need rule forchoosing when get multiple matches

» Longest matching token wins » Ties in length resolved by priorities

y

Token specification order often defines priority

» RE’s + priorities + longest matching token

elsex = 0; rule = definition of a lexer

else

x

elsex

  • 12 -

Lex/Flex

Flex

Flex Spec

user defs

tables lexer and

action routines

user code

lex.yy.c

foo.l

yylex()

tokens

request

token names, etc

  • 13 -

Lex Specification

™

Definition section

»

All code contained within “%{“and “%}” is copied to theresultant program. Usually hastoken defns established by theparser »

User can provide names forcomplex patterns used in rules »

Any additional lexing states(states prefaced by %s directive) »

Pattern and state definitionsmust start in column 1 (All lineswith a blank in column 1 arecopied to resulting C file)

lex file always has 3 sections:

definition section

rules section

user functions section

  • 15 -

Partial Flex Program

D
[0-9]

%% if

printf ("IF statement\n");

[a-z]+

printf ("tag, value %s\n", yytext);

{D}+

printf ("decimal number %s\n", yytext);

printf ("unary op\n");

printf ("binary op\n");

action

pattern

  • 16 -

Flex Program

%{

#include <stdio.h>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 );

} Running the above program:[ 17 ] sandbox -: flex count.l[ 18 ] sandbox -: gcc lex.yy.c -lfl[ 19 ] sandbox -: a.out < count.l# of lines = 16, # of chars = 245

  • 18 -

Lex Regular Expression Meta Chars

Meta Char

Meaning

.^

match any single char (except \n ?)


Kleene closure (0 or more)

[]

Match any character within brackets - in first position matches - ^ in first position inverts set

^

matches beginning of line

$

matches end of line

{a,b}

match count of preceding pattern

from a to b times, b optional

**^

escape for metacharacters

+

positive closure (1 or more)

?

matches 0 or 1 REs

|^

alteration

/^

provides lookahead

()

grouping of RE

<>

restricts pattern to matching only in that state

  • 19 -

Class Problem^ Write the flex rules to strip out all comments of the form^ /*, */ from an input program^ Hints: Action ECHO copies input token to output

Think of using 2 states Keyword BEGIN “state” takes you to that state