Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 09 / 12 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 09 / 12 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 09 / 12 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2004 Today’s Topics Continue the discussion of Language Evaluation von Neumann architecture / Imperitive languages The major language groups Language design tradeoffs Compilation vs. Interpretation vs. Hybrids Syntax & Semantics

3 Language Evaluation Writability –Orthogonality if small set and all combinations make sense + if large set and all combinations exist but some don't make sense to ever use or would be rarely used – (could cause undeteced errors) –Abstraction (functional (methods/functions) & data (classes)) don't need to know/be reminded of/replicate the implementation details, just need to use what you wrote –Expressivity more than one way to do things (e.g. count++, for vs. while...) Reliability –(+)Type checking (at run-time vs. compile-time vs. neither) what is type-checking? when is better? –(+)Exception Handling (why does the use of exception handling increase reliability? –(-)Aliasing (why?) Michael Eckmann - Skidmore College - CS 330 - Fall 2006

4 Language Evaluation Cost (time == $) –training programmers in the language –writing --- IDEs –compiling, executing --- compiler optimization tradeoff –Implementation system (e.g. The compiler and/or interpreter available for free?) –Poor reliability –Maintenance (up to 4 times cost of developing) Can you think of others? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

5 Some Language Design Trade-offs Reliability vs. cost of execution –e.g. Run-time array range checking --- Java vs. C Readability vs. writability (expressivity) –Compactness Flexibility vs. safety –e.g. Multiple types allowed in same memory location Michael Eckmann - Skidmore College - CS 330 - Fall 2006

6 Let's think about some stuff Any other language evaluation criteria you can think of? What are the pros and cons of case sensitivity in user defined names? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

7 A few Imperitive Languages C, Pascal, Fortran and many others C++ has imperitive features although it is OO Java is imperitive-based although it is OO

8 von Neumann Architecture Michael Eckmann - Skidmore College - CS 330 - Fall 2006

9 von Neumann bottleneck transmission/piping between memory and CPU takes longer than executing instructions within the CPU. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

10 Imperitive Language Features Imperitive languages are built FOR the von Neumann architecture –Data and programs stored in same memory –Memory is separate from CPU –Instructions and data are transmitted from memory to CPU Variables model memory cells Assignment statements model transmission (from CPU to memory) Iteration is efficient (b/c instructions are in adjacent memory cells) – recursion is inefficient. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

11 Functional vs. Imperitive Languages Functional (e.g. Lisp, Scheme, et al.) –Apply functions to parameters –different use of variables –No assignment statements –No iteration (in purely functional languages) –Many programmers feel that there are extreme benefits to computing using functional languages. So, why aren't they used more? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

12 Functional vs. Imperitive Languages Because of von Neumann, that's why! What might be a drawback to using a functional language on a von Neumann machine? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

13 Imperitive Languages Von Neumann => imperitive languages But many programmers do not realize this and think that imperitive languages are the natural way to program. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

14 The major Language Groups Imperitive –And visual langs (drag/drop code) Functional Logic –Rule based, no order of execution Object-Oriented Michael Eckmann - Skidmore College - CS 330 - Fall 2006

15 Compilation, Interpretation, Hybrids What is a compiler? What is an interpreter? What is a hybrid of these? –Java, Perl –Compiler generates intermediate code that is then interpreted –Any advantages/disadvantages? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

16 Compilation Process Michael Eckmann - Skidmore College - CS 330 - Fall 2006

17 Compilation Process Lexical analyzer – groups the code into variable names, reserved words, punctuation, etc. Syntax analyzer – constructs parse trees to determine if the lexical units in order are syntactically correct Both create items in the symbol table to refer to these units Intermediate code generator (and semantic analyzer) generates assembly-like code from the symbol table and parse trees. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

18 Compilation Process Optimization may occur at this stage What might the compiler try to optimize? Code generator takes the intermediate code and the symbol table to create the machine code for the particular architecture on which the code is compiled. Before execution, the machine code must be linked to any necessary programmed libraries and/or systems programs. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

19 Execution Process Fetch-execute cycle on a von-Neumann machine –Uses a program counter to know which instruction is next to be fetched. –While (true) {Fetch, increment, decode, execute} Michael Eckmann - Skidmore College - CS 330 - Fall 2006

20 Now Chapter 3... Describing a language –How to give it a clear and precise definition so that implementers (compiler writers) will get it right –How to describe it to users (programmers) of the language Michael Eckmann - Skidmore College - CS 330 - Fall 2006

21 Syntax and Semantics Describing a language involves both it's syntax and semantics Syntax is the form, semantics is the meaning –e.g. English language example: Time flies like an arrow. –Syntactically correct but has 3 different meanings (semantics) Easier to describe syntax formally than it is to desribe semantics formally Michael Eckmann - Skidmore College - CS 330 - Fall 2006

22 Syntax A language is a set of strings (or sentences or statements) of characters from an alphabet. Lexemes vs. tokens –tokens (more general) are categories of lexemes (more specific) –e.g. Some tokens might be: identifier, int_literal, plus_op –e.g. Some lexemes might be: idx, 42, + Michael Eckmann - Skidmore College - CS 330 - Fall 2006

23 Syntax Lexemes idx = 42 + Count ; Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Tokens identifier equal_sign int_literal plus_op identifier semicolon idx = 42 + count;

24 Syntax Recognizers and generators are used to define languages. Generators generate valid programs in a language. Recognizers determine whether or not a program is in the language (valid syntactically.) Generators are studied in Chapter 3 (stuff coming up next in this lecture) and recognizers (parsers) in Chapter 4. How many valid programs are there for some particular language, say Java? Michael Eckmann - Skidmore College - CS 330 - Fall 2006

25 Syntax Context Free Grammars (CFGs) developed by Noam Chomsky are essentially equal to Backus-Naur Form (BNF) by Backus and Naur. They are used to describe syntax. These are termed metalanguages (languages used to describe languages.) Michael Eckmann - Skidmore College - CS 330 - Fall 2006

26 Syntax A Context Free Grammar is a four-tuple (T, N, S, P) where –T is the set of terminal symbols –N is the set of non-terminal symbols –S is the start symbol (which is one of the non- terminals) –P is the set of productions of the form: A -> X 1... X M where –A element of N –X i element of N U T, 1 =0 Michael Eckmann - Skidmore College - CS 330 - Fall 2006

27 Syntax How are CFGs used to describe the syntax of a programming language? The nonterminals are abstractions The terminals are tokens and lexemes The productions are used to describe programs, individual statements, expressions etc. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

28 Syntax Example production:  while ( ) Everything to the left of the arrow is considered the left-hand side, LHS, and to the right the RHS. The only thing that can appear on the LHS is one nonterminal. Multiple RHS's for a LHS are seperated by the | or symbol, e.g.  ; | { } Michael Eckmann - Skidmore College - CS 330 - Fall 2006

29 Syntax Recursion is allowed in productions, e.g.  ident | ident, Michael Eckmann - Skidmore College - CS 330 - Fall 2006

30 Syntax An example grammar:   | ;  =  a | b | c | d  + | -  | const Michael Eckmann - Skidmore College - CS 330 - Fall 2006

31 Syntax Derivations are repeated applications of production rules. An example derivation: => => => = => a = => a = + => a = b + => a = b + const Michael Eckmann - Skidmore College - CS 330 - Fall 2006

32 Syntax Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded in each step of the derivation. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

33 Syntax / Parse Trees Michael Eckmann - Skidmore College - CS 330 - Fall 2006 A hierarchical representation of a derivation (parse trees also hold some semantic information) const a = b +

34 An Ambiguous Expression Grammar  | const  / | - const --//

35 This one is now unambiguous Ambiguity is bad for compilers, so the language description should be unambiguous.  - |  / const | const Compiler examines parse tree to determine the code to generate. Two parse trees for the same syntax causes the meaning (semantics) of the code to not be unique. Michael Eckmann - Skidmore College - CS 330 - Fall 2006

36 Ambiguous? Look at the if statement rules below  if then | if then else  |... Do you think this is ambiguous? That is, can more than one parse tree be generated from the same code? if (a==b) then if (c==d) then print_something() else print_something_else() Michael Eckmann - Skidmore College - CS 330 - Fall 2006

37 Ambiguous? if (a==b) then if (c==d) then print_something() else print_something_else() if (a==b) then if (c==d) then print_something() else print_something_else() Michael Eckmann - Skidmore College - CS 330 - Fall 2006 if (a==b) then if (c==d) then print_something() else print_something_else()

38 Ambiguous? To make it unambiguous take a look at page 138 in our text. Michael Eckmann - Skidmore College - CS 330 - Fall 2006


Download ppt "CS 330 Programming Languages 09 / 12 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google