course content structural programming and data structures
play

Course Content Structural Programming and Data Structures - PDF document

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 Structural Programming and Data Structures University of Alberta 1  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 2 2 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 Structural Programming and Data Structures University of Alberta 3  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 4 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  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 5 Structural Programming and Data Structures University of Alberta 6 1

  2. 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 Structural Programming and Data Structures University of Alberta 7  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 8 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 Structural Programming and Data Structures University of Alberta 9  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 10 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 Structural Programming and Data Structures University of Alberta 11 Structural Programming and Data Structures University of Alberta 12 2

  3. 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 Structural Programming and Data Structures University of Alberta 13  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 14 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 Structural Programming and Data Structures University of Alberta 15  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 16 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  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 17 Structural Programming and Data Structures University of Alberta 18 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend