You are here: Home > PLC C++ > PLC Chapter II – Describing Syntax and Semantics

PLC Chapter II – Describing Syntax and Semantics

A. Definition

The word syntax comes from Ancient Greek: σύνταξις “coordination”, which consists of σύν syn, “together,” and τάξις táxis, “an ordering”.
In computer science, the syntax of a computer language is the set of grammatical rules that defines the combinations of words and symbols that are considered to be a correctly structured document or fragment in that language.
Semantic focuses on the relationship between signifiers— like words, phrases, signs, and symbols—and what they stand for, their denotation. In computer science, semantics is the meaning of the expressions, statements, and program units.

B. C++ Syntax

The syntax of textual programming languages is usually defined using a combination of regular expressions (for lexical structure) and Backus–Naur Form (for grammatical structure) to inductively specify syntactic categories (nonterminals) and terminal symbols. Syntactic categories are defined by rules called productions, which specify the values that belong to a particular syntactic category. Terminal symbols are the concrete characters or strings of characters (for example keywords such as define, if, main, or void) from which syntactically valid programs are constructed.

The following are the examples of basic C++ keywords syntax:

  1. #include, which is used to insert the library of functions we are going to use in our program. For example, #include , which includes the standard input and output codes.
  2. int main(), which is called first by the program after the initialization. A statement return 0; should be written at the end of the codes inside int main().
  3. cout, which is used to print output on the screen.
  4. cin, which is used to input data by user and store it as a variable.
  5. int, char, etc., which are used to declare a variable, according to the data types. (int for integer, char for a character, string for string of characters, etc.)
  6. if, which is used in selection and will execute the codes if the condition is true.

There are also various points that should be paid attention to:

  1. The semicolon (;) is used to separate each statements.
  2. The curly brackets ({ }) must be used on several functions such as if, for, and while if there are more than one statements that will be executed in the function.
  3. C++ is a case-sensitive programming language, so if you use capitalization incorrectly, the compiler will give an error message.
  4. To set the value of char and string data types or to print text on a program, double quotation mark (“ “) is necessary.
  5. For comments, we use double slash (//) to mark the following text in a line as comment or type them between /* and */ to mark several lines of code or text as comments.

Here are an example of a program with correct syntax in C++:

#include
using namespace std;

int main() {

int x,y,z;
cout<<“Masukkan nilai x dan y : “; cin>> x >> y;
fflush(stdin);
z=x+y;
if (z>50) cout<<“Jumlah dari x dan y lebih dari 50”;
else cout<<“Jumlah dari x dan y kurang dari 50”;
getchar();
return 0;

}

C. Semantics
The requirements for a methodology and notation for semantics are as follows:
– Programmers need to know what statements mean
– Compiler writers must know exactly what language constructs do
– Correctness proofs would be possible
– Compiler generators would be possible
– Designers could detect ambiguities and inconsistencies
There are some kinds of semantics:
Operational semantics, which describes the meaning of a program directly by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement.
Denotational semantics, where each phrase in the language is interpreted as a denotation, i.e. a conceptual meaning that can be thought of abstractly.
Axiomatic semantics, where axioms or inference rules are defined for each statement type in the language (to allow transformations of logic expressions into more formal logic expressions).

Tags: , ,

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Leave a Reply