syntax and semantics

21
Syntax And Semantic

Upload: api-3739054

Post on 13-Nov-2014

40 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Syntax and Semantics

Syntax And

Semantic

Page 2: Syntax and Semantics

SYNTAX:

A set of strict grammar rules, symbols, and special words used to construct a computer program.

There are syntax rules (grammar: how valid instructions are written, what combinations of symbols can be used)

Page 3: Syntax and Semantics

Cont..

Syntax is the grammar of the language, that is the rules for constructing valid statements in that language.

If you break the rules the compiler will complain.

Page 4: Syntax and Semantics

Cont….

Study of the rules that govern the structure of sentences, and which determine their relative grammaticality. The term syntax can also be used to refer to these rules themselves, as in “the syntax of a language”.

Page 5: Syntax and Semantics

SYNTAX ERROR:

If a syntax error is encountered during compilation( mapping high level language to low level language) it must be corrected if the source code is to be successfully compiled.

Page 6: Syntax and Semantics

Cont..

Syntax errors may also occur when an invalid equation is entered into a calculator. This can be caused by opening brackets without closing them, or less commonly, using several decimal points in one number.

Page 7: Syntax and Semantics

Cont..

GOOD compiler should help in identifying and locating errors.

Any token which doesn’t match grammar rule is part of syntax error i.e arithematic expression with unbalance paranthesis.

Page 8: Syntax and Semantics

Recovery:

If we have a good idea of common error that might be encountered at hand.

End with delimiter

Never go in infinite loop

Replace comma (,) by semicolon(;)

Global correction

Page 9: Syntax and Semantics

Semantic:

Semantic is

MEANING OF STATEMENT

COMPATIBILITY OF STATEMENT

CORRECT USAGE OF STATEMENT

Page 10: Syntax and Semantics

Cont..

Computers can't read your mind, so they don't help much if the meaning of your program is not what you wanted.

Page 11: Syntax and Semantics

Cont..

In this regard, semantics permits programs to be separated into their syntactical part (grammatical structure) and their semantic part (meaning).

Page 12: Syntax and Semantics

FOR EXAMPLE

IF INTEAD OF

X = 1; STMT

IT WERE GIVEN AS

Y = 1;

WHERE X was declared in program

BUT Y was not

Page 13: Syntax and Semantics

Cont..

Scope of variable is the range of program instruction over which the variable is known.

If the program uses undeclare name such as

-Y = 1

Page 14: Syntax and Semantics

Cont..

Introduces (ambiguous name)

For example

int X

char X

Page 15: Syntax and Semantics

C++ Syntax and Semantics

3. Mark the following expressions as either legal or illegal, Indicate whether the value is an integer or a floating point value.   Valid Invalid   integer floating point

10 + 3-9.4 - 6.2   10.0 / 3.0   10 / 3   4 / 8  

Page 16: Syntax and Semantics

Cont…

x * y = c; invalid

y = con; valid

const int x : 10; invalid

int x: a = b % c; invalid

Page 17: Syntax and Semantics

ALGORITHM

Step-by-step procedures for solving a problem in a finite amount of time.

Algorithms encompass far more than computer programs.

We are learning to write algorithms and the computer is simply a fast and flexible tool for implementing algorithms.

Page 18: Syntax and Semantics

An algorithm is an ordered set of instructions such that:     (1) There are a finite number of steps.      (2) It takes a finite amount of time to execute the steps.      (3) Each step is precise in meaning.    

Page 19: Syntax and Semantics

(4) It is deterministic.during  the execution of an algorithm, the information must be sufficient to determine uniquely and completely the actions required by each step.

Page 20: Syntax and Semantics

(5) It solves a general class of problems

ensures that the algorithm is general enough that it solves a general set of problems, and not one specific/particular problem

Page 21: Syntax and Semantics

INCLUSION:

the execution of each step in an algorithm does not require creative skills, it requires rather only the ability to follow directions. In specifying behavior, an algorithm must be precise, unambiguous, complete, and correct.