140 likes | 257 Views
Programming Language Descriptions. What drives PL Development?. Computers are “in charge” of extremely important issues Execute a program literally. Exercise no “judgment”. Program development “works” : not too hard “works correctly”: beyond the state of the art
E N D
PL-Descriptions What drives PL Development? • Computers are “in charge” of extremely important issues • Execute a program literally. • Exercise no “judgment”. • Program development • “works” : not too hard • “works correctly”: beyond the state of the art • Errors are due to inadequate command over the • programming domain • programming language • A PL design should • Make programs efficient • Make programmers more productive • Help catch typical errors • High level operations • PL easy to understand
PL-Descriptions Division of PL Descriptions • Syntax • Context Free Grammars • Context Sensitive Details • Semantics • PL designer typically uses prose. • Others would have developed • Operational Semantics • Axiomatic Semantics • Denotational • Pragmatics • Various practical details • Prose
Description of Syntax: BNF Backus-Naur-Form Context-Free only LHS ::= seq of Terminals/Non-Terminals ::= separates LHS from RHS Repetition/Kleene Star { … }* Many variations of BNF PL-Descriptions
PL-Descriptions Description of Semantics • What does a program “mean”? • Mostly via carefully written prose. • Semantics of a language is (ought to be) independent of its machine implementation. • Subtleties abound
PL-Descriptions Side Effects • If function f has side-effect on variable x then it is possible to have • x + f(x) =/= f(x) + x • f(x) + f(x) =/= 2 * f(x) • f(x) / f(x) =/= 1, even assuming f(x) != 0 • Loss of Referential Transparency
PL-Descriptions Subtle variations in semantics • Are the following equivalent? • (Ignore PL specific syntactic details.) • while B do S • S stands for a statement sequence • 25: if B then begin S; goto 25 end • Equivalent in Pascal, Ada, etc., but not in C/C++/Java.
PL-Descriptions Scope of break statement #include <stdio.h> main() { inti, j, k1 = 2, k2 = 2; do { i = 2; while ( i > 0 ) { printf("\t i = %d \n", i--); } } while (k1--); printf("\n"); do { j = 2; TAG: if ( j > 0 ) { printf("\t j = %d \n", j--); goto TAG; } } while (k2--); }
PL-Descriptions Type Equivalence • type T = array [1..10] of integer; • var A,B : array [1..10] of integer; • C: array [1..10] of integer; • D, E: T; • Structural Equivalence: {A,B,C,D,E} • Name Chain Equivalence: {A,B},{C},{D,E} • Name Equivalence: {A},{B},{C},{D,E}
PL-Descriptions Interpreter vs Compiler • Interpreter evaluates the representation of a program. • Compiler transforms a program representation in one language into an “equivalent” program in another language.
PL-Descriptions Why is formal semantics not widely used? • Real PLs are too complex. • Not (yet) cost effective for everyday (and everybody’s) use. • Semantics is general. In particular, it must consider all possible situations (including the boundary cases). • But steady progress is being made ... cs784 (Prasad) L2SpecIntro 11
PL-Descriptions Approaches to Formal Semantics • Operational : How a program executes? Specifies abstract interpreter to carry-out the meaning of the programs. • Denotational : What a program computes? Maps a program to a mathematical function from its inputs to its outputs. • Axiomatic : For reasoning about programs Specifies properties of language constructs through pre-post conditions. Abstraction level: OS < DS < AS
PL-Descriptions Who needs semantics? • Those who write (meta-)programs that must work for all programs. • Designers of • program transformation tools. • compilers and interpreters. • program analyzers. • software engineering tools. • critical software. cs784 (Prasad) L2SpecIntro 13
PL-Descriptions CS784 Agenda Language Syntax(BNF) Semantics Pragmatics Data Control Abstract Data Types Denotational AxiomaticOperational (interpreter-based) Attribute Grammar Framework