• Tidak ada hasil yang ditemukan

Lexical Analysis (2)

N/A
N/A
Protected

Academic year: 2025

Membagikan "Lexical Analysis (2)"

Copied!
29
0
0

Teks penuh

(1)

Lexical Analysis (2)

Sukree Sinthupinyo 1

1

Department of Computer Engineering Chulalongkorn University

14 July 2012

(2)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(3)

Example

We will use this grammar as our first example

(4)

Example

(5)

Tokens, Patterns, and Attribute Values

(6)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(7)

Transition Diagrams

We’ll convert the regular-expression to transition diagrams Nodes represent states.

Edges represent methods of how to change the state.

If we find an edge labeled a connecting from current state

to another state and the next symbol is a, we will move to

such state.

(8)

Transition Diagrams (cont.)

Conventions about transition diagrams

Accepting or final state indicates that a lexeme has been found. It’s represented by a double circle.

If it is necessary to retract the forward pointer one position, then we shall place a * near that accepting state.

There is one start state, or initial state.

(9)

Transition Diagram for relop

(10)

Recognition of Reserved Words and Identifiers

How to recognize keywords, such as if, then and, else.

Install the reserved words in the symbol table initially. Any new identifier will be an id.

Create separate transition diagrams for each keyword.

(11)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(12)

Completion of the Running Example

A transition diagram for unsigned numbers.

A transition diagram for whitespace.

(13)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(14)

Architecture of a Transition-Diagram-Based Lexical Analyzer

Example Code for relop

(15)

Architecture of a Transition-Diagram-Based Lexical

Analyzer (cont.)

(16)

Architecture of a Transition-Diagram-Based Lexical

Analyzer (cont.)

(17)

Architecture of a Transition-Diagram-Based Lexical Analyzer (cont.)

Try sequentially. The function fail() will reset the pointer forward and starts the next transition diagram.

Run in parallel. Feed the next input to all of them and allowing each one to make whatever transition it required.

Combine all the transition diagrams into one.

We combine states 0, 9, 12, and 22 into one start state.

(18)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(19)

KMP Algorithm

Knuth, Morris, and Pratt algorithm

Recognize a single keyword b

1

b

2

. . . b

n

in a text string.

For example, the diagram for the keyword ababaa is

We must find the failure function, f (s) computed as follow.

(20)

Failure Function

ababaa

(21)

Failure Function cont.

To find whether a keyword b 1 b 2 . . . b n is a substring of a

string a 1 a 2 . . . a m , we use the following algorithm.

(22)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(23)

Use of Lex

Process of Lex

(24)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

(25)

Form of Lex

declarations

%%

translation rules

%%

auxiliary functions

(26)

Declarations

(27)

Translations Rules

(28)

Auxiliary Functions

(29)

Outline

1 Recognition of Tokens Transition Diagrams

Completion of the Running Example

Architecture of a Transition-Diagram-Based Lexical Analyzer

Recognizing a keyword

2 Lexical-Analyzer Generator Lex Use of Lex

Structure of Lex

Lex Demonstration

Referensi

Dokumen terkait

bahasa inggris textbook had different genre in eac h text but it didn’t influence lexical density of the text because the influencing of the high and low

two types of cohesive devices namely lexical cohesion and

The second research, conducted by Yi Wang with the title Lexical Cohesion Patterns of Natural Science Academic Text and Implications for Lexical Teaching: Textual Analysis of IELTS

86 AN ANALYSIS OF LEXICAL ERRORS FOUND IN STUDENTS’ ARGUMENTATIVE ESSAY Tia Melani * Institut Agama Islam Negeri Batusangkar, Sumatera Barat, Indonesia E-mail:

FINDINGS AND DISCUSSIONS Findings The analysis of the 3,643 words of data established 601 lexical cohesion devices in Naila Farhana's videos, namely "How you can start to speak

Finding and Discussion As a result of this study, the researcher found lexical cohesion of four types in CNN news: synonym, repetition, meronymy, and antonym.. The table below

Lexical Bundles and Rhetorical Moves Connection All of lexical bundles that have been listed as data in this study were examined structurally and functionally, those bundles were

The next lexical bundles with high frequent usage after the verb-based structural form is prepositional-based with a total of 12 lexical bundles used in the articles with the Other PP