# Compilers and Interpreters --- CS 135 // 2021-04-06 ## Administrivia - All labs have been graded and returned on codePost - Please let me know if you have any questions about the grading # Compilers and Interpreters ## Discussion Questions 1. What is a **compiler**? 2. What is an **interpreter**? 3. What are the differences between them? ## Responses - A **compiler** is: + Takes code from one language and translates to another + Only translates (and optimizes) - An **interpreter** is: + Reads and executes a program immediately ## Compiled VS Interpreted - Which of these languages are compiled and which are interpreted? + Python + Java + C + Haskell ## Components Every compiler/interpreter must perform: --- - **Lexical Analysis** (aka "Lexing", or "Tokenizing") + Turns source code into a sequence of **tokens** ```java boolean result = "123".equals(123); ``` ## Components Every compiler/interpreter must perform: --- - **Syntax Analysis** (aka "Parsing") + Constructs a **parse tree** from a sequence of tokens according to the grammar of the language - **Semantic Analysis** + Type checking and other compile-time verification ## Outline - Over the next five weeks, we are going to write an **interpreter** for a new programming language - We will focus on how features of many programming languages are implemented - Very little time will be given to lexing/parsing + General-purpose parsing libraries and tools exist to perform this # Features ## of Programming Languages ## Classifying Languages - We just saw 16 different presentations on programming languages and their features - What are the common features and categories of programming languages? - Think about what all the "buzzwords" were in these presentations that differentiate them - In your small groups, make a list of these buzzwords/categories along with brief definitions of what they mean - We will discuss them as a large group after ## Responses - Strong typing / weak typing - Static typing / dynamic typing + Static means that your types are checked at compile-time + Dynamic means that your types are checked at runtime - Object-oriented, functional, procedural, imperative + OO is a very specific style of programming where objects contain both data and methods for manipulating that data ## Responses - Open source / closed source - High-level / low-level / machine-level - Pure / impure (side-effects) ## Languages as a Collection of Features - These days, languages are big "blobs" of features - When learning a new language, it is helpful to learn its basic features - Every language has a (relatively) simple "core" language that it builds from ## Desugaring - We will develop the core of a programming language interpreter, one feature at a time - We will also be adding "syntactic sugar" to the language and a desugaring process to reduce more complicated features to the core features