1 october 16, 2015 1 october 16, 2015october 16, 2015october 16, 2015 azusa, ca sheldon x. liang ph....
TRANSCRIPT
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
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
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
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…
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
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
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)
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)
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)
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
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
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
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
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
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
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…
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