1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph....

17
1 May 12, 2022 1 May 12, 2022 May 12, 2022 Azusa, Azusa, CA CA Sheldon X. Liang Ph. D. Computer Science at Computer Science at Azusa Azusa Pacific University Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction CS400 Compiler Construction

Transcript of 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph....

Page 1: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

1

April 21, 20231

April 21, 2023April 21, 2023 Azusa, CAAzusa, CA

Sheldon X. Liang Ph. D.

Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS400 Compiler ConstructionCS400 Compiler Construction

Page 2: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

2

• Building a compiler involves:– Defining the syntax of a programming language

– Develop a source code parser: for our compiler we will use predictive parsing (greedy eater)

– Implementing syntax directed translation to generate intermediate code: our target is the JVM abstract stack machine

– Generating Java bytecode for the JVM

– Optimize the Java bytecode (optional)

April 21, 20232

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Building a Simple CompilerBuilding a Simple Compiler

Page 3: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

3

April 21, 20233

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Symbol TableSymbol Table

Page 4: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

4

April 21, 20234

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Keep in mind following questionsKeep in mind following questions

• Symbol table– A data structure– Hold information – Collected incrementally

• Why we need s-table– Keep track of semantics of variable– Character string (lexeme)– Variables’ type – Its position in storage

• What further use of s-table– Forward reference– Scope information– Storage allocation, size…

Page 5: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

5

April 21, 20235

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Content in Symbol TablesContent in Symbol Tables

Page 6: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

6

insert(s, t): returns array index to new entry for string s token t

lookup(s): returns array index to entry for string s or 0

The symbol table is globally accessible (to all phases of the compiler)

Each entry in the symbol table contains a string and a token value:struct entry{ char *lexptr; /* lexeme (string) */ int token;};struct entry symtable[];

Possible implementations:- simple C code (see textbook)- hashtables

April 21, 20236

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Symbol TableSymbol Table

Page 7: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

7

April 21, 20237

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Two Common Approaches(1/3)Two Common Approaches(1/3)

Page 8: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

8

April 21, 20238

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Two Common Approaches(2/3)Two Common Approaches(2/3)

Page 9: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

9

April 21, 20239

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Two Common Approaches(3/3)Two Common Approaches(3/3)

Page 10: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

10

April 21, 202310

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Forward ReferenceForward Reference

Page 11: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

11

factor ( expr ) | id { print(id.string) }

#define ID 259 /* token returned by lexan() */

factor(){ if (lookahead == ‘(‘) { match(‘(‘); expr(); match(‘)’); } else if (lookahead == ID) { printf(“ %s “, symtable[tokenval].lexptr); match(NUM); } else error();}

April 21, 202311

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

IdentifiersIdentifiers

Page 12: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

12

/* global.h */#define DIV 257 /* token */#define MOD 258 /* token */#define ID 259 /* token */

/* init.c */insert(“div”, DIV);insert(“mod”, MOD);

/* lexer.c */int lexan(){ … tokenval = lookup(lexbuf); if (tokenval == 0) tokenval = insert(lexbuf, ID); return symtable[p].token;}

We simply initialize the global symbol

table with the set of keywords

April 21, 202312

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Handling Reserved KeywordsHandling Reserved Keywords

Page 13: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

13

morefactors div factor { print(‘DIV’) } morefactors | mod factor { print(‘MOD’) } morefactors | …

/* parser.c */morefactors(){ if (lookahead == DIV) { match(DIV); factor(); printf(“DIV”); morefactors(); } else if (lookahead == MOD) { match(MOD); factor(); printf(“MOD”); morefactors(); } else …}

April 21, 202313

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Handling Reserved KeywordsHandling Reserved Keywords

Page 14: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

14

push 5rvalue 2+rvalue 3*…

16

7

0

11

7

Instructions Stack Data

1

2

3

4

123456

pc

top…

April 21, 202314

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Abstract Stack MachinesAbstract Stack Machines

Page 15: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

15

• Abstract stack machine architecture– Emulated in software with JVM interpreter

– Just-In-Time (JIT) compilers

– Hardware implementations available

• Java bytecode– Platform independent

– Small

– Safe

• The JavaTM Virtual Machine Specification, 2nd ed.http://java.sun.com/docs/books/vmspec

April 21, 202315

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

The JVMThe JVM

Page 16: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

16

April 21, 202316

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Got it with following questionsGot it with following questions• Symbol table

– A data structure– Hold information – Collected incrementally

• Why we need s-table– Keep track of semantics of variable– Character string (lexeme)– Variables’ type – Its position in storage

• What further use of s-table– Forward reference– Scope information– Storage allocation, size…

Page 17: 1 October 16, 2015 1 October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

17

Thank you very much!

Questions?

April 21, 202317

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

A Simple Syntax-Directed TranslatorA Simple Syntax-Directed Translator