Error semantico

Solo disponible en BuenasTareas
  • Páginas : 5 (1216 palabras )
  • Descarga(s) : 0
  • Publicado : 2 de enero de 2012
Leer documento completo
Vista previa del texto
C H A P T E R

2

Common Syntax and Semantic Errors

2.1

CHAPTER OBJECTIVES
• To understand the fundamental characteristics of syntax and semantic errors • To be able to identify specific common syntax and semantic errors frequently encountered by beginning programmers • To be able to interpret a syntax warning • To be able to apply appropriate techniques to correct these common errors2.2

SYNTAX ERRORS

A syntax error is a violation of the syntax, or grammatical rules, of a natural language or a programming language. The following sentence contains an error of English syntax:
I is going to the concert tonight.

If we write or say this sentence, other English speakers will know that we have used incorrect grammar, however they will still understand what we mean.Programming languages are not so forgiving, however. If we write a line of C++ code containing a syntax error, the compiler does not know what we mean. A syntax error is called a fatal compilation error, because the compiler cannot translate a C++ program into executable code if even one syntax error is present.
5

6

Chapter 2

Common Syntax and Semantic Errors

2.2.1 Syntax Errors: Summaryof Important Points • How are they detected? The compiler detects them when you try to compile your program. • Why do they occur? The syntax rules of C++ have been violated. • Is there object-code generated? No, so you cannot run the program. • Solution: Find the line(s) containing the syntax error(s) using the compiler’s flagged lines and error messages; using your textbook or other C++ referenceas a guide, correct them. • Remember, frequently, a syntax error occurs not in the line flagged by your compiler, but in some line above that line; it is often the previous line, but not necessarily. 2.2.2 Examples: Common Syntax Errors Some syntax errors are very common, especially for beginning programmers, and the examples that follow should help you identify and correct many syntax errors inwhatever program you are currently working on. The syntax diagrams in your C++ textbook or a C++ reference book should be your ultimate guide in correcting these types of errors. Different compilers report syntax errors in different formats. For these examples, we will assume that the compiler displays errors for a C++ program named “myprog.cpp” in the following way:
Syntax Error: Line ofprogram myprog.cpp

The first line indicates that a syntax error message is being displayed and then gives a brief description of what the compiler thinks the error is. The second line will give the line number on which the compiler has identified the error. Compilers that provide a graphical user interface (GUI) using windows and various graphical items to display information for you may display allsuch error messages in one window (which we will assume for our discussion here), or they may simply list the program with erroneous lines highlighted or pointed to by an arrow or other graphic, with written error messages shown off to the side. In any event, error messages displayed by different compilers generally are very similar. An important note about compilers: Modern compilers typicallyare very accurate in identifying syntax errors and will help you enormously in correcting your code. However, compilers often present two difficult problems for new programmers: (1) they frequently can miss reporting an actual error on one line but get “thrown off track,” then report errors on subsequent lines that are not truly errors; the compiler may then also display error messages which areincorrect; and (2) after encountering one true syntax error, compil-

2.2

Syntax Errors

7

ers often generate many incorrect syntax error messages; again, the compiler has been “thrown off track” by a particular error. Why does this occur? Basically, because a compiler is a very complex and sophisticated language-processing program, and no computer program can analyze any language as...
tracking img