chapter 5 compilers - tunghai universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 lexical analysis...

41
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj

Upload: others

Post on 17-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

1

Chapter 5Compilers

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler or Assembler

obj

Page 2: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

2

Terminology

Statement (敘述)Declaration, assignment containing expression (運算式)

Grammar (文法)A set of rules specify the form of legal statements

Syntax (語法) vs. Semantics (語意)Example: assuming I, J, K:integer and X,Y:float

I:=J+K vs. I:= X+Y

Compilation (編譯) Matching statements written by the programmer to structures defined by the grammar and generating the appropriate object code.

Page 3: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

3

Basic Compiler

Lexical analysis (字彙分析) - scannerScanning the source statement, recognizing and classifying the various tokens

Syntactic analysis (語法分析) – parser (剖析器)Recognizing the statement as some language construct.

Construct a parser tree (syntax tree)

Code generation – code generatorGenerate assembly language codes

Generate machine codes (Object codes)

Page 4: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

4

ScannerPROGRAMSTATSVARSUM,SUMSQ,I

SUM:=0;SUMSQ:=

READ(VALUE);

Page 5: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

5

Lexical Analysis

FunctionScanning the program to be compiled and recognizing the tokens that make up the source statements.

TokensTokens can be keywords, operators, identifiers, integers, floating-point numbers, character strings, etc.

Each token is usually represented by some fixed-length code, such as an integer, rather than as a variable-length character string (see Figure 5.5)

Token type, Token specifier (value) (see Figure 5.6)

Page 6: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

6

Scanner Output

Token specifierIdentifier name, integer value, (type)

Token coding schemeFigure 5.5

Page 7: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

7

Page 8: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

8

Token Recognizer

By grammar<ident>::= <letter>|<ident><letter>|<ident><digit><letter>::= A | B | C | D | … | Z<digit> ::= 0 | 1 | 2 | 3 | … | 9

By scanner - modeling as finite automata (FStateA)Figure 5.8 (a)

Page 9: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

9

Recognizing Identifier

Identifiers allowing underscore (_)

Figure 5.8 (b)

Page 10: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

10

Recognizing Identifier

Page 11: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

11

Recognizing Integer

Allowing leading zeroesFigure 5.8 (c)

Disallowing leading zeroesFigure 5.8 (d)

Page 12: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

12

Scanner - Implementation

Figure 5.10 (a)Algorithmic code for identifier recognition

Tabular representation of finite automaton for Figure 5.9.

State A-Z 0-9 ;,+-*() : = .1 2 4 5 62 2 2 334 456 77

Page 13: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

13

4

8

Page 14: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

14

Parser

Grammar: a set of rules Backus-Naur Form (BNF)

Ex: Figure 5.2

TerminologyDefine symbol ::=

Nonterminal symbols <>

Alternative symbols |

Terminal symbols

Page 15: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

15

Simplified Pascal Grammar

Page 16: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

16

Parser<read> ::= READ (<id-list>)<id-list>::= id | <id-list>,id

<assign>::= id := <exp><exp> ::= <term> | <exp>+<term> | <exp>-<term><term>::=<factor> | <term>*<factor> | <term> DIV <factor><factor>::= id | int | (<exp>)

READ(VALUE)

SUM := 0

SUM := SUM + VALUE

MEAN := SUM DIV 100

Page 17: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

17

Syntax Tree

Page 18: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

18

Syntax Tree for Program 5.1

Page 19: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

19

Page 20: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

20

Syntactic Analysis

Recognize source statements as language constructs or build the parse tree for the statements.

Bottom-upOperator-precedence parsing

Shift-reduce parsing

LR(0) parsing

LR(1) parsing

SLR(1) parsing

LALR(1) parsing

Top-downRecursive-descent parsing

LL(1) parsing

Page 21: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

21

Operator-Precedence Parsing

OperatorAny terminal symbol (or any token)

Precedence* » +

+ « *

Operator-precedencePrecedence relations between operators

Page 22: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

22

Precedence Matrix for the Fig. 5.2

Page 23: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

23

Operator-Precedence Parse Example

BEGIN READ ( VALUE ) ;

Page 24: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

24

Operator-Precedence Parse Example

Page 25: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

25

Operator-Precedence Parse Example

Page 26: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

26

Operator-Precedence Parse Example

Page 27: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

27

Operator-Precedence Parsing

Bottom-up parsing

Generating precedence matrix Aho et al. (1988)

Page 28: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

28

Shift-reduce Parsing with Stack

Figure 5.14

Page 29: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

29

Recursive-Descent Parsing

Each nonterminal symbol in the grammar is associated with a procedure.

<read>::=READ (<id-list>)<stmt>::=<assign> | <read> | <write> | <for>

Left recursion<dec-list> ::= <dec> | <dec-list>;<dec>

Modification<dec-list> ::= <dec> {;<dec>}

Page 30: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

30

Recursive-Descent Parsing (cont’d.)

Page 31: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

31

Recursive-Descent Parsing of READ

Page 32: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

32

Recursive-Descent Parsing of IDLIST

Page 33: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

33

Recursive-Descent Parsing (cont’d.)

Page 34: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

34

Recursive-Descent Parsing of ASSIGN

Page 35: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

35

Recursive-Descent Parsing of EXP

Page 36: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

36

Recursive-Descent Parsing of TERM

Page 37: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

37

Recursive-Descent Parsing of FACTOR

Page 38: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

38

Recursive-Descent Parsing (cont’d.)

Page 39: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

39

Recursive-Descent Parsing (cont’d.)

Page 40: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

40

Recursive-Descent Parsing (cont’d.)

Page 41: Chapter 5 Compilers - Tunghai Universityctyang.thu.edu.tw/files/sp_chap5-1.pdf5 Lexical Analysis Function zScanning the program to be compiled and recognizing the tokens that make

41

Code Generation

Add S(id) to LIST and LISTCOUNT++