Website for the College study materials and cheats you can use

Translate

Defining and explaining Syntax of a programming language, Differences between syntax errors and semantics errors

Defining and explaining the Syntax of a programming language

A program in computer science is a sequence of instructions that a computer can execute (Eck, D.J. 2019 pg 34). This sequence of instructions needs to be written in programming languages (formal languages established by humans) before the computer can understand them and make execution. The reason is that human language (natural language) really differs from computer language in the sense that computer language is very strict, unambiguous (Eck, D. J. 2019 pg 34), they are not literal meaning that they mean exactly what they say: a correct input gives correct output but an error input gives an error output ( Downey.A. 2015 pg27) whereas human language is the opposite of the computer language characteristics. So with all these being said, the syntax of a language are the rules that determine how a program should be constructed in that specific language since every formal language has its own syntax.


Defining and explaining Semantics of a programming language

Semantics are referred to as the meaning of a written program. Elaborately a programming language semantics is the set of rules that determine the meaning of a program in a specific language ( Eck, D.J. 2019 pg 35).


Differences between syntax errors and semantics errors.

First, let us define what these two errors mean when they occur in programming in so doing we can easily grab the differences between them.


A syntax error is an error in a program that makes it impossible for the interpreter to interpret (Downey 2015 pg 37). This usually occurs when we do not respect the syntax of the programming language we are utilizing. And since formal languages are not literal, meaning that they mean exactly they say, the input we put is the output we get (Downey.A. 2015 pg 27).


A semantic error is an error in a program that makes the program do the opposite of what the programmer intended the program to do (Downey.A. 2015 pg 37). or causes the program to run incorrectly (Eck. Dj 2019 pg 74). Some semantic errors occur when a variable is not properly initialized. (when another variable type is used to initialize another variable type).


With each definition, we can clearly see the difference between these two types of programming errors. In a nutshell, a Syntax error has to do with the syntactic error of the programming language we are using and Semantic error has to do with the error occurred in the meaning of what the programmer wants the program to produce.


Example of Syntax error:

public class syntaxandsemanticerror {

public static void main(String[] args) {

/* This program will produce a syntax error because i missed putting the variable

* "principle" near the if conditional in parenthesis */

int principle;

principle = 87;

Double interest;


if principle > 98

interest = principle * 0.02;

else

interest = principle *2.1;

System.out.println(interest);


Example of Semantic errors:

// Semantic error

/* This will output a semantic error because i used a type variable for another variable type on number2*/

int number;

int calculate;

int number2;

number = 43;

number2 = 32.32;

calculate = number + number2;



// Second semantic error


/* I want (the programmer) the program to print in a new line but it will do something different

* This is because i used "print" instead of "println*/

String name;

name = "Israel";

System.out.print("Welcome, " + name + " Pleased to meet you");


// Note that this program won't be able to run due to the syntax and semantic errors it contains to run it try removing the errors

}

}


Please find below an attached file of the program.


REFERENCES:

Eck, D. J. (2019).Introduction to programming using Java, version 8.1. retrieved from http://math.hws.edu/javanotes.

Downey, A.(2015). Think python, How to think like a computer science

0 comments:

Post a Comment

Popular Posts

Categories

Contact Form

Name

Email *

Message *