Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
1Activity
×
0 of .
Results for:
No results containing your search query
P. 1
Creating an Appropriate Programming Language for Student Compiler Project

Creating an Appropriate Programming Language for Student Compiler Project

Ratings: (0)|Views: 267|Likes:
Published by ijcsis
Finding an appropriate and simple source language, to be used in implementing student compiler project, is one of challenges, especially in cases when the students are not familiar with high level programming languages. This paper presents a new programming language intended principally for beginners and didactic purposes in the course of compiler design. SimJ, a reduced form of the Java programming language, is designed for
a simple and faster programming. More readable code, no complexity, and basic functionality are the primary goals of SimJ. The language includes the most important functions and data structures needed for creating simple programs found generally in beginners programming text books. The Polyglot compiler framework is used for the implementation of SimJ.
Finding an appropriate and simple source language, to be used in implementing student compiler project, is one of challenges, especially in cases when the students are not familiar with high level programming languages. This paper presents a new programming language intended principally for beginners and didactic purposes in the course of compiler design. SimJ, a reduced form of the Java programming language, is designed for
a simple and faster programming. More readable code, no complexity, and basic functionality are the primary goals of SimJ. The language includes the most important functions and data structures needed for creating simple programs found generally in beginners programming text books. The Polyglot compiler framework is used for the implementation of SimJ.

More info:

Published by: ijcsis on Jul 07, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See More
See less

07/07/2011

pdf

text

original

 
(IJCSIS) International Journal of Computer Science and Information Security,Vol. 9, No. 6, 2011
Creating an Appropriate Programming Language for Student Compiler Project
Elinda Kajo Mece
Department of Informatics EngineeringPolytechnic University of TiranaTirana, Albaniaekajo@fti.edu.al
 Abstract 
 — Finding an appropriate and simple source language, tobe used in implementing student compiler project, is one of challenges, especially in cases when the students are not familiarwith high level programming languages. This paper presents anew programming language intended principally for beginnersand didactic purposes in the course of compiler design. SimJ, areduced form of the Java programming language, is designed fora simple and faster programming. More readable code, nocomplexity, and basic functionality are the primary goals of SimJ. The language includes the most important functions anddata structures needed for creating simple programs foundgenerally in beginners programming text books. The
 
Polyglotcompiler framework is used for the implementation of SimJ.
 Keywords-
 
