Top Banner
Lex: A Lexical Analyser Generator
23

Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

May 11, 2018

Download

Documents

phungkhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Lex: A Lexical Analyser Generator

Page 2: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Compiler-Construction Tools

The compiler writer uses specialised tools (in addition to thosenormally used for software development) that produce componentsthat can easily be integrated in the compiler and help implementvarious phases of a compiler.

Scanner generators

Parser generators

Syntax-directed translation

Code-generator generators

Data-flow analysis: key part of code optimisation

Page 3: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Constructing a Lexical Analyser

Problem:

Write a piece of code that examines the input string and finda prefix that is a lexeme matching one of the patterns for allthe needed tokens.

Page 4: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

A Simple Example

Example

Consider the following grammar:

Page 5: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.
Page 6: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Regular Definitions for the Language Tokens

Note that keywords if, then, else, also match the patterns forrelop, id and number.Assumption: consider keywords as ‘reserved words’.

Page 7: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Tokens Table

Page 8: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Whitespace

The LA also recognises the ‘token’ ws defined by:

ws → (blank|tab|newline)

This token will not be returned to the parser; the LA will restartfrom the next character.

Page 9: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Recogniser for relop

Page 10: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

An Implementation

Page 11: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Recogniser for id

Page 12: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Recogniser for number

Page 13: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Recogniser for whitespace

Page 14: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Lex

The Lex compiler is a tool that allows one to specify a lexicalanalyser from regular expressions.Inputs are specified in the Lex language.

A Lex program consists of declarations %% translation rules %%auxiliary functions.

Page 15: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Example

Page 16: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Example (ctd.)

Page 17: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Design of a LA Generator

Two approaches:

NFA-based

DFA-based

The Lex compiler is implemented using the second approach.

Page 18: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Generated LA

Page 19: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

We show how o convert regular expressions to NFA’s and aconversion of NFA’s into DFA’s. The NFA produced can then betransformed into a DFA by meanns of the latter construction ifdesired. It can also be used directly via a simulation following thesubset construction translation alorithm.

Page 20: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Constructing the Automaton

For each regular expression in the Lex program construct a NFA.

Page 21: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Simulating NFA’s

Example:

Page 22: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

Pattern Matching

Page 23: Lex: A Lexical Analyser Generator · Lex The Lex compiler is a tool that allows one to specify a lexical analyser from regular expressions. Inputs are speci ed in the Lex language.

LA Based on DFA’s