course content structural programming and data structures
play

Course Content Structural Programming and Data Structures - PowerPoint PPT Presentation

Course Content Structural Programming and Data Structures Introduction Vectors Objects Testing/Debugging Winter 2000 Methods Arrays Tracing Programs Searching CMPUT 102: Simple Program Object State


  1. Course Content Structural Programming and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Simple Program • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion Lecture 6, 7, 8, 9: Simple Program University of Alberta  Dr. Osmar R. Zaïane, 2000 1  Dr. Osmar R. Zaïane, 2000 2 2 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Objectives of Lecture 6 Outline of Lecture 6 Programming Language Syntax Programming Language Syntax • Understand the types of errors that can be • Program errors found in a Java program. • Grammars, syntax and BNF • Get a basic idea about what a Java compiler • Tokens goes through to parse a Java program. • Identifiers • Understand the importance of syntax rules. • Literals • Translate the computation diagrams into • Semantics Java statements.  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 3 4 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  2. Program - Adventure V0 Program Errors public class Adventure { /* Version 0 • There are four kinds of errors you can make This program is an arithmetic adventure game where an adventurer when writing a program: navigates rooms that contain treasure chests that are opened by – insignificant errors correctly answering arithmetic problems. */ – compile-time errors Syntax error public static void main(String args[]) { – run-time errors /* Program statements go here. */ bug – semantic errors System.out.println(" Welcome to the Arithmetic Adventure game. "); } }  Dr. Osmar R. Zaïane, 2000 5  Dr. Osmar R. Zaïane, 2000 6 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Program - Adventure V0 Insignificant Errors without comments If we mis-spell or leave out any red public class Adventure { word this program works the same. /* Version 0 This program is an arithmetic adventure game where an adventurer navigates rooms that contain treasure chests that are opened by public class Adventure { correctly answering arithmetic problems. public static void main(String args[]) { */ System.out.println(" Welcome to the Arithmetic Adventure game. "); public static void main(String args[]) { } /* Program statements go here. */ } System.out.println(" Welcome to the Arithmetic Adventure game. "); } }  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 7 8 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  3. Compilation Errors Run-time Errors If we leave out either of these word If we mis-spell or leave out any of these public class Adventure { public class Adventure { this program compiles but won’t run. words the program won’t compile. /* Version 0 /* Version 0 This program is an arithmetic adventure game where an adventurer This program is an arithmetic adventure game where an adventurer navigates rooms that contain treasure chests that are opened by navigates rooms that contain treasure chests that are opened by correctly answering arithmetic problems. correctly answering arithmetic problems. */ */ public static void main(String args[]) { public static void main(String args[]) { /* Program statements go here. */ /* Program statements go here. */ System.out.println(" Welcome to the Arithmetic Adventure game. "); System.out.println(" Welcome to the Arithmetic Adventure game. "); } } } }  Dr. Osmar R. Zaïane, 2000 9  Dr. Osmar R. Zaïane, 2000 10 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Semantic Errors Outline of Lecture 6 public class Adventure { If we leave out any words between quotation marks, the program works differently. /* Version 0 • Program errors This program is an arithmetic adventure game where an adventurer • Grammars, syntax and BNF navigates rooms that contain treasure chests that are opened by correctly answering arithmetic problems. • Tokens */ • Identifiers public static void main(String args[]) { • Literals /* Program statements go here. */ System.out.println(" Welcome to the Arithmetic Adventure game. "); • Semantics } }  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 11 12 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  4. Need for LanguageRules Natural Language Rules • How do we know what words to use in the • Some language expressions make sense: program: “ public ”, “ class ”, “ static ”, “ void ”? – John ate the green apple. • What order should we use for the words? • How do we know if a program is expressed • Some language expressions don’t: correctly in a programming language? – Walk red Mary eat square. We need some rules for writing a program so that if we follow the rules the program will • There are rules that determine whether a be correct. natural language expression makes sense.  Dr. Osmar R. Zaïane, 2000 13  Dr. Osmar R. Zaïane, 2000 14 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Grammars and Syntax Notation • The set of rules that define the syntax of <sentence> ::= <subject> <verb> <article> <adjective> <object>. legal constructs in a natural language is called a grammar . • ::= means “is defined as”. • Here is a grammar rule for one simple Sentence is defined as a subject followed by a English sentence structure: verb followed by an article followed by an adjective followed by an object and <sentence> ::= <subject> <verb> <article> <adjective> <object>. terminated by a period. • <sentence>, <subject>, <verb>, <article>,… • Here is sentence that conforms to this are not real words but represent real words grammar rule: John ate the green apple. (tokens).  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 15 16 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  5. Backus-Naur Form (BNF) Syntax Errors • The notation: <sentence> ::= <subject> <verb> <article> <adjective> <object>. • If there are syntax errors in a natural is called Backus-Naur Form (BNF). language sentence, it may still be • Words in < > are called non-terminals since they understandable: John ate the apple green . must be further defined. • In general, computer programs are much • The symbols < > ::= are called meta-characters more sensitive to minor changes than since they are part of the BNF language, not part natural languages. of the target language. • If there are syntax errors in a program, the • All other symbols (like the dot) are called compiler reports the errors and does not terminals and must appear as shown. translate the program to machine language.  Dr. Osmar R. Zaïane, 2000 17  Dr. Osmar R. Zaïane, 2000 18 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Common Syntactic Concepts Outline of Lecture 6 • Different natural languages share common concepts like: words, punctuation, phrases • Program errors and sentences. • Grammars, syntax and BNF • Programming languages also share some common concepts. • Tokens • Three common concepts that are used to • Identifiers build larger syntactic structures are: • Literals – tokens • Semantics – identifiers – literals  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 19 20 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

Recommend


More recommend