Chapter 1: Introduction to CompilingIntroduction to CompilersWhat is the Compilation Process? Consider the main.c C Program?Viewing C from Compiler TheoryViewing C from Usage SideWhat are Tokens? Smallest Individual Units that are RecognizedC Lexical Specification in FlexC Flex Continued …Slide 9Slide 10Slide 11Slide 12C Bison SpecificationBison Spec Continued …Slide 15Slide 16Slide 17Reviewing Compilation Process in CFrom Source to Preprocessing: What Does main.e Contain?Slide 20From Preprocessing to Assembly: What Does main.s Contain?What are Final Two Steps?Programming Languages Offer …InterpreterCompilerMixedClassifications of CompilersCompiler STructureImportant NotesSlide 30Summary of Various LanguagesThe Many Phases of a CompilerWhat Does Relocatable Mean?The Analysis Task For CompilationPhase 1. Lexical AnalysisLexical AnalysisPhase 2. Hierarchical Analysis aka Parsing or Syntax AnalysisSyntax Analysis (parsing)What is a Grammar?Summary so far…Semantic AnalysisPhase 3. Semantic AnalysisSlide 43Analysis in Text FormattingSupporting Phases/ Activities for AnalysisSummary so far...From Analysis to SynthesisThe Synthesis Task For CompilationIntermediate CodeIR Code exampleCode OptimizationMachine Code GenerationMachine code generationAssemblersLinker & LoaderReviewing the Entire ProcessSlide 57Compiler Cousins: Preprocessors 1. Macro Preprocessor2. File Inclusion3. Rational Preprocessors4. Language Extensions for a Database SystemThe Grouping of PhasesCompiler Construction ToolsModern CompilersModern Compiler ExampleCH1.1CSE4100Chapter 1: Introduction to CompilingChapter 1: Introduction to CompilingProf. Steven A. DemurjianComputer Science & Engineering DepartmentThe University of Connecticut371 Fairfield Way, Unit 2155Storrs, CT [email protected]://www.engr.uconn.edu/~steve(860) 486 - 4818Material for course thanks to:Laurent MichelAggelos KiayiasRobert LeBarreCH1.2CSE4100Introduction to CompilersIntroduction to CompilersAs a Discipline, Involves Multiple CSE AreasAs a Discipline, Involves Multiple CSE AreasProgramming Languages and Algorithms Software Engineering & Theory / Foundations Computer Architecture & Operating SystemsBut, Has Surprisingly Simplistic Intent:But, Has Surprisingly Simplistic Intent:CompilerSource programTarget ProgramError messagesDiverse & VariedCH1.3CSE4100What is the Compilation Process?What is the Compilation Process?Consider the main.c C Program?Consider the main.c C Program?#include <stdio.h>#include <ctype.h>#include <string.h>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);}CH1.4CSE4100Viewing C from Compiler TheoryViewing C from Compiler TheoryTokens or Patterns for the “Words” of the Tokens or Patterns for the “Words” of the Programming LanguageProgramming LanguageSpecified in FlexGrammar Rules that Describe the Allowable Grammar Rules that Describe the Allowable Sentences of the Programming LanguageSentences of the Programming LanguageSpecified in BisonOther Stages of CompilationOther Stages of CompilationSemantic and Type Checking AnalysisIntermediate Code GenerationCode OptimizationFinal (Relocatable) Code GenerationCH1.5CSE4100Viewing C from Usage SideViewing C from Usage SideANSI C Specification of Lexical Structure in FlexANSI C Specification of Lexical Structure in FlexANSI C Specification of Grammar Rules in BisonANSI C Specification of Grammar Rules in BisonMulti-Stage Compilation Process from Source Multi-Stage Compilation Process from Source Code to Preprocessed Code to Assembly Code to Code to Preprocessed Code to Assembly Code to Object Code to ExecutableObject Code to ExecutablePreprocess: gcc –E main.c > main.e Assembly: gcc –S main.c Generates main.sObject: gcc –c main.c Generates main.oExecutable: gcc main.c Generates a.outCH1.6CSE4100What are Tokens?What are Tokens?Smallest Individual Units that are RecognizedSmallest Individual Units that are Recognized#include <stdio.h>#include <ctype.h>#include <string.h>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);}CH1.7CSE4100C Lexical Specification in FlexC Lexical Specification in Flexhttp://www.lysator.liu.se/c/ANSI-C-grammar-l.htmlhttp://www.lysator.liu.se/c/ANSI-C-grammar-l.htmlD [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 <stdio.h>#include "y.tab.h"void count();%}%%"/*" { comment(); }CH1.8CSE4100C Flex Continued …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); }CH1.9CSE4100C Flex Continued …C Flex Continued …"long" { count(); return(LONG); }"register" { count(); return(REGISTER); }"return" { count(); return(RETURN); }"short" { count(); return(SHORT); }"signed" { count(); return(SIGNED); }"sizeof" { count(); return(SIZEOF); }"static" { count(); return(STATIC); }"struct" { count(); return(STRUCT); }"switch" { count(); return(SWITCH); }"typedef" { count(); return(TYPEDEF); }"union" { count(); return(UNION); }"unsigned" { count(); return(UNSIGNED); }"void" { count(); return(VOID); }"volatile" { count(); return(VOLATILE); }"while" { count(); return(WHILE); }What TOKEN is Missing?CH1.10CSE4100C Flex Continued …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?CH1.11CSE4100C Flex Continued …C Flex Continued …"..." { count(); return(ELLIPSIS); }">>=" { count(); return(RIGHT_ASSIGN); }"<<=" { count();
View Full Document