100 likes | 116 Views
CS 476 – Programming Language Design. William Mansky. Structure of a Language. Syntax Abstract – what are the pieces of a program? Concrete – what do programs look like? Semantics Static – what are acceptable programs? Dynamic – what do programs do when we run them? Pragmatics
E N D
CS 476 – Programming Language Design William Mansky
Structure of a Language • Syntax • Abstract – what are the pieces of a program? • Concrete – what do programs look like? • Semantics • Static – what are acceptable programs? • Dynamic – what do programs do when we run them? • Pragmatics • Implementation – how can we actually make the semantics happen? • IDE, tool support, etc.
Context-Free Grammars • Terminals: a, b, c, +, *, (, … • Nonterminals: A, B, C, … • Productions: A -> aAc | B | ef
BNF Grammars • A convenient notation for context-free grammars A ::= a | aA B ::= b | bB S ::= A | B | (S) | SS
BNF Grammars • A convenient notation for context-free grammars …that looks a lot like a datatype! A ::= a | aA B ::= b | bB S ::= A | B | (S) | SS type S = cA of A | cB of B | cParen of S | c2 of S * S
BNF Grammars for Syntax E ::= <#> | E + E | E – E | E * E | – E type_E = cInt of int | cAdd of type_E * type_E | … C ::= <var> = E | if E then C else C | C; C | while(E) C type_C = cIf of type_E * type_C * type_C | …
Notes on tuples f (x, y) = x + y (1, 2) : int * int int * int * int = int * (int * int) let fst x = match x with (a, b) -> a let snd x = match x with (a, b) -> b let third x = match (a, b, c, d) -> c
Abstract Syntax Trees E ::= <#> | E + E | E – E | E * E | – E E + + E E 3 * E 9 E 5 3 * Add(Int 3, Mul (Int 5, Int 9)) 9 5