CS152 – Programming Language Paradigms Prof. Tom Austin, Fall 2015 Syntax & Semantics, and Language Design Criteria
Lab 1 solution (in class)
Formally defining a language When we define a language, we need to keep two concepts in mind: • What is the structure ( syntax ) of a given program in the language. • What is the meaning ( semantics ) of a given program.
Defining Syntax There are 2 parts to the syntax of a language: • Lexemes/tokens – the "words" of the language • Grammar – the way that words can be ordered We'll review how a compiler (or interpreter) handles these parts.
How a compiler works Lexer/ source tokens … code Tokenizer Tokens are the "words" of the language.
How a compiler works if (x < 42) { y++; Lexer/ source tokens } else { code Tokenizer "if" "(" "x" "<" y = 42; "42" ")" "{" "y" } "++" ";" "}" "else" "{" "y" "=" "42" ";" "}" Tokens are the "words" of the language.
How a compiler works if (x < 42) { y++; Lexer/ source tokens } else { code Tokenizer "if" "(" "x" "<" y = 42; "42" ")" "{" "y" } "++" ";" "}" "else" "{" "y" "=" "42" ";" "}" There are several types of tokens: • Identifiers • Numbers • Reserved words • Special characters
How a compiler works Lexer/ source tokens Parser code Tokenizer The parser's job is to Abstract combine the tokens Syntax Tree together into an abstract syntax tree. (AST)
Parsing Example if "if" "(" "x" "<" "42" ")" "{" "y" Parser "++" ";" "}" < = = "else" "{" "y" "=" "42" ";" "}" 42 X y + y 42 Note that y++ has disappeared in the AST. The ++ operator is 1 y an example of syntactic sugar.
Formally defining language syntax Context-free grammars define the structure of a language. Backas-Naur Form (BNF) is a common notation.
Context-free grammar for math expressions (in BNF notation) <expr> -> <expr> + <term> | <expr> - <term> | <term> <term> -> <term> * <factor> | <term> / <factor> | <factor>
How a compiler works Lexer/ source tokens Parser code Tokenizer Abstract Compiler Interpreter Syntax Tree (AST) Machine code Commands
Compilers and interpreters must derive meaning from the ASTs in order to turn programs into actions. Approaches for formally defining the meaning of a language include: • Operational semantics • Denotational semantics • Axiomatic semantics
Judging a language
Louden & Lambert's Design Criteria 1. Efficiency 2. Regularity 3. Security 4. Extensibility
Efficiency Can mean different things, such as • Machine efficiency – Does the language give tips to the compiler for more efficient compilation? • Programmer efficiency – How easy is it to write in the language? – How expressive is the language? (conciseness helps) • Reliability – How difficult is code maintenance?
Efficiency Java: Python: int i = 10; i = 10 String s = "hi"; s = "hi" • Machine efficiency: Java offers tips to the compiler • Programmer efficiency: Python reduces the amount of typing required
Regularity Not always well-defined, but can be divided into • Generality: avoids special cases in favor of more general constructs • Orthogonal design: different constructs can be combined with no unexpected restrictions • Uniformity: similar things look similar, different things look different
Bad uniformity example (PHP): Same things look different Inconsistent function naming: • isset() • is_null() • strip_tags() • stripslashes()
Bad uniformity example (Pascal): Different things look the same function f : boolean; begin ... Return value is true f := true; end;
Security • Stop programmer from making errors (or handle them gracefully when they arise). Closely related to reliability. • Strong typing prevents some run-time errors. • Semantically-safe languages prevent executing code that violates the definition of the language. – Contrast array handling by Java and by C/C++
Safety (Java vs. Scheme) Java: Scheme: int x = 4; (let ([x 4] boolean b = true; [b #t]) if (b) { (if b x++; (+ 1 x) } else { (/ x "2"))) x = x / "2"; }
Extensibility With an extensible allows the programmer to add new language constructs easily. Macros in Lisp and Scheme specify the syntax of code that expands to other code.
Lab 2: More Scheme practice • Download lab2.rkt from the course website – Implement reverse function – Implement add-two-lists – Implement positive-nums-only • Using Louden & Lambert's criteria, compare Java & Scheme (or two languages of your choice)
Recommend
More recommend