Short Article
A Better Syntax
Design For Programming Languages To Avoid Inconsistencies Problems
ESSAM
M. ARIF*
Department
of Electrical and Computer Engineering, College of Engineering,
Umm
Al-Qura University, P. O. Box 6240, Makkah, Saudi Arabia
تصميم أفضل لقواعد
البناء في لغات البرمجة بقصد تجنب
المشاكل المتناقضة
على مصممي لغات البرمجة أن لا يقدموا فقط قواعد بناء إلى المطورين، وإنما يجب عليهم تصميم قواعد بناء مثالية تعالج المشاكل المؤثرة على تصميم اللغة. وبملاحظة المبرمجين المستخدمين للغة البرمجة سي++ وجدنا انه من السهل عليهم الوقوع في هذه المشاكل المتناقضة مثل إهمال التهيئة والتعريف غير الدقيق مع قواعد اللغة. وبمقارنة هذه المشاكل مع لغة البرمجة الكينونية (Object-Oriented Programming) "الرسالة" وجدنا أن قواعد بناء هذه اللغة قادرة على تجنب مثل هذه المشاكل المتناقضة.
Programming language designers should not only provide
syntax to the developers, but they should design a useful syntax idiom to
remedy some problems that impact language design. Observation using C++
programming language showed us that programmers could easily fall into
inconsistency problems such as neglecting initialization, imprecise definition,
and unwrapping. Comparing these problems with Al-Risalah object-oriented
programming language showed us that the language's syntax is capable of
avoiding these problems.
Keywords: Al-Risalah language, object oriented programming, language design, syntax, Inconsistencies problems.
![]()
INTRODUCTION
Many
factors are involved in designing a didactic, rational, and pragmatic
object-oriented programming language. One of the most important factors is the
syntax. It is the core of any programming language and once it is well defined
and designed, inconsistencies and ambiguities problems could be avoided. This
article not only discusses the importance of avoiding inconsistencies in the
syntax, but it also provides a better syntax design to solve these problems.
Preventing beginners from falling into these problems can help them write
better programs from the start. Our experience has strongly influenced the
design of Al-Risalah an object oriented programming language (Arif 1995).
Al-Risalah’s grammar is an LR grammar having bottom-up parsing, where the L
indicates that the input string is scanned from left to right and the R
indicates that a right derivation is being constructed. LR parsers are
efficient (fast) and can find errors as soon as they occur. LR parsers have two parts: a table and
a driver. Al-Risalah’s parser uses a variation of the typical LR parsing
algorithm. A modified parsing table is constructed instead of the yacc-generated
table (Arif & Armouti 1998).
NEGLECTING
INITIALIZATION INCONSISTENCIES
Inconsistencies
occur in the language when it neglects variable or object initialization. In
such a case, initialization is not mandatory, which leads to incorrect result.
In the following code fragment, which is part of the C++ programming language:
int uninit, sum;
sum
= 9;
sum
+= uninit;
sum will produce inconsistent
result since the variable uninit is uninitialized. Uninitalized variable
could also cause abnormal program termination as in the following code fragment:
int count;
while
(count < 10)
{
cout
<< count << endl;
count++;
}
In
object-oriented programming a constructor is used to initialize the class data
items. But the problem occurs when the language's syntax does not force the
programmer to declare a constructor within the class declaration. Consider the
following class and its implementation:
class
counter
{ private:
unsigned
int count;
public:
void
inc_count ( )
{
count++; }
int
get_count( )
{
return count; }
};
void
main( )
{ counter c1;
c1.inc_count(
);
cout
<< c1.get_count( ) << endl;
}
A
wrong result is produced since the syntax is compiled without invoking an error
message about an undeclared and undefined constructor to initialize the data
item count. Al-Risalah language syntax solves such a problem by
forcing the programmer to declare a constructor within the class declaration as
follows:
InterfaceDecl interfaceDecl ®
'interface' 'colon' constructorDcl msgDecls
ConstructorDcl
constructorDcl ® 'constructor' '(' ')'
initStatement initStatementList
InitStatement initStatement ®
'name' '=' 'dec'
InitStatementList
initStatementList ® 'semicolon' initStatement
InitStatementListEmpty
initStatementList ® Î
The
above syntax ensures that the programmer must declare a constructor. Since data
items and their type will be constructed in the symbol table, the semantic
analyzer will report informative error messages regarding uninitialized data
items. Therefore, Al-Risalah language's syntax is capable of solving
uninitialized variables or objects problems.
IMPRECISE
DEFINITION INCONSISTENCIES
Inconsistencies
occur in the language when its syntax is not well defined. For instance, in C++
programming language, a programmer can declare a method, call it, and then
define it. Alternatively, he can define the method before the main program then
calls it. However, undeclared methods will provoke an uninformative error
message (Arif & Evens 1997). This kind of inconsistency in the language's
syntax will lead to misunderstanding and confusion. Since Al-Risalah is a pure
object-oriented programming language, such a problem can be avoided. The
following class declaration syntax for Al-Risalah OO programming language:
ClassDecl classDecl ®
'class' 'name' super 'begin' classDeclBdy 'end'
ClassDeclBdy
classDeclBdy ® collaboratorsDecl interfaceDecl
privatesDecl
attributesDecl
will
ensure consistency, encapsulation, and abstraction since interface and private
methods must be declared within the class declaration. It will also help the
programmer to organize his or her program.
UNWRAPPING
INCONSISTENCIES
Most
programming languages require wrapping the statement lists either by begin
and end (e.g., Pascal) or braces (e.g., C++). But if the statement list
consists of only one single statement then wrapping is not required, which
leads to inconsistency problems. One of the most important problems that cause
inconsistency is the dangling else problem. Consider the following code
fragment in C++:
if
( a == b)
if (b
== c)
cout
<< “a, b, and c are the same”;
else
cout
<< “a and b are different”;
Suppose
the programmer assigned the values 2, 3, and 3 to the variables a, b,
and c, respectively. Since the first test expression will be evaluated
to False, the programmer expects the else part to be invoked and print
the statement:
a and b are different
But
in fact nothing is printed since the else part belongs to the inner if. Language
syntax designers should anticipate such a mistake from beginners or even
professionals specially when their programs contain many nested ifs, in
which the else part becomes hard to know to which if it belongs.
Another
way of expressing unwrapping inconsistency problem is by what is called the
“indentation syntax mismatch”, where the programmer intended a way but the
program has been read by the syntax analyzer in another way. However, the
syntax editor of the language encourages programmers to fall into this problem.
Consider the following code fragment, which is written in C++ programming
language:
if
(x < 9)
a =
1;
b =
2;
In
the above example, the programmer intended to assign the values 1 and 2 to the
variables a and b, respectively, if the test condition evaluated
to True. However, the syntax analyzer will not parse the above if
statement the way it is intended by the programmer, because the value 2 will be
assigned to the variable b no matter what the evaluation of the test
condition is. Therefore, the problem is neither with syntax analyzer nor with
the programmer, but it is actually with the design of the language's syntax.
There
are many examples where unwrapping inconsistency problems are encountered. In
such a situation the syntax analyzer can not detect the error to report an
error message. We observed another
way of expressing the problem. The following program, coded in C++ programming
language, will produce an inconsistent answer since the multiple statements on
the statement list of the first for loop are unwrapped.
const
int size = 4;
void
main()
{
float
grades[size];
float
score = 0.0, total = 0.0;
cout
<< "Enter scores for Four tests : ";
for
(int j = 0; j < size; j++)
cin
>> score;
grades[j]
= score;
for
(j=0; j<size; j++)
total
+= grades[j];
cout
<< "Total= " << total;
float
avrge = total / size;
cout
<< " Avrage= " << avrge;
}
From
the above three problems, we can ensure that unwrapping causes inconsistency in
the language. We have defined Al-Risalah language so it can help the programmer
to solve these problems. We have applied some constraints when applying the
statement list, in which the programmer must wrap the statement list with begin
and end whether it includes a single statement or multiple statements.
This technique will avoid programmers to fall into unwrapping inconsistencies,
which will lead to any of the above problems. For example, the syntax for the If
statement of Al-Risalah's language is as follows:
IfStatement
ifStatement ® 'if' expr 'begin' statementList
'end' ifStatementTail
IfStatementTailList
ifStatementTail ® 'else' 'begin' statementList
'end'
IfStatementTailEmpty
ifStatementTail ® Î
The
above syntax forces the programmer to wrap the statement list with begin
and end whether it consists of one single statement or multiple
statements. Applying this syntax will not only prevent the dangling else or the
indentation syntax mismatch problems, but it will ensure such problems will never
occur.
CONCLUSION
A
programming language's syntax should be designed without inconsistencies and
ambiguities. A better syntax has been designed and addressed to solve
inconsistency problems that impact language design. Applying the wrapping technique
can solve some of the inconsistencies in the language design. Methods must be
declared within the class declaration to ensure integrity, and abstraction. A
constructor must be declared and all class data items must be initialized in it
to ensure consistency and normal program termination. All these points have
been taken care of in the design of Al-Risalah’s language.
REFERENCES
Arif, E. M. 1995. Design of an arabic object-oriented programming language and a help system for pedagogical purposes. Ph.D. thesis, Illinois Institute of Technology,
Chicago, IL, U.S.A.
Arif, E. M. & Armouti, H. A. 1998. A contextual help system for "Al-Risalah" an object-oriented programming language. Proceedings of the Second Computer and its Applications Conference, 1: 47-52. Applied Science University, Amman, Jordan.
Arif, E. M. & Evens, M. C. 1997. Informative error messages. Proceedings of the Fifteenth National Computer Conference, 2: 741-752. King Fahad University of Petroleum and Minerals, Dhahran, Saudi Arabia.
(Received
4/12/1419; 21st March 1999, accepted 12/8/1420; 20th November 1999)