Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 432: Compiler Construction Lecture 1

Similar presentations


Presentation on theme: "CS 432: Compiler Construction Lecture 1"— Presentation transcript:

1 CS 432: Compiler Construction Lecture 1
Department of Computer Science Salisbury University Fall 2017 Instructor: Dr. Sophie Wang 11/8/2017

2 Goals of the Course Understand the concepts of compilers and interpreters. Parser, scanner, tokens Symbol tables, intermediate code Executors, code generators Compiler-compilers Learn important job skills that employers want. Work as a member of a small programming team. Understand Big Application. Use modern software engineering practices to develop a complex application. 11/8/2017

3 Course Notes Class website
Latest news Lecture notes and handouts The textbook and Java source code from the textbook: Writing Compilers and Interpreters, 3rd edition Second reference textbook: Generating Parsers with JavaCC, 2nd edition 11/8/2017

4 How to instruct a computer
In the beginning… 11/8/2017

5 How to instruct a computer
Programming bit-by-bit doesn’t scale we want to instruct the computer at a higher level of abstraction 11/8/2017

6 High-level Abstract Description to Low-level Implementation Details
President My poll ratings are low, lets invade a small nation General Cross the river and take defensive positions Sergeant Forward march, turn left Stop!, Shoot Foot Soldier 11/8/2017

7 How to instruct a computer
Programming bit-by-bit doesn’t scale we want to instruct the computer at a higher level of abstraction Natural Languages: Powerful, but… Ambiguous Same expression describes many possible action 11/8/2017

8 The computer understands you as well as your mother understands you.
“At last, a computer that understands you like your mother” McDonnell-Douglas Ad The computer understands you as well as your mother understands you. The computer understands that you like your mother. The computer understands you as well as it understands your mother. 11/8/2017

9 “ I know you believe you understood what you think I said, but I am not sure you realize that what you heard is not what I meant.” S. I. Hayakawa ( ), U.S. senator from California 11/8/2017

10 George Romney (1907-1995), U.S. presidential candidate
“I didn’t say that I didn’t say it. I said that I didn’t say I said it. I want to make that very clear.” 11/8/2017

11 Programming Languages
We want a high level of abstraction We also want precision (avoid ambiguity) conciseness expressiveness modularity The goal is to achieve: Programmer Productivity Also Efficiency and Performance 11/8/2017 11

12 The role of the compiler
Translate a program from high-level description to low-level implementation High-level of Abstraction Low-level of Abstraction Program written in a Programming Languages Assembly Language Translation Compiler 11/8/2017

13 What is a Compiler? A software utility that is extremely important for developing applications … … but is otherwise overlooked and taken for granted UNLESS … you can’t get your program to compile! _ 11/8/2017

14 A Compiler is a Translator
A compiler translates a program you’ve written ... in a high-level language C, C++, Java, Pascal, etc. ... into a low-level language assembly language or machine language ... that a computer can understand and eventually execute. 11/8/2017

15 More Definitions source program: the program (application) you write in a high-level language that the compiler will translate Usually stored in a source file. source language: the high-level language in which you write your source program Pascal object language: the low-level language (AKA target language) into which the compiler translates the source program Do not confuse object language with object-oriented language. Jasmin assembly language 11/8/2017

16 Still More Definitions
object program: your program after it has been translated into the object language target machine: the computer that will eventually execute the object program Java Virtual Machine (JVM) The JVM runs on your workstation or laptop (any computer that supports Java) implementation language: the language that the compiler itself is written in Java _ 11/8/2017

17 Overview of the Compilation Process
Sort.pas Sort.java What’s in this box? Pascal compiler (you will write this in Java) Java compiler translation: javac Sort.java ... Sort.asm Sort.class assembly language object program Jasmin assembler (provided for you) translation: Sort.class Java linker & loader (provided for you) Java linker & loader java Sort ... Java Virtual Machine (provided for you) Java Virtual Machine execution: 11/8/2017

18 What is Conceptual Design?
The conceptual design of a program is a high-level view of its software architecture. It includes the primary components of the program, how they’re organized, and how they interact with each other. It does not necessarily say how these components will be implemented. Rather, it allows you to examine and understand the components first without worrying about how you’re eventually going to develop them. 11/8/2017

19 Conceptual Design (Version 1)
Parser Controls the translation process. Repeatedly asks the scanner for the next token. Scanner Repeatedly reads characters from the source to construct tokens for the parser. Token A source language element identifier (name) number special symbol (+ - * / = etc.) reserved word Also reads from the source Source The source program 11/8/2017

20 Token A low-level element of the source language.
AKA lexeme Pascal language tokens Identifiers names of variables, types, procedures, functions, enumeration values, etc. Numbers integer and real (floating-point) Reserved words BEGIN END IF THEN ELSE AND OR NOT etc. Special symbols + - * / := < <= = >= > . , .. : ( ) [ ] { } ′ 11/8/2017

21 Parser Controls the translation process.
Repeatedly asks the scanner for the next token. Knows the syntax (“grammar”) of the source language’s statements and expressions. Analyzes the sequence of tokens to determine what kind of statement or expression it is translating. Verifies that what it’s seeing is syntactically correct. Flags any syntax errors that it finds and attempts to recover from them. What the parser does is called parsing – it parses the source program in order to translate it. AKA syntax analyzer 11/8/2017

