in Action Lexer Parser Semantic Analyzer Code Generation Words Tokens Sentences Symbol table Uniqueness Type matching Translation Source Code è Intermediate Code Intermediate Code è Machine or Binary Code Analysis Compilation Assembly
Lexer Parser Semantic Analyzer Code Generation Report Errors JTable with Tokens Report Errors Draw a Tree Report Errors Create Symbol Table Following lectures
Virtual Machine CPU ALU Register PC Memory program Code sto 0, s sto 0, d sto 0, c sto 0, d lod s, 0 lit “s”, 0 opr 14, 0 jmc #a1, false lod b, 0 Symbol Table
LOD <variable>, <register_id> Search for <variable> in the symbol table Read its value Put the value of <variable> in the CPU register Examples: LOD a, 0 LOD hello, 0 LOD cse340, 0
STO <variable>, <register_id> Read a value from the CPU register Search for <variable> in the symbol table Store the value into the variable Examples: STO a, 0 STO hello, 0 STO cse340, 0
Code: { int a; int b; a = 10; b = a; Symbol Table: Type Name Scope Value int a global 0 int b global 0 Intermediate (Object) Code: LIT 10,0 STO a,0 LOD a,0 STO b,0
OPR <operation>, <register_id> Read one or two values from the CPU register Do the operation <operation> Put the result into the CPU register Examples: OPR 1, 0 ; return OPR 2, 0 ; add OPR 3, 0 ; subtract
Action 0 Exit program 1 Return 2 ADD: POP A, POP B, PUSH B+A 3 SUBTRACT: POP A, POP B, PUSH B-A 4 MULTIPLY: POP A, POP B, PUSH B*A 5 DIVIDE: POP A, POP B, PUSH B/A 6 MOD: POP A, POP B, PUSH (B mod A) 7 POW: POP A, POP B, PUSH (A to the power B) 8 OR: POP A, POP B, PUSH (B or A) 9 AND: POP A, POP B, PUSH (B and A) 10 NOT: POP A, PUSH (not A) 11 TEST GREATER THAN: POP A, POP B, PUSH (B>A) 12 TEST LESS THAN: POP A, POP B, PUSH (B<A) 13 TEST GREATER THAN OR EQUAL: POP A, POP B, PUSH (B>=A)
Action 14 TEST LESS THAN OR EQUAL: POP A, POP B, PUSH (B<=A) 15 TEST EQUAL: POP A, POP B, PUSH (B=A) 16 TEST NOT EQUAL: POP A, POP B, PUSH (B<>A) 17 18 clear screen 19 read a value from the standard input 20 print a value to the standard output 21 print a value to the standard output and a newline character
int a; int b; a = 10; b = a; a = a * 10; b = 2 + 3 + 4; } Type Name Scope Value int a global 0 int b global 0 LIT 10, 0 STO a, 0 LOD a, 0 STO b, 0 LOD a, 0 LIT 10, 0 OPR 4, 0 ; multiply STO a, 0 LIT 2, 0 LIT 3, 0 OPR 2, 0 ; add LIT 4, 0 OPR 2, 0 ; add STO b, 0 OPR 1,0 OPR 0,0
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.