Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction

2 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) October 16, 2015 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Building a Simple Compiler

3 3 October 16, 2015 3 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Symbol Table

4 4 October 16, 2015 4 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Keep in mind following questions Symbol table –A–A data structure –H–Hold information –C–Collected incrementally Why we need s-table –K–Keep track of semantics of variable –C–Character string (lexeme) –V–Variables’ type –I–Its position in storage What further use of s-table –F–Forward reference –S–Scope information –S–Storage allocation, size…

5 5 October 16, 2015 5 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Content in Symbol Tables

6 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 October 16, 2015 6 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Symbol Table

7 7 October 16, 2015 7 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Two Common Approaches(1/3)

8 8 October 16, 2015 8 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Two Common Approaches(2/3)

9 9 October 16, 2015 9 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Two Common Approaches(3/3)

10 10 October 16, 2015 10 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Forward Reference

11 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(); } October 16, 2015 11 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Identifiers

12 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 October 16, 2015 12 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Handling Reserved Keywords

13 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 … } October 16, 2015 13 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Handling Reserved Keywords

14 14 push 5 rvalue 2 + rvalue 3 * … 16 7 0 11 7 … InstructionsStackData 1 2 3 4 1 2 3 4 5 6 pc top … October 16, 2015 14 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Abstract Stack Machines

15 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 Java TM Virtual Machine Specification, 2nd ed. http://java.sun.com/docs/books/vmspec October 16, 2015 15 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction The JVM

16 16 October 16, 2015 16 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Got it with following questions Symbol table –A–A data structure –H–Hold information –C–Collected incrementally Why we need s-table –K–Keep track of semantics of variable –C–Character string (lexeme) –V–Variables’ type –I–Its position in storage What further use of s-table –F–Forward reference –S–Scope information –S–Storage allocation, size…

17 17 Thank you very much! Questions? October 16, 2015 17 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction A Simple Syntax-Directed Translator


Download ppt "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."

Similar presentations


Ads by Google