Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4.

Similar presentations


Presentation on theme: "The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4."— Presentation transcript:

1 The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4

2 Compiling and Debugging Executable code will not be created until you correct all of the syntax errors in your source code Executable code will not be created until you correct all of the syntax errors in your source code

3 Syntax & Logic Errors A syntax error is simply the violation of the rules of a language; misuse of structure and form in programming or a violation of the compiler’s rules. These errors are detected by the compiler A syntax error is simply the violation of the rules of a language; misuse of structure and form in programming or a violation of the compiler’s rules. These errors are detected by the compiler –Also know as 'fatal compilation errors' A logic error is a mistake that complies with the rules of the compiler that causes the program to generate incorrect output A logic error is a mistake that complies with the rules of the compiler that causes the program to generate incorrect output

4 Compiling or Re-compiling 1.Open the file by going the directory you stored it in and double clicking on it 2.Scan the source code for obvious errors before attempting to compile 3.Attempt to compile using the COMPILE button or choosing the first option on the BUILD menu 4.Answer Yes to use the default project workspace –The lower window will be active and the compile will occur 5.Note the number of errors and warnings –These need to be dealt with before executable code is created

5 Debugging Debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device. Debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device.bugs To debug a program or hardware device is to start with a problem, isolate the source of the problem, and then fix it. To debug a program or hardware device is to start with a problem, isolate the source of the problem, and then fix it.

6 Debugging Objective 1. Find the line(s) containing the syntax error(s) using the compiler's chosen line and error messages as a starting point

7 Debugging is an Art Compilers often miss reporting an actual error and report on subsequent lines which are effected by error but may be completely correct Compilers often miss reporting an actual error and report on subsequent lines which are effected by error but may be completely correct After encountering a real syntax error, compilers often generate many incorrect syntax error messages After encountering a real syntax error, compilers often generate many incorrect syntax error messages Different compilers produce different errors and warnings for the same errors in the same program Different compilers produce different errors and warnings for the same errors in the same program

8 Debugging Steps 1. Proofread before compiling 2. Compile 3. Correct all the obvious errors »Start at the beginning of the list of errors and warnings »A single syntax error may cause the compiler to believe numerous other syntax errors are occurring »Look at the error lines and if you see the error, fix it. Otherwise, leave it for later. It may vanish when you fix something else »Don’t worry if more errors appear. Some errors mask other errors 4. Recompile when you have fixed what you recognize

9 Debugging Steps 5. Repeat 3 & 4 until no further errors are obvious 6. Attempt to solve the remaining errors in a top-down fashion 7. Solve whatever errors you can without spending long periods of time on any given error 8. Recompile whenever you feel you don’t see any further solutions

10 Debugging Aids 1. In the Visual C++ (and other GUI-based compilers) double-clicking on an error message move the cursor to the line where the compiler detected the error –This may not be the actual line where the error occurred – don’t trust the compiler on lines 2. Work from the beginning of the program, because in most compilers, the errors are detected from the beginning to end, sequentially 3. Some errors are so severe, they stop the compiler from continuing so more errors may appear after you successfully fix one or more

11 A Debugging Mindset Assume your syntax is wrong. Look it up! Assume your syntax is wrong. Look it up! »Add working comments as you change things If you are 100% sure a line is correct, then search for a syntax error in the lines ABOVE that line If you are 100% sure a line is correct, then search for a syntax error in the lines ABOVE that line –Start with the immediately previous line and work backward Never make a change you can’t explain Never make a change you can’t explain

12 A Working Debugging Comment cuot << "This is a line of code"<< endl; /*************Debug************* Error is undeclared identifier 1. Checked syntax for endl 2. Check syntax for screen output -- Cuot is misspelled */

13 Debugging Checklist 1. Visually verify the spelling and case of keywords and identifiers -- Remember, in the editor, keywords are blue, literals are black and comments are green -- Look for problems with l and 1 and o and 0 2. Verify syntax with a reference book, not just visually -- Don’t trust your eyes; you see what is supposed to be there 3. Try to find an example in the reference book that does something similar and compare the code 4. Verify that the necessary delimiters used for that line are there -- Check the lines above and below as well

14 Debugging Checklist 5. Without looking at your source code or notes, rewrite the instruction on a piece of paper and then compare it to your actual code; don’t cheat 6. Verify that the line is really the source of the error by commenting the line using // a)Don’t worry about other errors that result from this b)If the error disappears, it probably results from the line you are working on c)If it moves to the next line it is probably caused earlier in the code d)Remember that the compiler cannot be trusted to pinpoint lines

