27
Compiler Principle and Technology Prof. Dongming LU Feb. 25th, 2019

Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Compiler Principle

and Technology

Prof. Dongming LU

Feb. 25th, 2019

Page 2: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2. Scanning

(Lexical

Analysis)

Page 3: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Contents

2.1 The Scanning Process

2.2 Regular Expression

2.3 Finite Automata

2.4 From Regular Expressions to DFAs

2.5 Implementation of a TINY Scanner

2.6 Use of Lex to Generate a Scanner Automatically

Page 4: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2.1 The

Scanning

Process

Page 5: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

The Function of a Scanner

Scanner

Tokens

Source code

PatternrecognitionHow to specification:

Regular Expression

Its Tools:DFA & NFA

Page 6: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Tokens

• RESERVED WORDS

Such as IF and THEN, represent the strings of characters “if”and “then”

• SPECIAL SYMBOLS

Such as PLUS and MINUS, represent the characters “+” and “-“

• OTHER TOKENS

Such as NUM and ID, represent numbers and identifiers

Tokens are defined as an enumerated type

Typedef enum

{IF, THEN, ELSE, PLUS, MINUS, NUM, ID,…}

TokenType;

Categories of Tokens:

Page 7: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Some Practical Issues of the Scanner

• A token record : one structured data type to collect all the attributes of a token

▫ Typedef struct

{

TokenType tokenval;

char *stringval;

int numval;

} TokenRecord

Page 8: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Some Practical Issues of the Scanner

• The scanner returns the token value only and places the other attributes in variables

TokenType getToken(void)

• As an example of operation of getToken, consider the following line of C code.

A[index] = 4+2

a [ i n d e x ] = 4 + 2

a [ i n d e x ] = 4 + 2

Page 9: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2.2 Regular

Expression

Page 10: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Some Relative Basic Concepts • Regular expressions :Patterns of strings of characters

• Three elements: r:the regular expression

L(r):The set of language match r

∑:The set elements referred to as symbols(alphabet)

RE is only a tool for Scanning

Page 11: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2.2.1

Definition of

Regular

Expressions

Page 12: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Basic Regular Expressions

The single characters from alphabet matching themselves

Eg:

▫ a matches the character a by writing L(a)={ a }

▫ ε denotes the empty string, by L(ε)={ε}

▫ {} or Φ matches no string at all, by L(Φ)={ }

Page 13: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Regular Expression Operations

• Choice among alternatives

▫ indicated by the meta-character |

• Concatenation

▫ indicated by juxtaposition

• Repetition or “closure”

▫ indicated by the meta-character *

Page 14: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Precedence of Operation and Use of

Parentheses

• The standard precedence convention

* > concatenation > |

• A simple example

a|bc* is interpreted as a|(b(c*))

• Parentheses is used to indicate a different precedence

Page 15: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Definition of Regular Expression

• A regular expression is one of the following:

(1) A basic regular expression, a single legal character

a from alphabet ∑, or meta-character ε or Φ.

(2) The form r|s, where r and s are regular expressions

(3) The form rs, where r and s are regular expressions

(4) The form r*, where r is a regular expression

(5) The form (r), where r is a regular expression

• Parentheses do not change the language.

Page 16: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Examples of Regular Expressions

Example 1:

∑={ a,b,c} ,the set of all strings over this alphabet that

contain exactly one b:

(a|c)*b(a|c)*

Example 2:∑={ a,b,c} ,the set of all strings that contain at most one b:

(a|c)*|(a|c)*b(a|c)* (a|c)*(b|ε)(a|c)*

the same language may be generated by many different regular expressions.

Page 17: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Examples of Regular Expressions

Example 3:

∑={ a,b}, the set of strings consists of a single b

surrounded by the same number of a’s :

S = {b, aba, aabaa,aaabaaa,……} = { anban | n≠0}

This set can not be described by a regular expression.

“regular expression can’t count ”

▫ not all sets of strings can be generated by regular expressions.

▫ A regular set : a set of strings that is the language for a regular expression is distinguished from other sets.

Page 18: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Examples of Regular Expressions

Example 4:

∑={ a,b,c}, the strings contain no two consecutive b’s :

( (a|c)* | (b(a|c))* )*

( (a | c ) | (b( a | c )) )* or (a | c | ba | bc)*

Not yet the correct answer

The correct regular expression :

▫ (a | c | ba | bc)* (b |ε)

▫ ((b |ε) (a | c | ab| cb )*

▫ (not b |b not b)*(b|ε) not b = a|c

Page 19: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Examples of Regular Expressions

Example 5:

∑={ a,b,c}, ((b|c)* a(b|c)*a)* (b|c)* Determine a concise

English description of the language.The strings contain an

even number of a’s:

(not a* a not a* a)* not a*

Page 20: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2.2.2

Extensions to

Regular

Expression

Page 21: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

List of New Operations

1) One or more repetitions:

r+

2) Any character:

period “.”

3) A range of characters:

[0-9], [a-zA-Z]

4) Any character not in a given set:

(a|b|c) a character not either a or b or c

[^abc] in Lex

5) Optional sub-expressions

r ? the strings matched by r are optional

Page 22: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

2.2.3 Regular Expressions

for Programming

Language Tokens

Page 23: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Number, Reserved word and Identifiers

Numbers

▫ nat = [0-9]+

▫ signedNat = (+|-)?nat

▫ number = signedNat(“.”nat)? (E signedNat)?

Reserved Words and Identifiers

▫ reserved = if | while | do |………

▫ letter = [a-z A-Z]

▫ digit = [0-9]

▫ identifier = letter(letter|digit)*

Page 24: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

CommentsSeveral forms:

{ this is a pascal comment } {( })*}

; this is a schema comment-- this is an Ada comment --(newline)* newline \n

/* this is a C comment */ can not written as ba(~(ab))*ab, ~ restricted to single

character one solution for ~(ab) : b*(a*(a|b)b*)*a*

Because of the complexity of regular expression, the comments will be handled by ad hoc methods in actual scanners.

Page 25: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

Ambiguity

• Ambiguity: some strings can be matched by several

different regular expressions

▫ Either an identifier or a keyword, keyword

interpretation preferred. Such as “if”

▫ A single token or a sequence of several tokens, the

single-token preferred.( the principle of longest

sub-string.) such as “abcdefg”

Page 26: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

White Space and Lookahead

White space:

▫ Delimiters: characters that are unambiguously part of other

tokens are delimiters.

Such as xtemp=ytemp

▫ whitespace = ( newline | blank | tab | comment)+

Free format or fixed format

Lookahead:

▫ Buffering of input characters , marking places for

Backtracking , such as (well-known example)

DO99I=1,10

DO99I=1.10

Page 27: Compiler Principle and Technology - Zhejiang Universitynetmedia.zju.edu.cn/compiler/lecture_for_19/Chapter_two... · 2019. 2. 22. · int numval;} TokenRecord. Some Practical Issues

End of

Chapter Two(1)

THANKS