240 likes | 1.08k Views
THE PROBLEM. We can divide the problem into five basic parts:Defining a language for writing compiler algorithmsWriting an interpreter for the languageBuilding a compiler to convert an algorithm to C codeProviding a debuggerProviding a GUI and web-enabled interface. MOTIVATION. Development of
E N D
1. COMPILER ALGORITHM NOTATION: AN INTERPRETER AND COMPILER CS499 - B. Tech. Project Project Guide: Prof. S. K. Aggarwal
Project Partners: Abhinav Bhatele (Y1008)
Shubham Satyarth (Y1344)
2. THE PROBLEM We can divide the problem into five basic parts:
Defining a language for writing compiler algorithms
Writing an interpreter for the language
Building a compiler to convert an algorithm to C code
Providing a debugger
Providing a GUI and web-enabled interface
3. MOTIVATION Development of new and faster algorithms for different parts of the compiler is a growing trend
Algorithms generally written in pseudo-code
Optimization is the heart of advanced compiler design
This includes control and data flow analysis of programs
These algorithms are generally written in complex notations
We wish to save the compiler writer the trouble of writing algorithms in languages like C.
4. EXAMPLE 01 G: set of Productions
procedure closure(I) returns set of Items
I: in set of Items
begin
J: set of Items
X: Items
Y: Production
J := I
for each X INSET J
do
for each Y INSET G
do
if .Y NOTINSET J
then
J := J + .Y
fi
od
od
return J
end
5. C code for Example 01 struct Items
{
// code for Items
Items *next;
}
struct Production
{
// code for Production
Production *next;
}
struct setofItems
{
Items head;
}
struct setofProductions
{
Production head
}*G
// Initialize G
6. C code for Example 01(contd.) void setofItems closure(setofItems I)
{
setofItems *J = I;
setofProductions *K;
K = G;
Items *X;
Items *X1;
Production *Y;
X = J->head;
Y = G->head;
while(X!=NULL)
while(Y!=NULL)
{
Productions Z = K->head;
while(K!=NULL)
if(.Y==Z)
{
X1 = Z;
X1->next = J->head;
J->head = X1;
}
Z = Z->next;
Y = Y->next;
}
X = X->next;
return J;
}
7. EXAMPLE 02 procedure exam(x, y) returns boolean
x, y: out integer
begin
is: in intset
INPUT is
tv := true: boolean
z: integer
for each z INSET is (z > 0)
do
if x = z
then return tv
fi
od
return y INSET is
end
8. C code for Example 02 boolean exam(int x, int y)
{
int n;
printf("Enter the size of the set");
scanf("\%d",n);
int is[n];
printf("Enter the set");
for(int i=0;i<n;i++)
scanf("\%d",is[i]);
boolean tv = true;
for(int i=0;i<n;i++)
if(is[i]==x)
return tv;
for(int i=0;i<n;i++)
if(is[i]==y)
return true;
return false;
}
9. CAL - The Language On the lines of Informal Compiler Algorithm Language (ICAN), we have defined a new language called Compiler Algorithm Language (CAL)
It’s constructs are similar to languages like C and Pascal
Allows using natural objects like sets and arrays
Algorithms in compiler design can be easily converted into this form with minor modifications
10. CAL – Data Types and Operators Data Types
Generic Simple Types: boolean, integer, real, character
Constructed Types: enum, array … of, set of
Compiler-specific Types: for example,
Block = array [..] of array [..] of MIRInst
Operators
For Simple Types: +, -, *, /, %, ~,&,|,!
For sets: UNION, INTERSECTION, INSET, NOTINSET,@, . , ?, SEQCONCAT, ELOFSEQ
Conditional Operators: =, ?, <, =,>, =
11. CAL - Statements Simple Statements:
Assignment
Procedure call
Return and Goto
Input and Output
Compound Statements:
Beginning Internal Ending
if elif, else fi
case of, default esac
for do od
while do od
repeat until
12. CAL - Keywords
array begin boolean by
case character default do
each elif else end
esac false fi for
goto if in inout
integer nil od of
out procedure real repeat
Return returns set to
true until where while
Additional Keywords:
DIFF EXISTS FORALL NOTINSET
NULL INSET OUTPUT UNION
INPUT INTERSECTION
13. CAL – Program Structure A typical CAL file consists of:
<type_definitions>
<variable_declarations>
<procedure_declarations>
<optional_main_program>
Type definitions: Type name followed by an equals sign and type expression
Variable declaration: name of the variable followed by an optional initialization, followed by a colon and the variable’s type
14. CAL – Program Structure (contd.)
Procedure declaration: Procedure’s name followed by its parameter’s list in parenthesis, an optional return type, followed by its parameter declarations and its body
Main program: Has a form similar to other procedure
15. INTERPRETER – Implementation Front end of the interpreter:
Lexical Analyzer (lexer.java): 33 types of tokens
Syntax Analyzer (parser.java): more than 200 productions
The back end or the executor consists of the following parts:
Symbol Table cum Scratch Memory (SymbolTable.java)
Virtual Machine (VirtualMachine.java)
The Interpreter (MainProg.java)
16. Implementation Details One Pass Interpreter
We do syntax directed evaluation
Concept is similar to a Virtual Machine that does evaluation of statements and simulates a processor
The Symbol Table is used as a memory also and is used by the virtual machine
For procedure calls and loops, the virtual machine uses different objects of the executor
17. Implementation Details Each global variable encountered is directly stored in the symbol table
Each procedure has an entry in the symbol table with a pointer to a file which stored the actual code
This is done to avoid a second pass on the procedures
Symbol Table for the procedure is initialized as soon as the procedure is encountered
When there is a procedure call in main(), the code for that procedure is again parsed and this time executed
18. Symbol Table - Details A global symbol table with entries of global variables and procedures
Each procedure including main has its own symbol table
Two levels of the symbol table
Outer level entries just have the lexeme and a pointer to the location where the actual value is stored
Different linked lists for different data types like ArrList, SetList, etc.
Each entry in the symbol table points to a particular entry in one of these linked lists
19. Symbol Table - Structure