compiler design; new programming language; polyglot  framework 
I.INTRODUCTIONA compiler course takes a significant place in computer science curricula. This course is always associated with animplementing project. Being a multidimensional course, itrequires the students to be familiar with high level programming languages among the other things. The firstimpact with these high level languages is almost alwaysconsidered confusing because of their complexity. This becomes more obvious in object-oriented languages like Java[8]. Object-orientation [15] hinders to learn Java step-by-stepfrom basic principles, because right from the beginning thelearner has to define at least one public class with a methodwith signature public static void main(String[] args). So theteacher has two choices here: trying to explain most of theconcepts involved (classes, methods, types, arrays, etc.) or just provide the surrounding program text and let the learner addcode to the body of the method main.SimJ is a simple, Java based programming language. It isconceived and designed to ease teaching of basic programming to beginners. We believe that they should learneasily the basic concepts, before they are exposed to morecomplex programming issues. It is much simpler for a new programmer to write println ("Hello world) instead of writinga confusing line like System.out.println ("Hello world"). Thissimple but concise example shows the importance of the firstimpact with programming languages. The role of SimJ is tomake this impact less “painful”.Compiler frameworks are widely used as a simple tool for implementing new languages based on existing ones. Thecomplexity begins to increase if the differences between theexisting language and the new one become significant [4].That is why we used Java as a base language for SimJ. For this purpose we have chosen Polyglot [4,5] as a compiler framework for creating compiler for languages similar to Java.II.THE POLYGLOT FRAMEWORK Polyglot is an extensible Java compiler toolkit designed for experimentation with new language extensions. The base polyglot compiler, jlc ("Java language compiler"), is a mostly-complete Java front end [1]; that is, it parses [1,2] and performs semantic checking on Java source code. Thecompiler outputs Java source code. Thus, the base compiler implements the identity translation. Language extensions areimplemented on top of the base compiler by extending theconcrete and abstract syntax and the type system [4].After type checking the language extension, the abstractsyntax tree (AST) [1,14] is translated into a Java AST and theexisting code is output into a Java source file which can then be compiled with javac.Polyglot supports the easy creation of compilers for languagessimilar to Java. The Polyglot framework is useful for domain-specific languages, exploration of language design, and for simplified versions of Java for pedagogical use. As mentionedabove, the last part is where we intend to focus on this paper.A Polyglot extension is a source-to-source compiler thataccepts a program written in a language extension andtranslates it to Java source code [4,5]. It also may invoke aJava compiler such as javac to convert its output to bytecode[13]. A SimJ oriented view of this process, including theeventual compilation to Java bytecode, is shown in figure 1.
Figure 1. The Polyglot Compiler Framework Architecture
36http://sites.google.com/site/ijcsis/ISSN 1947-5500
 
(IJCSIS) International Journal of Computer Science and Information Security,Vol. 9, No. 6, 2011
The first step in compilation is parsing input source code to produce an AST. Polyglot includes an extensible parser generator, PPG [5], which allows the implementer to definethe syntax of the language extension (SimJ in our case) as a setof changes to the base grammar for Java [7]. The extendedAST may contain new kinds of nodes either to representsyntax added to the base language or to record newinformation in the AST.The core of the compilation process is a series of compilation passes applied to the abstract syntax tree. Both semanticanalysis and translation [1] to Java may comprise several such passes. The pass scheduler selects passes to run over the ASTof a single source file, in an order defined by the extension,ensuring that dependencies between source files are notviolated. Each compilation pass, if successful, rewrites theAST, producing a new AST that is the input to the next pass.A language extension may modify the base language passschedule by adding, replacing, reordering, or removingcompiler passes. The rewriting process is entirely functional;compilation passes do not destructively modify the AST.Compilation passes do their work using objects that defineimportant characteristics of the source and target languages. Atype system object acts as a factory for objects representingtypes and related constructs such as method signatures[4,5].The type system object also provides some type checkingfunctionality. A node factory [4] constructs AST nodes for itsextension. In extensions that rely on an intermediate language,multiple type systems and node factories may be used duringcompilation. After all compilation passes complete, the usualresult is a Java AST. A Java compiler such as javac is invokedto compile the Java code to bytecode.III.SIMJ PROGRAMMING LANGUAGESimJ (stands for Simple Java) is a simplified version of theJava programming language conceived especially fo beginners. The language is very simple, easy to learn and isvery similar to Java. Previous work has been done in this field(i.e. the J0 programming language [5] but these languages arequite different compared to Java syntax [7]. We think thatsimilarity with Java is very important in order to allow the programmer to switch to Java without any problems regardingthe syntax when he thinks is ready to explore the full potentialand the advanced features of it.Figure 2 shows an example of the same code written in Javaand in SimJ. This example shows, as mentioned above, thatthe code in SimJ is clearly more readable than the one in Java.Generally, programming courses and textbooks for beginnersinclude many programs that during their execution require or the input of the user. In Java this part it’s definitely neither simple nor easy to implement at the beginning level. Weaddress this problem by removing the complex part andleaving only the “understandable” one (i.e. readLine()). public class A {public static void main(String[] args) {try {BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));System.out.print(“Your name:” );String name = reader.readLine();System.out.print(“\nHello, ” + name + “!”);}catch (IOException ioexeption) {System.out.println(ioexeption);}}}class A {main() {print(“Your name:”);String name = readLine();print(“\nHello, ” + name + “!”);}}
Figure 2.
 
Example code writen in Java and SimJ
The simplified versions of the printing methods are quiteobvious, since they are almost always used in simple programs. It is also important to mention that, compared toJava, the structure of the program is unchanged thus preserving its object-orientation character.Another important goal of this language is to help teaching of compiler design [1].SimJ language specification [3,10,11] shown in figure 3 isvery simple, short, equipped with the fundamental and mostlyused parts of a programming language at the beginning level[9,7]. Related work (i.e. MiniJava [1]) shows that simplicity isthe primary characteristic of these languages.As mentioned previously we think that similarities with Javaare important but also they should not lose their identity. InMiniJava for example the System.out. println(), that is thesame as in Java, is defined to do the printing but the meaningof System.out in this language cannot be found. With SimJ wetry to address these problems by creating a simple but welldefined language that syntactically talking is not a reducedexact copy of the mother language but has its own identity.
Program ::= MainClass ( Class )*MainClass ::= "class" Identifier "{" "main" "(" ")" "{" Statement "}" "}"Class ::= "class" Identifier "{" (Variable)* (Method)* "}"Variable ::= Type Identifier ";"Method ::= Type Identifier "(" (Type Identifier ("," Type Identifier)*)?")" "{" (Variable)* (Statement)* "return" Expression ";" "}"Type ::= "boolean"| "int"| "char"| "string"
37http://sites.google.com/site/ijcsis/ISSN 1947-5500
 
