Docsity

Lexical Analysis: Identifying Tokens in Computer Science, Study notes of Computer Science

Partial preview of the text

Download Lexical Analysis: Identifying Tokens in Computer Science and more Study notes Computer Science in PDF only on Docsity!

Prof. Fateman CS 164 Lecture 5

Lexical Analysis

Lecture 5

Prof. Fateman CS 164 Lecture 5

Outline • Informal sketch of lexical analysis

  • Identifies tokens in input string
    • Issues in lexical analysis
      • Lookahead – Ambiguities
        • Specifying lexers
          • Regular expressions – Examples of regular expressions

Prof. Fateman CS 164 Lecture 5

What’s a Token? • A syntactic category

  • In English:

noun, verb, adjective, …

  • In a programming language:

Identifier, Integer, Single-Float, Double-Float, operator

(perhaps single or multiple character), Comment,Keyword, Whitespace, string constant, …

Prof. Fateman CS 164 Lecture 5

Defining Tokens more precisely •^

Token categories

correspond to sets of strings

, some

of them finite sets, like keywords, but some, likeidentifiers, unbounded.

-^

Identifier: (typically)

strings of letters or digits,

starting with a letter. Some languages restrict lengthof identifier. Lisp does not require first letter…

•^

Integer:

a non-empty string of digits. Bounded?

•^

Keyword:

else

or

if

or

begin

or …

•^

Whitespace:

a non-empty sequence of blanks,

newlines, and tabs.

Prof. Fateman CS 164 Lecture 5

How are Tokens used? •^

Output of lexical analysis is a stream of tokens... InLisp, we just make a list.. (token1 token2 token3 ….).[In an interactive system we would just burp outtokens as we find their ends; we don’t wait for EOF]

-^

This stream is the input to the next pass, the parser.^ –

(parser (lexer “filename”))

in Lisp.

•^

Parser makes use of token distinctions^ –

An identifier FOO may be treated differently from akeyword ELSE.

(Some language have no keywords. Lisp.)

Prof. Fateman CS 164 Lecture 5

Designing a Lexical Analyzer: Step 1 • Define a finite set of tokens

  • Tokens describe all items of interest – Choice of tokens depends on language, design of

parser

  • Recall

\tif

(i

==

j)\n\t\tz

=

0;\n\telse\n\t\tz

=

1;

  • Useful tokens for this expression:

Integer, Keyword, operator, Identifier, Whitespace,

Prof. Fateman CS 164 Lecture 5

Lexical Analyzer: Implementation •^

An implementation reads characters until itfinds the end of a token. (Longest possiblesingle token). It then returns a package ofup to 3 items: –^

What kind of thing is it? Identifier? Number?Keyword?

-^

If it is an identifier, which one exactly? Foo? Bar?

-^

Where did this token appear in the source code(line/col).

-^

Whitespace and comments are skipped.

Prof. Fateman CS 164 Lecture 5

Example •^

Recall:^ \tif (i == j)\n\t\tz = 0;\n\telse\n\t\tz = 1;

-^

Possible Token-lexeme groupings:^ –

Identifier: i, j, z

-^

Keyword: if, else

-^

Multi-character operator: ==

(not in MiniJava)

-^

Integer: 0, 1

-^

(, ), =, ; single character operators of the same name

-^

Illegal.

E.g. for MiniJava, ^ % $

… everything else?

Prof. Fateman CS 164 Lecture 5

True Crimes of Lexical Analysis • Is it as easy as it sounds? • Not quite! • Look at some history...

Prof. Fateman CS 164 Lecture 5

Lexical Analysis in FORTRAN • FORTRAN rule: Whitespace is insignificant • E.g.,

VAR

is the same as

VA R

  • Footnote: FORTRAN whitespace rule

motivated by inaccuracy of punch cardoperators

Prof. Fateman CS 164 Lecture 5

A nicer design limits the amount of lookaheadneeded

Even our simple example has lookahead issues

ii

vs.

if

vs.

&& (not in MJ…)

But this uses only one or two characters, and is

certainly resolved by the time you find whitespace, or another character, e.g. X&Y or X&&Y.In C, consider a+++++b. Is it a++ + ++b? Or(a++)++ +b

or some other breakup?

Prof. Fateman CS 164 Lecture 5

Review • The goal of lexical analysis is to

  • Partition the input string into lexemes – Identify the token type, and perhaps the value of

each lexeme (probably. E.g.convert_string_to_integer(“12345") )

  • Left-to-right scan => lookahead sometimes

required.

Prof. Fateman CS 164 Lecture 5

Languages

Def.

Let

be a set of characters. A

language

over

is a set of strings of characters drawn

from

is called the

alphabet. Pronounced SIGMA.

Why are we using Greek? So as to separate the

language from the meta language.

Presumably

our alphabet does not include

as a

character.)

Prof. Fateman CS 164 Lecture 5

Examples of Languages •^

Alphabet = Englishcharacters

-^

Language = Englishsentences

-^

Not every string ofEnglish characters is anEnglish sentence.

•^

Alphabet = ASCII,

-^

Language = C programs,

-^

Note: ASCII characterset is different fromEnglish character set.

-^

UNICODE is another,much larger set, used byJava, Common Lisp.