jagomart
digital resources
picture1_Cimplementation


 110x       Filetype PDF       File size 0.34 MB       Source: www.cs.usfca.edu


File: Cimplementation
modern compiler design associated supplemental materials cimplementation details david galles contents 1 introduction explanation 3 1 1 what is this document 3 1 2 how to use this document 3 ...

icon picture PDF Filetype PDF | Posted on 04 Feb 2023 | 2 years ago
Partial capture of text on file.
                Modern Compiler Design
                Associated Supplemental Materials
                   CImplementation Details
                       David Galles
                 Contents
                 1 Introduction & Explanation                                                                                            3
                     1.1   What Is This Document? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            3
                     1.2   How To Use This Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            3
                     1.3   So Where Are Chapters 3 and 4? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .            3
                     1.4   Where Can I Find Necessary Files for Creating a Compiler in C? . . . . . . . . . . . . . . . .                3
                 2 Lexical Analysis                                                                                                      4
                     2.1    Lex – A Lexical Analyzer Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           4
                           2.1.1    Structure of a Lex file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       5
                           2.1.2   Named Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           6
                           2.1.3   Tokens With Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .          7
                           2.1.4    Dealing with White Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           8
                           2.1.5    Keeping Track of Token Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           9
                           2.1.6   States in Lex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       9
                           2.1.7    Using Lex with Other C Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           11
                           2.1.8    Advanced Lex Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         11
                     2.2    Creating a Lexical Analyzer for simpleJava in Lex . . . . . . . . . . . . . . . . . . . . . . . .           14
                           2.2.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       14
                           2.2.2    Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        18
                           2.2.3    Project “Gotcha”s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       18
                           2.2.4    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      19
                 5 Bottom-Up Parsing & Yacc                                                                                            21
                     5.1   Yacc – Yet Another Compiler Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .             21
                           5.1.1   Structure of a Yacc File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       21
                           5.1.2   Dealing With Parsing Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .          22
                           5.1.3   Tokens With Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         23
                           5.1.4   When Yacc Has Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .           23
                           5.1.5   Operator Precedence        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   26
                     5.2   Writing a Parser For simpleJava Using Yacc           . . . . . . . . . . . . . . . . . . . . . . . . . . .   29
                           5.2.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       29
                           5.2.2   Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       29
                           5.2.3    Project “Gotcha”s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       30
                           5.2.4    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      30
                     5.3   Exercises    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   32
                 6 Abstract Syntax Trees in C                                                                                          33
                     6.1    Implementing Trees in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         33
                           6.1.1    Representing trees – structs and unions . . . . . . . . . . . . . . . . . . . . . . . . . .         33
                           6.1.2    Using Constructors in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         35
                           6.1.3    Traversing Trees in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       36
                     6.2    Yacc Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      36
                           6.2.1    Simple Yacc Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       36
                                                                             1
                          6.2.2   Using the Yacc Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     37
                          6.2.3    ASimple Yacc Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .       38
                          6.2.4    Creating an Abstract Syntax Tree for a Simple Language . . . . . . . . . . . . . . . .            39
                          6.2.5    Tokens With Complicated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .          43
                    6.3    Creating an Abstract Syntax Tree for simpleJava Using C and Yacc . . . . . . . . . . . . . .              43
                          6.3.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     43
                          6.3.2    Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      45
                          6.3.3    Project “Gotcha’s” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      45
                          6.3.4    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    48
                 7 Semantic Analysis in C                                                                                            49
                    7.1    Types in simpleJava . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     49
                          7.1.1    Built-in Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    49
                          7.1.2    Array Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     51
                          7.1.3    Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     51
                    7.2   Implementing Environments in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         51
                    7.3    Writing a Suite of Functions for Semantic Analysis . . . . . . . . . . . . . . . . . . . . . . . .        52
                          7.3.1    Analyzing a simpleJava Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         52
                          7.3.2    Analyzing simpleJava Expressions in C . . . . . . . . . . . . . . . . . . . . . . . . . .         52
                          7.3.3    Analyzing simpleJava Statements in C . . . . . . . . . . . . . . . . . . . . . . . . . .          54
                    7.4    Semantic Analyzer Project in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        56
                          7.4.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     56
                          7.4.2    Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      57
                          7.4.3    Project “Gotcha”s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     57
                          7.4.4    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    57
                 8 Generating Abstract Assembly in C                                                                                 59
                    8.1    Creating Abstract Assembly Trees in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .         59
                          8.1.1    Assembly Language Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .        59
                          8.1.2    Interface for Building Abstract Assembly Trees in C . . . . . . . . . . . . . . . . . . .         59
                          8.1.3    Adding Code to the Semantic Analyzer to Build Abstract Assembly Trees . . . . . .                 62
                          8.1.4    Functions That Return Abstract Assembly Trees . . . . . . . . . . . . . . . . . . . . .           62
                          8.1.5    Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   62
                          8.1.6    Function Prototypes and Function Definitions . . . . . . . . . . . . . . . . . . . . . .           64
                    8.2    Building Assembly Trees for simpleJava in C . . . . . . . . . . . . . . . . . . . . . . . . . . .         64
                          8.2.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     64
                          8.2.2    Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      65
                          8.2.3    Project “Gotcha’s” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      65
                          8.2.4    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    65
                 9 Code Generation                                                                                                   66
                    9.1    Project Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .     66
                          9.1.1    Project Difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      66
                          9.1.2    Project “Gotcha’s” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      66
                          9.1.3    Provided Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    67
                                                                            2
              Chapter 1
              Introduction & Explanation
              1.1      What Is This Document?
              This document is a companion to the textbook Modern Compiler Design by David Galles. The textbook
              covers compiler design theory, as well as implementation details for writing a compiler using JavaCC and
              Java. This document contains all of the implementation details for writing a compiler using C, Lex, and
              Yacc. Note that this document is not self contained, and is only meant to be used in conjunction with the
              textbook. Much of the material in this document will be difficult to understand if read in isolation.
              1.2      How To Use This Document
              This document is designed to be used in conjunction with the textbook Compiler Design. The chapters
              in this document correspond to the chapters in the textbook. For instance, Chapter 2 in the text covers
              lexical analysis, and Chapter 2 in this document covers writing a lexical analyzer in C. First, read the main
              textbook, starting with Chapter 1. When the textbook covers implementation details using Java, refer to
                                                                                           1
              the corresponding chapter of this document for an equivalent description in C.
              1.3      So Where Are Chapters 3 and 4?
              Chapter 3 in the textbook covers Context-Free Grammars, and is a “theory only” chapter that contains
              no implementation details. Hence, there is no need for additional materials covering C implementation for
              Chapter 3. Chapter 4 in the textbook covers Top-Down parsing, while Chapter 5 in the textbook covers
              Bottom-Up parsing. Since JavaCC is a Top-Down parser generator, and Yacc is a Bottom-Up parser gener-
              ator, Chapter 4 has no associated C implementation details (and hence does not appear in this document),
              which chapter 5 has no Java implementation details (though Bottom-Up parsing theory is indeed covered in
              the textbook.
              1.4      Where Can I Find Necessary Files for Creating a Compiler in
                       C?
              The CD on which you found this document contains all necessary files for writing a compiler in either C or
              Java. Youcanalsofindupdatedversionsofthesefiles,aswellaserrata,atwww.cs.usfca.edu/galles/compilertext
                 1With one exception – JavaCC is a Top-Down parser generator, so the Java implementation details for parser generation
              are in Chapter 4, which covers Top-Down parsing. Yacc is a Bottom-Up parser generator, so the C implementation details for
              parser generation are in Chapter 5 of this document, which covers Bottom-Up parsing.
                                                                  3
The words contained in this file might help you see if this file matches what you are looking for:

...Modern compiler design associated supplemental materials cimplementation details david galles contents introduction explanation what is this document how to use so where are chapters and can i find necessary files for creating a in c lexical analysis lex analyzer generator structure of le named regular expressions tokens with values dealing white space keeping track token positions states using other programs advanced features simplejava project denition diculty gotcha s provided bottom up parsing yacc yet another file errors when has conicts operator precedence writing parser exercises abstract syntax trees implementing representing structs unions constructors traversing actions simple the stack asimple calculator an tree language complicated semantic types built array class environments suite functions analyzing program statements generating assembly labels interface building adding code build...

no reviews yet
Please Login to review.