What Do Compilers Do 1 A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages. Ignore machine-dependent details for programmer Programming Language (Source) Compiler Machine Language (Target)
The Structure of a Compiler 2 Scanner Parser Semantic Routines Semantic Routines Code Generator Code Generator Optimizer Source Program TokensSyntactic Structure Symbol and Attribute Tables Symbol and Attribute Tables (Used by all Phases of The Compiler) (Character Stream) Intermediate Representation Target machine code
Machine Language – Assembly Language – High-Level Language entry main,^m sub12 #12,sp jsb C$MAIN_ARGS moveb $CHAR_STRING_CON pusha1 -8(fp) pusha1 (r2) calls #2,SCANF pusha1 -12(fp) pusha1 3(r2) calls #2,SCANF mull3 -8(fp),-12(fp),- pusha 6(fp) calls #2,PRINTF clrl r0 ret #include int main(void) { int n1, n2,product; printf(“Enter two numbers : “); scanf(“%d %d”,&n1,&n2); product = n1 * n2; printf(“%d”,product); return 0; } The only language the computer can understand is machine language (binary language). A high level language is an English like language where one instruction typically translates into a series of machine- language instructions. A low level language corresponds closely to machine code so that a single low-level language instruction translates to a single machine language instruction.