(IJCSIS) International Journal of Computer Science and Information Security,Vol. 9, No. 6, 2011
| "int" "[" "]"| Identifier Statement ::= "{" ( Statement )* "}"| "if" "(" Expression ")" Statement "else" Statement| "while" "(" Expression ")" Statement| "for" "(" Expression ";" Expression ";" Expression ")" Statement| "switch" "(" Expression ")" "{" ("case" Expression ":"Statement "break" ";")* "default" ":" Statement "}"| "print" "(" Expression ")" ";"| "println" "(" Expression ")" ";"| "readLine" "(" ")" ";"| "readInt" "(" ")" ";"| Identifier "=" Expression ";"| Identifier "[" Expression "]" "=" Expression ";"Expression ::= Expression ( "||" | "&&" | "<" | ">" | "!=" | "==" | "+" | "-"| "*" | "/" ) Expression| Expression "[" Expression "]"|Expression "."Identifier"("(Expression("," Expression)*)?")"| <INTEGER>| <STRING>| <CHARACTER>| "true"| "false"| Identifier | "this"| "new" "int" "[" Expression "]"| "new" Identifier "(" ")"| "!" Expression| "(" Expression ")"Identifier ::= <IDENTIFIER>Figure 3: SimJ language specification
This is an important point that helps reducing possibleambiguities and makes the language more understandable.SimJ includes the basic building blocks of a programminglanguage. From this point of view it is quite similar with
 
Java[8,7]. We have implemented the basic primitive data types(figure 2):
 boolean – true or false
int – integers
char characters
string – sequence of characters (string in SimJ for simplicity is considered a primitive data type)
int[] – array of integersMostly used control flow statements [9,8] are implemented inSimJ (figure 2). Their syntax is the same as in Javaconsidering that they have no redundant complexity to beremoved:
if else
for 
while
switchPrincipal operators [9,8] are also present in SimJ. Theseinclude: addition, subtraction, multiplication, division, logicaland, logical or, logical not, smaller than, greater than, notequal, equal.IV. I
MPLEMENTATION
For the implementation of SimJ we have used Polyglot as aframework that improves and simplifies compiler design for languages similar to Java. This process consists in creating anew language extension. Extensions (in our case SimJ) usuallyhave the following sub packages [5]:
ext.simj.ast
AST nodes specific to SimJlanguage.
ext.simj.extension
New extension anddelegate objects specific to SimJ.
ext.simj.types
Type objects and typing judgments specific to SimJ.
ext.simj.visit
– Visitors specific to SimJ.
ext.simj.parse
– The parser and lexer for theSimJ language.In addition, our extension defines the class
ext.simj.ExtensionInfo [5]
, which contains theobjects which define how the language is to be parsed andtype checked. There is also a class
ext.simj.Version
defined [5], which specifies the version number of SimJ. The
Version
class is used as a check when extracting extension-specific type information from
.class
files.The design process of SimJ includes the following tasks [5]: 
Syntactic differences between SimJ and Java aredefined based on the Java grammar found in polyglot/ext/jl/parse/java12.cup.
Any new AST nodes that SimJ requires are defined based on the existing Java nodes found in polyglot.ast(interfaces) and polyglot.ext.jl.ast (implementations).
Semantic differences between SimJ and Java aredefined. The Polyglot base compiler (jlc) implementsmost of the static semantic of Java as defined in theJava Language Specification [7].
Translation from SimJ to Java is defined. Thetranslation produces a legal Java program that can becompiled by javac.We implement SimJ by creating a Polyglot extension withthe characteristics described above. Implementation followsthese steps [5]:
build.xml
is modified and a target for SimJ isadded. This is done based on the skeleton extensionfound in
polyglot/ext/skel
. Running thecustomization script
polyglot/ext/newext
copies the skeleton to
polyglot/ext/simj
, andsubstitutes our languages name at all the appropriate places in the skeleton.
A new parser is implemented using PPG. This is done by modifying
38http://sites.google.com/site/ijcsis/ISSN 1947-5500

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->