Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 341 -- S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.

Similar presentations


Presentation on theme: "CSE 341 -- S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference."— Presentation transcript:

1 CSE 341 -- S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference

2 CSE 341 -- S. Tanimoto Syntax and Types 2 General Issues Representation (of data, of computation) Form (Syntax) Meaning (Semantics) Paradigm (General way of thinking) Naming (names, name spaces, bindings, locality) Functionality (numeric, data manip, I/O, communication, synchronization, security) Correctness (types, exception handling, error checking, bug avoidance)

3 CSE 341 -- S. Tanimoto Syntax and Types 3 Syntax Syntax: The grammatical form of programs. vs Semantics: The meaning of the program Syntax (of textual languages) is typically specified by production rules for a context-free grammar using Backus-Naur Form (BNF) or Extended BNF (EBNF) In visual languages, syntax is described by a set of restrictions on how diagrams may be constructed. (e.g., connection constraints)

4 CSE 341 -- S. Tanimoto Syntax and Types 4 Syntactic Components Identifiers and reserved words Numeric constants Parentheses, braces and brackets Expressions Statements

5 CSE 341 -- S. Tanimoto Syntax and Types 5 BNF (Backus-Naur Form) (2.0 * PI) / n ::= + | - | ::= * | / | ::= number | name | ( )

6 CSE 341 -- S. Tanimoto Syntax and Types 6 Extended BNF Optional constructs written as [ x ] Zero or more of x written as { x } Choice (“or”) written using | Grouping with parentheses ( x | y ) as in { (x | y ) z } ::= { (+ | -) } ::= { (* | /) } ::= ’(’ ’)’ | name | number

7 CSE 341 -- S. Tanimoto Syntax and Types 7 Derivation E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= number | name | ( E ) E E + T T + T T / F + T F / F + T F / F + F 25 / F + F 25 / 100 + F 25 / 100 + total

8 CSE 341 -- S. Tanimoto Syntax and Types 8 Representation of Data Constants, Variables Types, classes Compounds: arrays, structures. Non-numeric objects: strings, images, audio. Values vs references Machine dependencies: word size, addressing resolution. In C, characters and booleans are actually integers.

9 CSE 341 -- S. Tanimoto Syntax and Types 9 Representation of Process Arithmetic and logical expressions Conditional expressions Loops Recursive and nonrecursive functions Multiple threads of control, forking, joining, synchronizing Single-threaded Parallel processing (in Single- instruction stream/multiple data stream processors) Throwing and catching of exceptions Declaration of constraints and rules

10 CSE 341 -- S. Tanimoto Syntax and Types 10 Paradigm General style of thinking that underlies a programming language Webster’s New World Dictionary: “a pattern, example, or model”. ImperativeRule-based FunctionalLogic Object-orientedVisual data-flow

11 CSE 341 -- S. Tanimoto Syntax and Types 11 The Imperative Paradigm An imperative program is a sequence of commands Read a value from a file. Evaluate an arithmetic expression. Assign a value to a variable. Test a condition and branch if it is true. Iterate a loop body until a condition is false. Print a value onto the screen.

12 CSE 341 -- S. Tanimoto Syntax and Types 12 The Functional Paradigm An functional program is a collection of function definitions and function applications. Define SQR(x): { Apply the * function to x and x} Apply SQR to 7;

13 CSE 341 -- S. Tanimoto Syntax and Types 13 The Object-Oriented Paradigm An object-oriented program is a collection of object class definitions, in which both data members and methods are specified. Class Student extends Person { int student_number; int get_student_number() { return student_number; } int set_student_number (int num) { student_number = num; }

14 CSE 341 -- S. Tanimoto Syntax and Types 14 The Rule-Based Paradigm A rule-based program is a collection of if-then rules. if name = "" then input name; if name starts with "A" then print "Early in the alphabet";

15 CSE 341 -- S. Tanimoto Syntax and Types 15 The Logic-Programming Paradigm A logic program is a collection of logical propositions and questions. If x is a bird or an airplane, then x has wings. Tweety is a bird. Does Tweety have wings?

16 CSE 341 -- S. Tanimoto Syntax and Types 16 The Visual Data-Flow Paradigm A visual data-flow program is a diagram in which boxes represent operations and arrows indicate the flow of data from outputs of operations to inputs of other operations. input x * * * 5 3 + + 8 3x 2 + 5x + 8

17 CSE 341 -- S. Tanimoto Syntax and Types 17 Types Category or class for a value (or object) that permits its bits to be interpreted. Central Processing Unit instruction sets recognize certain types, such as integers and floats of different sizes. A programming language is not limited to the types that are directly supported by the CPU.

18 CSE 341 -- S. Tanimoto Syntax and Types 18 Strong vs Weak Typing Strong typing: (static and usually monomorphic) Every variable must have a type. A variable may receive only values of its type. Type-related bugs can be reduced. Type identification tags are not needed a run time. Weak typing: (dynamic and usually polymorphic) Variables need not be declared with particular types. The type of value held by a variable can change dynamically. Values must be tagged during execution with their types.

19 CSE 341 -- S. Tanimoto Syntax and Types 19 Coercion and Contagion Coercion: Any automatic type conversion. A value of type A is being assigned to a variable of type B. The value is coerced into one of type B. double x = 3 * 5; Contagion: Values of lower precision “catch” the precision of their higher precision co-arguments. int y = 10 * (3.1415 / 10); /* result is 3, not 0. */ In Common Lisp, when a rational meets a float, the rule of floating- point contagion rules.

20 CSE 341 -- S. Tanimoto Syntax and Types 20 Type Inference With weak typing, type inference is needed. (But ML, which is strongly typed, also uses a kind of type inference.) The resulting type can be determined from the types of the arguments, and the nature of the operations. Contagion provides one method for type inference: With strong typing, means for determining type equivalence are needed. int x[10]; int y[10]; Here x and y have structurally equivalent types. C and C++ recognize structurally equivalent types as equivalent. In Modula, most structurally equivalent types are not automatically considered equivalent, but they can be declared equivalent.


Download ppt "CSE 341 -- S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference."

Similar presentations


Ads by Google