Docsity

Lexical Analysis - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Lexical Analysis - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 3: Lexical Analysis

Lexical Analysis

 Basic Concepts & Regular Expressions

 What does a Lexical Analyzer do?

 How does it Work?

 Formalizing Token Definition & Recognition

 Regular Expressions and Languages

 Reviewing Finite Automata Concepts

 Non-Deterministic and Deterministic FA

 Conversion Process

 Regular Expressions to NFA  NFA to DFA

 Relating NFAs/DFAs /Conversion to Lexical

Analysis – Tools Lex/Flex/JFlex/ANTLR

 Concluding Remarks /Looking Ahead

Scanning Perspective

 Purpose

 Transform a stream of symbols

 Into a stream of tokens

Lexical Analyzer in Perspective

 LEXICAL ANALYZER

Scan InputRemove WS, NL, …Identify TokensCreate Symbol TableInsert Tokens into STGenerate ErrorsSend Tokens to Parser

 PARSER

Perform Syntax AnalysisActions Dictated by Token OrderUpdate Symbol Table EntriesCreate Abstract Rep. of SourceGenerate ErrorsAnd More…. (We’ll see later)

Introducing Basic Terminology

 What are Major Terms for Lexical Analysis?

 TOKEN

 A classification for a common set of strings  Examples Include Identifier, Integer, Float, Assign, LParen, RParen, etc.  PATTERN  The rules which characterize the set of strings for a token – integers [0-9]+  Recall File and OS Wildcards ([A-Z].)  LEXEME  Actual sequence of characters that matches pattern and is classified by a token  Identifiers: x, count, name, etc…  Integers: 345, 20 - 12, etc. Docsity.com

Introducing Basic Terminology

Token Sample Lexemes Informal Description of Pattern const

if

relation

id

num

literal

const if <, <=, =, < >, >, >= pi, count, D 3.1416, 0, 6.02E “core dumped”

const if < or <= or = or < > or >= or > letter followed by letters and digits any numeric constant any characters between “ and “ except “

Classifies Pattern

Actual values are critical. Info is :

  1. Stored in symbol table
  2. Returned to parser

How Does Lexical Analysis Work?

 Question is related to efficiency

 Where is potential performance bottleneck?

 Reconsider slide ASU - 3-

 3 Techniques to Address Efficiency :

 Lexical Analyzer Generator

 Hand-Code / High Level Language

 Hand-Code / Assembly Language

 In Each Technique …

 Who handles efficiency?

 How is it handled?

Efficiency issues

 Is efficiency an issue?

 3 Lexical Analyzer construction techniques

 How they address efficiency?

 Lexical Analyzer Generator  Hand-Code / High Level Language (I/O facilitated by the language)  Hand-Code / Assembly Language (explicitly manage I/O).

 In Each Technique …

 Who handles efficiency?

 How is it handled?

Formalization

 How to formalize this pseudo-algorithm?

 Idea

 Lexemes are simple

 Tokens are sets of lexemes....

 So: Tokens form a LANGUAGE

 Question

 What languages do we know?

Regular Context Free Context Sensitive Natural

I/O - Key For Successful Lexical Analysis

 Character-at-a-time I/O

 Block / Buffered I/O

 Block/Buffered I/O

 Utilize Block of memory

 Stage data from source to buffer block at a time

 Maintain two blocks - Why (Recall OS)?

 Asynchronous I/O - for 1 block  While Lexical Analysis on 2nd block

Tradeoffs?

Block 1 Block 2

When done, ptr... issue I/O Still Process tokenin 2nd block

Formalizing Token Definition

ALPHABET : Finite set of symbols {0,1}, or {a,b,c}, or {n,m, … , z}

DEFINITIONS:

STRING : Finite sequence of symbols from an alphabet. 0011 or abbca or AABBC … A.K.A. word / sentence

If S is a string, then |S| is the length of S, i.e. the number of symbols in the string S. ∈ : Empty String, with | ∈ | = 0

Formalizing Token Definition

EXAMPLES AND OTHER CONCEPTS:

Suppose: S is the string banana

Prefix : ban, banana Suffix : ana, banana Substring : nan, ban, ana, banana Subsequence: bnan, nn

Proper prefix, suffix, or substring cannot be all of S

Formal Language Operations

OPERATION DEFINITION

union of L and M written L ∪ M concatenation o f L and M written LM Kleene closure of L written L*

positive closure of L written L+

L ∪ M = {s | s is in L or s is in M}

LM = {st | s is in L and t is in M}

L+=

∞ i = 0

Li L denotes “zero or more concatenations of “ L*

L=*

∞ i = 1 Li L+^ denotes “one or more concatenations of “ L

Formal Language Operations

Examples

L = {A, B, C, D } D = {1, 2, 3}

L ∪ D = {A, B, C, D, 1, 2, 3 }

LD = {A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3 }

L^2 = { AA, AB, AC, AD, BA, BB, BC, BD, CA, … DD}

L^4 = L^2 L^2 = ??

L = { All possible strings of L plus* ∈ }

L+^ = L -* ∈

L (L ∪ D ) = ??

L (L ∪ D ) = ??*