Docsity

Introduction to Compiling - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Introduction to Compiling - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 1: Introduction to Compiling

Introduction to Compilers

  • As a Discipline, Involves Multiple CSE Areas
    • Programming Languages and Algorithms
    • Software Engineering & Theory / Foundations
    • Computer Architecture & Operating Systems
  • But, Has Surprisingly Simplistic Intent:

Compiler

Source program

Target Program

Error messages

Diverse & Varied

Viewing C from Compiler Theory

• Tokens or Patterns for the “Words” of the

Programming Language

  • Specified in Flex

• Grammar Rules that Describe the Allowable

Sentences of the Programming Language

  • Specified in Bison

• Other Stages of Compilation

  • Semantic and Type Checking Analysis
  • Intermediate Code Generation
  • Code Optimization
  • Final (Relocatable) Code Generation

Viewing C from Usage Side

• ANSI C Specification of Lexical Structure in Flex

• ANSI C Specification of Grammar Rules in Bison

• Multi-Stage Compilation Process from Source

Code to Preprocessed Code to Assembly Code to

Object Code to Executable

  • Preprocess: gcc –E main.c > main.e
  • Assembly: gcc –S main.c Generates main.s
  • Object: gcc –c main.c Generates main.o
  • Executable: gcc main.c Generates a.out

C Lexical Specification in Flex

  • http://www.lysator.liu.se/c/ANSI-C-grammar-l.html

D [0-9] L [a-zA-Z_] H [a-fA-F0-9] E [Ee][+-]?{D}+ FS (f|F|l|L) IS (u|U|l|L)* %{ #include #include "y.tab.h" void count(); %} %% "/*" { comment(); }

C Flex Continued …

"auto" { count(); return(AUTO); } "break" { count(); return(BREAK); } "case" { count(); return(CASE); } "char" { count(); return(CHAR); } "const" { count(); return(CONST); } "continue" { count(); return(CONTINUE); } "default" { count(); return(DEFAULT); } "do" { count(); return(DO); } "double" { count(); return(DOUBLE); } "else" { count(); return(ELSE); } "enum" { count(); return(ENUM); } "extern" { count(); return(EXTERN); } "float" { count(); return(FLOAT); } "for" { count(); return(FOR); } "goto" { count(); return(GOTO); } "if" { count(); return(IF); } "int" { count(); return(INT); }

C Flex Continued …

{L}({L}|{D})* { count(); return(check_type()); } 0[xX]{H}+{IS}? { count(); return(CONSTANT); } 0{D}+{IS}? { count(); return(CONSTANT); } {D}+{IS}? { count(); return(CONSTANT); } L?'(\.|[^\'])+' { count(); return(CONSTANT); } {D}+{E}{FS}? { count(); return(CONSTANT); } {D}"."{D}+({E})?{FS}? { count(); return(CONSTANT); } {D}+"."{D}({E})?{FS}? { count(); return(CONSTANT); } L?"(\.|[^\"])*" { count(); return(STRING_LITERAL); }

What Do These Represent?

C Flex Continued …

"..." { count(); return(ELLIPSIS); } ">>=" { count(); return(RIGHT_ASSIGN); } "<<=" { count(); return(LEFT_ASSIGN); } "+=" { count(); return(ADD_ASSIGN); } "-=" { count(); return(SUB_ASSIGN); } "*=" { count(); return(MUL_ASSIGN); } "/=" { count(); return(DIV_ASSIGN); } "%=" { count(); return(MOD_ASSIGN); } "&=" { count(); return(AND_ASSIGN); } "^=" { count(); return(XOR_ASSIGN); } "|=" { count(); return(OR_ASSIGN); } ">>" { count(); return(RIGHT_OP); } "<<" { count(); return(LEFT_OP); } "++" { count(); return(INC_OP); } "--" { count(); return(DEC_OP); } "->" { count(); return(PTR_OP); }

C Bison Specification

  • http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP %token GE_OP EQ_OP NE_OP %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN %token XOR_ASSIGN OR_ASSIGN TYPE_NAME ADD_ASSIGN

%token TYPEDEF EXTERN STATIC AUTO REGISTER %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST %token VOLATILE VOID %token STRUCT UNION ENUM ELLIPSIS

%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE %token BREAK RETURN

%start translation_unit %% Docsity.com

Bison Spec Continued …

primary_expression : IDENTIFIER | CONSTANT | STRING_LITERAL | '(' expression ')' ;

postfix_expression : primary_expression | postfix_expression '[' expression ']' | postfix_expression '(' ')' | postfix_expression '(' argument_expression_list ')' | postfix_expression '.' IDENTIFIER | postfix_expression PTR_OP IDENTIFIER | postfix_expression INC_OP | postfix_expression DEC_OP ;

argument_expression_list : assignment_expression | argument_expression_list ',' assignment_expression

These are

Grammar Rules

Bison Spec Continued …

statement_list : statement | statement_list statement ;

statement : labeled_statement | compound_statement | expression_statement | selection_statement | iteration_statement | jump_statement ;

labeled_statement : IDENTIFIER ':' statement | CASE constant_expression ':' statement | DEFAULT ':' statement ;

List of

Statements

and

Different

Statements

Bison Spec Continued …

expression_statement : ';' | expression ';' ;

selection_statement : IF '(' expression ')' statement | IF '(' expression ')' statement ELSE statement | SWITCH '(' expression ')' statement ;

iteration_statement : WHILE '(' expression ')' statement | DO statement WHILE '(' expression ')' ';' | FOR '(' expression_statement expression_statement ')' statement | FOR '(' expression_statement expression_statement expression ')' statement ;

From Source to Preprocessing:

What Does main.e Contain?

1 "main.c"

1 ""

1 ""

1 "main.c"

1 "/usr/include/stdio.h" 1 3 4

28 "/usr/include/stdio.h" 3 4

1 "/usr/include/features.h" 1 3 4

330 "/usr/include/features.h" 3 4

1 "/usr/include/sys/cdefs.h" 1 3 4

348 "/usr/include/sys/cdefs.h" 3 4

1 "/usr/include/bits/wordsize.h" 1 3 4

349 "/usr/include/sys/cdefs.h" 2 3 4

331 "/usr/include/features.h" 2 3 4

354 "/usr/include/features.h" 3 4

1 "/usr/include/gnu/stubs.h" 1 3 4

… etc ….

From Source to Preprocessing:

What Does main.e Contain?

… missing almost 900 lines of code …

4 "main.c" 2

main() { char day[10]; int m, d, y;

m = 9; d = 7; y = 2011; strcpy(day, "Wednesday");

printf("Today is: "); printf("%s %d/%d/%d\n", day, m, d, y); }