Imperative Paradigm Syntax Rupesh Nasre. CS3100 Paradigms of Programming IIT Madras Jul 2014 Symbols int, cs12b063, function Expressions 10 * x, abs(x y), ++x Constructs if-else, while {, functionbeginend Grammar Role of Programming Languages Expressions 1: M[0] := 0 2: read(m[1]) 3: if M[1] >= 0 then goto 5 4: goto 7 5: M[3] := M[0] M[1] 6: if M[3] >= 0 then goto 16 7: writeln(m[1]) 8: read(m[2]) 9: M[3] := M[2] M[1] 10: if M[3] >= 0 then goto 12 11: goto 14 12: M[3] := M[1] M[2] 13: if M[3] >= 0 then goto 8 14: M[1] := M[2] + M[0] 15: goto 3 16: halt 1: read(x); 2: if x == 0 then goto 8; 3: writeln(x); 4: read(next); 5: if next == x then goto 4; 6: x = next; 7: goto 2; 8: ; 1: read(x); 2: while x!= 0 do begin 3: writeln(x); 4: repeat 5: read(next); 6: until next!= x; 7: x = next; 8: end; Assembly With goto With structured statements Input: 1 1 2 2 2 3 1 4 4 0 Output: 1 2 3 1 4 Infix 2 + 3 * 4 + 5 e.g., +, *, -, ==, <, p.x Postfix 2 3 4 * + 5 + e.g., a[i], f(), x++ Prefix + + 2 * 3 4 5 e.g., int i; *p, ++x Role of Programming Languages Expressions 1: read(x); 2: while x!= 0 do begin 3: writeln(x); 4: repeat 5: read(next); 6: until next!= x; 7: x = next; 8: end; 1: readentry(x); 2: while not endmarker(x) begin 3: writeentry(x); 4: repeat 5: readentry(next); 6: until not equalentry(x, next); 7: copyentry(x, next); 8: end; 1: readentry(x); 2: while not endmarker(x) { 3: writeentry(x); 4: next = removedup(x); 5: copyentry(x, next); 6: Infix 2 + 3 * 4 + 5 Postfix 2 3 4 * + 5 + Prefix + + 2 * 3 4 5 Infix 2 + -3 ** y + x++ Postfix t x = 2 3 u- y ** + t + x ++ Prefix Homework With structured statements With procedures Further abstraction
Precedence Ambiguous / Unambiguous Grammars x # y @ z How does a compiler know whether to execute # first or @ first? Think about x+y*z vs. x/y-z A similar situation arises in if-if-else. Humans and compilers may see different parse trees. #define MULT(x) x*x int main() { What if if both both the the operators are are the the same? printf( %d, MULT(3 + 1)); ## y @ ## @ yy Grammar for simple arithmetic expressions E E + E E * E E E E / E (E) number name E E + E E E T T T * T T / T F F (E) number name E E + T E T T T T * F T / F F F (E) number name Precedence not encoded Associativity not encoded Unambiguous grammar Check a b c Same Precedence Peculiar Constructs If-else nesting versus elseif or elsif. # x @ yy @ # z yy x + y + z x - y - z Order of evaluation doesn't matter. Order of evaluation matters. yy y Switch statement. Case ordering. Conversion of repeat-until into if + while. for i := 1 to 10 versus for (i = 1; i <= 10; ++i) for i := 10 downto 1: Why downto? Pascal: If expr then if expr then S else S Modula-2: if expr then if expr then S else S end end Modula-2: if expr then if expr then S end else S end Associativity C++ File I/O Associativity decides the order in which multiple instances of an operation are executed. Binary minus is left associative, hence x-y-z is equal to (x-y)-z. Homework: Write a C program to find out that assignment operator = is right associative. yy y #include <fstream> void SLL::read(std::string file) { std::ifstream in; std::string str; in.open(file.c_str()); in >> str; while (!in.eof()) { insert(str); in >> str; in.close(); New construct #include <fstream> void SLL::read(std::string file) { std::ifstream in; std::string str; in.open(file.c_str()); do { in >> str; while (!in.eof()); insert(str); in.close(); The do {while construct enables avoidance of code duplication. Homework: What are the constructs in your favorite language that allow you to avoid code duplication directly?
Duals break and continue while and until << and >> in C++ {x > 0 x++; {x > 1 {x > 0 x++; {?? {x > 0 {x > 0 x++; {x > 1 {x > 0 {x > 5 {x == 4 {x > 0 x == 4
{x > 0 {x > 5 {x == 4 {x > 0 {x > 0 {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x > 0 {x > 5 {x == 4 else { {x > 0 {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x == 4 x є (-10..-5] {x > 0 {x > 5 {x == 4 else { {x <= 5 {x > 0 if () { {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x == 4 x є (-10..-5] Thought exercise: Given a set of pre and post-conditions, can we automatically generate a program?
{x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 We want to check if x becomes negative at the end of the loop. {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x >= 0 && y >= 0 {x >= y {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0
{x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 Post-condition indicates that x may be negative at the end of the loop. Type of types Primitive Derived User-defined Type conversion Coersion Data Types Static versus dynamic typing Type hierarchy Static versus dynamic allocation Recursive Data Types {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 Post-condition indicates that x may be negative at the end of the loop; but the program doesn't say so. Procedures Abstraction: What versus How Procedures versus Functions Connection with flow-charts Declaration, Definition, Activation Parameter passing Call by value, reference, name, value-result {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 && x >= 0 x cannot be negative at the end of the loop. Multiple activations Recursion Imagining with recursion: list, tree, sort, search, factorial (more in functional paradigm) Drawing Hands, M C Escher
Static versus Dynamic Block structure Name resolution Scope