Docsity

Lecture Slides on Lexical Analysis | EECS 483, Study Guides, Projects, Research of Electrical and Electronics Engineering

Partial preview of the text

Download Lecture Slides on Lexical Analysis | EECS 483 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 8, 2003

  • 1 -

Announcements^ Y^

Overrides^ » All that I have been given are processed^ » Need one, see me Y Course webpage is up^ » http://www.eecs.umich.edu/~mahlke/483f03^ » Also link from EECS webpage, under courses Y Project 1^ » Not done yet, we’ll have it for you Wednes^ » This book is very useful (borrow or buy it)

y^ Lex & Yacc

, Levine, Mason and Brown

  • 3 -

Lexical Analysis Process

Preprocessed source code, read char by char

if (b == 0) a = b; Lexical Analysis or Scanner

if^

(^

b^

==^

0

)^

a^

=^

b^

;

Lexical analysis

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

Tokens^ Y^

Identifiers: x y11 elsex Y Keywords: if else while for break Y Integers: 2 1000 -20 Y Floating-point: 2.

-0.0010 .02 1e

Y^ Symbols: + * { } ++ << < <= [ ] Y^ Strings: “x” “He said, \”I love EECS483\””Comments: /* bla bla bla */ Y^

  • 6 -

Language^ Y^

A regular expression R describes a set ofstrings of characters denoted L(R) Y 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

Y^ Each token can be defined using a regularexpression

  • 7 -

RE Notational Shorthand^ Y^

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

optional R: (R|

Y^ [abcd]

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

Y^ [a-z]

one character from this range:

(a|b|c|d...|z) Y [^ab]

anything but one of the listed chars

Y^ [^a-z]

one character not from this range

  • 9 -

Class Problem^ Y^

A. Whats the difference?^ » [abc]

abc

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

y^ -2.3E+17, -2.3e-17, -2.3E

  • 10 -

How to Break up Text

else

x^

=^

elsex = 0;

2 elsex

=^

Y^ 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 tokenrule = definition of a lexer

  • 12 -

Lex/Flex

tokens

Flex Flex Spec

request

user defs^ tables lexer and action routines user code

token names, etc

lex.yy.c

yylex()

foo.l

  • 13 -

Lex Specification^ Y^

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");

pattern

action

  • 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