1 / 19

Introduction and Syntax

Introduction and Syntax. Course objectives. Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture (what is happening under the hood). Introduce a few specific programming languages: C++, Java, Ada, Scheme, ML. Desiderata.

yardan
Télécharger la présentation

Introduction and Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction and Syntax

  2. Course objectives • Discuss features of programming languages. • Discuss how the features are implemented in a simple computer architecture (what is happening under the hood). • Introduce a few specific programming languages: C++, Java, Ada, Scheme, ML.

  3. Desiderata Ease of human use: Writing, debugging, reading, maintaining. Automated/formal analysis Efficient implementation Portability: Machine independence

  4. Categories of languages • Imperative (Assembly, FORTRAN, Algol, C, Ada …). Statement modifying memory locations execute in sequence. • Object-oriented (Smalltalk, C++, Java): Computation via interaction of objects. • Functional: (LISP, Scheme, ML). Computation as definition and application of functions.

  5. Categories of languages (cntd) • Logic-based (Prolog): Computation as inference from statements and rules. • Special purpose (Postscript, Javascript, database languages): PL’s geared toward a specific application.

  6. Components of a PL • Syntax: What constitutes a well-formed program? • Semantics: What computational activity constitutes a proper execution of a given program on a given input? • Implementation.

  7. Compilers and interpreters An interpreter preserves the text of the program, and executes the program by constant referral to the text. A compiler translates the program from the source language into a target language. This may be either machine language or (more commonly) a lower level language (e.g. assembly or C).

  8. Compilers and interpreters (cntd) The distinction between compiled and interpreted languages is not clear cut: • Most interpreters begin by doing some degree of preprocessing (e.g. comment removal). • The target language may then be interpreted (e.g. Java compiles into byte language, interpreted on JVM). • Machine language is interpreted in microcode.

  9. Advantages of interpreters • Some language features are difficult or impossible to compile (e.g. writing and executing code on the fly). • Portability and machine independence (This is the main reason that Java byte code and Javascript are interpreted.) • Interpreters are easier to write than compilers.

  10. Advantages of interpreters (cntd) Interpreters also make it easier to get • Interactive environments • Source code is available for debugging. • Light-weight coding (e.g. no declarations) though it is not impossible to get these feature in compiled languages.

  11. Advantage of compilers • Speed of compiled code. One or two orders of magnitude. • Can distribute object code without publishing source code.

  12. Time of features A compile-time feature of a program can be determined before execution begins (and is therefore independent of the input data). A run-time feature of a program can only be detected during execution and is generally dependent on the input.

  13. Syntax The syntax of a program is its formal structure. The line separating syntactic from non-syntactic features is somewhat arbitrary. All syntactic features are detectable at compile time, but not vice versa. E.g. it is detectable at run time that “x=1/0” is an error, but this is not (generally) a syntactic error.

  14. Tokens In almost all programming languages, a program is a sequence of tokens. Types of tokens: • Special symbols: “;”, “+”, “++”, “{“ … • Reserved words: “if”, “then”, “function” … • Numbers: “5”, “4123”, “1.2E+08”, “0x4AC” • Identifiers: “x”, “i”, “append”, “employee” …

  15. Tokens (cntd) Language specific rules for: • What forms are in each of these categories. • How tokens are delimited (usually by white space or special symbols, but not in FORTRAN) • When two tokens are the same (e.g. case sensitivity)

  16. Tokens (cntd) Lexer or tokenizer divides the source code into tokens and categorizes the tokens. Generally, this is done by a regular language = finite automaton.

  17. Syntax tree int increment (int i) { return (1+i); }

  18. Backus-Naur Form (BNF) FunDefn ::= FunDecl Block Term ::= num | var | Term arithOp Term | funCall ArgList ::= () | (VarDecl [, VarDecl]*)

  19. Context-Free Language A BNF definition defines a context-free language. The syntax of programming languages is “almost” context free. A few syntactic constraints are not (e.g. identifiers must be declared before they are used).

More Related