15 Debugging Tutorial – Some Practice Press the compiler button to run first scan of program Press the compiler button to run first scan of program Verify the number of errors that appear in the lower window Verify the number of errors that appear in the lower window Double click on an error to jump to the referenced line of the program Double click on an error to jump to the referenced line of the program Read and examine the error Read and examine the error Correct the error Correct the error Save the file Save the file Recompile Recompile

16 Some Recognizable Errors If a delimiter is missing is indicated before the first word, the error belongs on the previous line If a delimiter is missing is indicated before the first word, the error belongs on the previous line If you use an open parenthesis, there must be a corresponding closing parenthesis If you use an open parenthesis, there must be a corresponding closing parenthesis An undeclared identifier error generally means a misspelling has occurred An undeclared identifier error generally means a misspelling has occurred –This includes case errors. Keywords have no caps –Remember, in C++, the case of everything matters It is not possible to illustrate every conceivable error, nor is it possible to describe all of the circumstances that generates errors It is not possible to illustrate every conceivable error, nor is it possible to describe all of the circumstances that generates errors

17 C2065: Undeclared Identifier 1.Several things may produce this error »Misspelling a keyword »Misspelling a programmer defined name (identifier) »Misuse of case in a keyword or identifier »Failure to declare an identifier

18 Delimiter Errors C2146: syntax error : missing '; ' C2146: syntax error : missing '; ' –The programmer must tell the compiler where an instruction ends C2059: syntax error : ';' C2059: syntax error : ';' –Involves the use of braces, brackets and parenthesis in C++ C1004 : unexpected end of file found C1004 : unexpected end of file found –Often caused by missing } or an extra { somewhere »Use // to sequentially number opens and closes –Missing closes can cause some horrible messes

19 Common Causes for Errors Remember that in C++, lowercase letters are different from uppercase letters Remember that in C++, lowercase letters are different from uppercase letters Beware of unmatched parenthesis, square brackets, and curly brackets Beware of unmatched parenthesis, square brackets, and curly brackets It is easy to forge the last double quote in a string It is easy to forge the last double quote in a string The left side of an assignment must be a variable The left side of an assignment must be a variable

20 All I Have is Warnings – Am I Done? Even though an executable has been generated, you may not be done with syntax errors Even though an executable has been generated, you may not be done with syntax errors Compilers generate syntax warning messages which are not fatal errors but represent special error checking functions for certain common programming errors Compilers generate syntax warning messages which are not fatal errors but represent special error checking functions for certain common programming errors

21 Warnings Actions that may represent problems but do not cause the compiler to flag an error Actions that may represent problems but do not cause the compiler to flag an error –Don’t ignore warnings –Most common warning is a ‘typecasting’ warning »Indicates that the conversion of one type of a number was moved to a number (variable) of a different type without inclusion of a typecasting operation E.G. – Moving a float to an integer variable E.G. – Moving a float to an integer variable

22 Common Causes for Warnings An equal sign used in an expression is actually an assignment operator, not the relational operator testing for equality An equal sign used in an expression is actually an assignment operator, not the relational operator testing for equality Loops where one condition could never logically be executed Loops where one condition could never logically be executed Testing a variable that has not received a value yet Testing a variable that has not received a value yet

23 Disk Space Issues If the floppy is full or becomes full during the compilation process, the compile will fail with an error message such as: If the floppy is full or becomes full during the compilation process, the compile will fail with an error message such as: –fatal error C1033: cannot open program database »A very cryptic message like this can result If you are not able to view all of the intermediate files created in a compile, suspect a space error If you are not able to view all of the intermediate files created in a compile, suspect a space error

24 Linker Errors Not all syntax errors are detectable by the compiler Not all syntax errors are detectable by the compiler –These errors do not become apparent until files are put together to create an executable –These errors are not linked to a specific line of code »Look for the name of the variable and see what lines of code it occurs on using EDIT and FIND LNIK2001: unresolved external LNIK2001: unresolved external LNK1120: unresolved externals LNK1120: unresolved externals

25 Executing the Program Use REBUILD ALL on the BUILD menu Use REBUILD ALL on the BUILD menu –An.EXE file will be created The EXECUTE button is a red exclamation mark The EXECUTE button is a red exclamation mark –You can also press CNTL-F5


Download ppt "The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4."

Similar presentations


Ads by Google