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
17
Embed
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.
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
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
• 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