27
1 CSE 3341: Principles of Programming Languages PL Overview Jeremy Morris

CSE 3341: Principles of Programming Languages PL Overviewweb.cse.ohio-state.edu/~morris.343/Teaching/CSE3341/...CSE 3341: Principles of Programming Languages PL Overview Jeremy Morris

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

  • 1

    CSE 3341: Principles of Programming Languages

    PL OverviewJeremy Morris

  • Why are we here?

    Quick quiz: How many programming languages do you know? How many programming languages do you think you will know

    ten years from now? How many programming languages do you think you will need to

    know before you retire? Who thinks they will never have to? How many programming languages do you think there are?

    2

  • Why are we here?

    How many programming languages do you think there are? Hard to say – new ones come along constantly The “99 Bottles of Beer” website currently has implementations of

    the famous song in over 1500 languages Many variants of the same languages (BASIC, FORTRAN, etc.) but

    many distinct languages as well Why do programming languages exist?

    Why are there so many of them?

    3

    http://www.99-bottles-of-beer.net/p.html

  • Why are we here?

    Understanding language design and implementation can help you learn new languages. See connections between languages in similar families. Understand the common things that all languages share. Understand why languages are designed in certain ways Be able to have intelligent and informed opinions on

    programming languages Be able to chose the right tool for the right job

    4

  • Why are we here?

    A few other reasons to study programming languages To understand what the machine is doing when it compiles your

    code. To be able to understand features in languages outside of your

    own experience. And possibly think of ways to simulate them in a language that

    doesn’t have them. To be able to make intelligent choices about your

    implementations. Some solutions may be better or worse in the language of your

    choice. To understand the algorithms used for programming languages

    and apply them to other tasks.

    5

  • How do we study a language?

    Syntax What do legal programs written in the language look like?

    Semantics What do the various instructions in the language do when

    executed?

    Methodology How are you supposed to use the language? What features does it have? What kinds of problems is it supposed to solve?

    6

  • Types of Programming Languages We often talk about two different categories of

    programming languages: Imperative Declarative

    The author also talks about different subcategories of programming languages: Functional Logic Object-oriented Scripting von Neumann

    Never heard of this one? Reasons for that. “Fish don’t have a word for water…”

    7

  • Declarative vs. Imperative

    Imperative Tell the computer how to perform an action Tied to control flow – do this, then do this, then do this

    Declarative Describe what the result should look like Abstracted from control flow (to a degree anyway) A few different points of view on what “declarative” means:

    “Not imperative” – which is not helpful Sometimes described as “rule based” or “function based” Often has direct ties to mathematics – especially logic Lacking in “side effects” (or at least minimizing side effects)

    8

  • Imperative example - Javapublic static int gcd(int a, int b) {

    while (a != b) {if (a > b) {

    a = a – b;

    }

    else {b = b – a;

    }

    }

    return a;}

    9

  • Declarative example - PROLOG

    gcd(A,B,G) :- A = B, G = A.

    gcd(A,B,G) :- A > B, C is A-B, gcd(C,B,G).

    gcd(A,B,G) :- B > A, C is B-A, gcd(C,A,G).

    10

  • Declarative example - SQL

    SELECT Name, Title, IdNum

    FROM PEOPLE

    WHERE DeptNo = 6 AND Salary > 40000;

    11

  • Functional example - Scheme

    (define gcd

    (lambda (a b)

    (cond ( (= a b) a)

    ( (> a b) (gcd (- a b) b))

    ( else (gcd (- b a) a)))))))

    12

  • Compilation

    Abstract overview of compilation

    13

    SourceCode Compiler

    Object Code

  • Compilation (more formally)

    𝑃𝑃𝐿𝐿 - Program in language L 𝑃𝑃𝑀𝑀 - Program in language M 𝐶𝐶𝐿𝐿→𝑀𝑀𝑀𝑀 - Compiler in language M that translates L to M

    14

    𝑃𝑃𝐿𝐿 𝐶𝐶𝐿𝐿→𝑀𝑀𝑀𝑀 𝑃𝑃𝑀𝑀

  • Execution (run time)

    At runtime, the program written in machine languageexecutes

    15

    𝑃𝑃𝑀𝑀Input Data Output Behavior

  • A new language?

    Suppose we now have a new language L’ We could write a new compiler Or…

    16

    𝑃𝑃𝐿𝐿 𝐶𝐶𝐿𝐿→𝑀𝑀𝑀𝑀 𝑃𝑃𝑀𝑀

  • A new language?

    17

    𝑃𝑃𝐿𝐿 𝐶𝐶𝐿𝐿→𝑀𝑀𝑀𝑀 𝑃𝑃𝑀𝑀

    𝑃𝑃𝐿𝐿′ 𝐶𝐶𝐿𝐿′→𝐿𝐿𝑀𝑀 𝑃𝑃𝐿𝐿

  • Intermediate Language

    18

    𝑃𝑃𝐼𝐼 𝐶𝐶𝐼𝐼→𝑀𝑀𝑀𝑀 𝑃𝑃𝑀𝑀

    𝑃𝑃𝐿𝐿 𝐶𝐶𝐿𝐿→𝐼𝐼𝑀𝑀 𝑃𝑃𝐼𝐼

  • Interpretation

    Abstract overview of interpretation

    19

    Source Code Interpreter Output Behavior

    Input Data

  • Interpretation (more formally)

    𝑃𝑃𝐿𝐿 - Program in language L 𝐼𝐼𝐿𝐿𝑀𝑀 - Interpreter in language M that translates L outputs

    20

    𝑃𝑃𝐿𝐿 𝐼𝐼𝐿𝐿𝑀𝑀 Output Behavior

    Input Data

  • Layers of Interpretation

    𝑃𝑃𝐿𝐿 - Program in language L is an interpreter for language L’

    𝐼𝐼𝐿𝐿𝑀𝑀 - Interpreter in language M that translates L outputs 𝑃𝑃𝐿𝐿′ - Program for language L’ used as input to the

    interpreted program 𝑃𝑃𝐿𝐿

    21

    𝑃𝑃𝐿𝐿 𝐼𝐼𝐿𝐿𝑀𝑀 Output Behavior

    𝑃𝑃𝐿𝐿′

  • Virtual Machines?

    Are languages that use VMs compiled or interpreted? Tricky question actually Consider Java:

    At compile-time, translated from source code into byte code At run-time, a virtual machine loads the byte code and runs it

    First part sounds like a compiler, second part like an interpreter

    22

  • Java Compilation

    23

    𝑃𝑃𝐽𝐽𝐽𝐽𝐽𝐽𝐽𝐽 𝐶𝐶𝐽𝐽𝐽𝐽𝐽𝐽𝐽𝐽→𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝑀𝑀 𝑃𝑃𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵

  • Java Runtime

    Looks like interpretation! But wait, there’s more…

    24

    𝐼𝐼𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝑀𝑀

    (JVM)Output Behavior

    Input Data

    𝑃𝑃𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵

  • “Just in Time” compilation

    The JVM is “smart” enough to do “just in time” compilation Sometimes it compiles the bytecode into native machine code

    and runs that instead of interpreting bytecode!

    25

    𝐼𝐼𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝑀𝑀

    (JVM)Output Behavior

    Input Data

    𝑃𝑃𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵

  • Takeaways

    Why study programming languages? What types of programming languages are there? How do we translate from a programming language to a

    machine language? And what makes these styles of translation different?

    26

  • Readings

    Chapter 1 – Introduction For next time: Chapter 2 – Syntax

    27

    CSE 3341: Principles of Programming Languages�PL OverviewWhy are we here?Why are we here?Why are we here?Why are we here?How do we study a language?Types of Programming LanguagesDeclarative vs. ImperativeImperative example - JavaDeclarative example - PROLOGDeclarative example - SQLFunctional example - SchemeCompilationCompilation (more formally)Execution (run time)A new language?A new language?Intermediate LanguageInterpretationInterpretation (more formally)Layers of InterpretationVirtual Machines?Java CompilationJava Runtime“Just in Time” compilationTakeawaysReadings