22 Scanner Reads characters sequentially from the source in order to construct the next token whenever requested by the parser. Knows the syntax of the source language’s tokens. What the scanner does is called scanning – it scans the source program in order to extract tokens. AKA lexical analyzer _ 11/8/2017

23 Conceptual Design (Version 2)
We can architect a compiler with three major parts: 11/8/2017

24 Major Parts of a Compiler
Front end Parser, Scanner, Source, Token Intermediate tier Intermediate code (icode) “Predigested” form of the source code that the back end can process efficiently. Example: parse trees AKA intermediate representation (IR) Symbol table (symtab) Stores information about the symbols (such as the identifiers) contained in the source program. Back end Code generator Processes the icode and the symtab in order to generate the object code. Only the front end needs to be source language-specific. The intermediate tier and the back end can be language-independent! 11/8/2017

25 What Else Can Compilers Do?
Compilers allow you to program in a high-level language and think about your algorithms, not about machine architecture. Compilers provide language portability. You can run your C++ and Java programs on different machines because their compilers enforce language standards. Compilers can optimize and improve how well your programs execute. Optimize the object code for speed. Optimize the object code for size. Optimize the object code for power consumption. 11/8/2017

26 What about Interpreters?
An interpreter executes a source program instead of generating object code. It executes a source program using the intermediate code and the symbol table. It shares many of the components of a compiler. Instead of a code generator in the back end, it has an executor. _ 11/8/2017

27 Conceptual Design (Version 3)
A compiler and an interpreter can both use the same front end and intermediate tier. 11/8/2017

28 Comparing Compilers and Interpreters
A compiler generates object code, but an interpreter does not. Executing the source program from object code can be several orders of magnitude faster than executing the program by interpreting the intermediate code and the symbol table. But an interpreter requires less effort to get a source program to execute = faster turnaround time. An interpreter maintains control of the source program’s execution. Interpreters often come with interactive source-level debuggers that allow you to refer to source program elements, such as variable names. AKA symbolic debugger _ 11/8/2017

29 Compilers and Interpreters, cont’d
Therefore ... Interpreters are useful during program development. Compilers are useful to run released programs in a production environment. In this course, you will ... Develop a compiler/an interpretor for a language of your choice. 11/8/2017

30 A Pascal Interpreter The interpreter parses and executes a Pascal program without first generating object code. The parser generates intermediate code (icode) and a symbol table (symtab). The executor interprets the icode and the symtab in order to execute the Pascal program. _ 11/8/2017

31 Sample Pascal Program PROGRAM Newton; CONST epsilon = 1e-6; VAR number : integer; root, sqroot : real; BEGIN REPEAT writeln; write('Enter new number (0 to quit): '); read(number); IF number = 0 THEN BEGIN writeln(number:12, 0.0:12:6); END ELSE IF number < 0 THEN BEGIN writeln('*** ERROR: number < 0'); ELSE BEGIN sqroot := sqrt(number); writeln(number:12, sqroot:12:6); root := 1; root := (number/root + root)/2; writeln(root:24:6, 100*abs(root - sqroot)/sqroot:12:2, '%') UNTIL abs(number/sqr(root) - 1) < epsilon; UNTIL number = 0 END. read, write, and writeln are standard (built-in) Pascal procedures. abs, sqr, and sqrt are standard Pascal functions. Command line: java –classpath classes Pascal execute newton.pas 11/8/2017

32 Why Study Compilers and Interpreters?
Compilers and interpreters are complex programs. The subject matter has a well-established body of theory and techniques. Don’t take compilers and interpreters for granted! Understand how they work and you’ll become a better programmer. _ 11/8/2017

33 Why Study Compilers and Interpreters?
Programming challenge. This is one of the largest, most complex programs you’ll ever write. Develop it incrementally and have working code at each step. Use good object-oriented design and software engineering practices. Useful and fun. Create a compiler or an interpreter for a programming language that you invent that’s suitable for your application domain. a procedure-oriented language like Pascal a scripting language like the Linux shell Learn programming techniques and algorithms that you can apply to other applications. _ 11/8/2017

34 Small Teams Compilers and interpreters are generally written by small teams. Form your own teams of 2 or 3 students each. Choose your team members wisely! Be sure you’ll be able to meet and communicate with each other and work together well. Each team member will receive the same score on the team assignments and on the team compiler project. me your team name and the list of team members by Tuesday, September 5. 11/8/2017

35 An Open Source Pascal IDE
You can use Lazarus for Pascal development. Runs on Windows, Mac OS X, and Linux. It’s an integrated development environment (IDE). Your task # 1. Use Lazarus to develop your first Pascal program that displays “Hello World!”. Crash course to learning Pascal. 11/8/2017

36 Compiler Project Each team develops a working compiler for a procedure-oriented source language. A subset of c. A subset of any other procedure-oriented languages. Your own procedure-oriented language. Use the JavaCC compiler-compiler to generate a parser and a scanner for your source language. You will need to provide a grammar for the language. Re-use any other compiler code from the class. Start thinking about and planning for your project early in the semester. 11/8/2017

37 Postmortem Report At the end of the semester, each student will individually turn in a short (1 or 2 pp.) report: A brief description of what you learned in the course. An assessment of your personal accomplishments for your project team. An assessment of each of your other project team members. _ 11/8/2017


Download ppt "CS 432: Compiler Construction Lecture 1"

Similar presentations


Ads by Google