Some Basic Programming Language Features CMPT 383 Syntax
Some Basic Programming Language Features CMPT 383
Syntax • What the language “looks like” when you read the source code • These all do essentially the same thing, but their syntax differs • In practice, syntax is typically precisely specified using grammars Python for i in range(seq): val[i] += 1 C++ for(int i = 0; i < seq. size(); i++) { val[i]++; } Racket (map (lambda (n) (+ n 1)) seq)
Semantics • What the language “means” • Logic and mathematics can be used to give expressions clear meaning • However, this is quite hard to do as compared to using grammars for syntax, and so in practice we usually use carefully written English to specify semantics C++ Modifies seq inplace (i. e. no copy is made). for(int i = 0; i < seq. size(); i++) { val[i]++; } Racket Makes a copy of seq. (map (lambda (n) (+ n 1)) seq)
Interpretation vs. Compiling • A compiled language • An interpreted processes converts the language process each entire program into source code statement machine language one at a time, executing it immediately after its • Then, after the translated compilation is successful, you run the • Interpreters intermix resulting machine running the program, language and converting it into Many languages are a hybrid of machine language compiled and interpreted. For example, Python compiles to bytecode and then interprets that bytecode.
Interpretation vs. Compiling • • Interpreters Pro: easy to test small bits of code Pro: can have access to the source code at run time Pro: compared to a compiler, often easier to implement Con: usually slower than a compiler since conversion to machine language happens at run time • • Compilers Pro: usually faster than an interpreter since conversion to machine language done before running Pro: can catch some errors before running the program Con: more work to run small pieces of code Con: the compiling phase can be get complex (and slow)
Static vs. Dynamic • Static generally refers to things done to a program before it runs • E. g. anything a compiler does to a program is considered static • A defined function and the types of its parameters can be determined at compile-time • In most modern languages, the scope of a variable can be determined statically (i. e. at compile-time) • Dynamic generally refers to things done to a program while it is running • The value of most variables is not known until the program runs, e. g. x = input('Age? ‘) print(x)
Static Typing vs Dynamic Typing • Static typing refers to type • Dynamic typing refers to type information in a program that can be determined by the can be determined at run-time compiler • Python and Racket are • C++, Haskell, and Go are (mostly) dynamically typed statically typed • Type errors found at run-time • Their compilers can catch many kinds of type errors before running
- Slides: 7