COMPILER ALGORITHM NOTATION: AN INTERPRETER AND COMPILER CS499 B' Tech' Project - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

COMPILER ALGORITHM NOTATION: AN INTERPRETER AND COMPILER CS499 B' Tech' Project

Description:

return y INSET is. end. C code for Example 02. boolean exam(int x, int y) int n; ... NULL INSET OUTPUT UNION. INPUT INTERSECTION. CAL Program Structure ... – PowerPoint PPT presentation

Number of Views:197
Avg rating:3.0/5.0
Slides: 25
Provided by: charmC
Category:

less

Transcript and Presenter's Notes

Title: COMPILER ALGORITHM NOTATION: AN INTERPRETER AND COMPILER CS499 B' Tech' Project


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

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-gthead
  • Y G-gthead
  • while(X!NULL)
  • while(Y!NULL)
  • Productions Z K-gthead
  • while(K!NULL)
  • if(.YZ)
  • X1 Z
  • X1-gtnext J-gthead

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 gt 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 isn
  • printf("Enter the set")
  • for(int i0iltni)
  • scanf("\d",isi)
  • boolean tv true
  • for(int i0iltni)
  • if(isix)
  • return tv
  • for(int i0iltni)
  • if(isiy)
  • 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)
  • Its 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,_at_,
    . , ?, SEQCONCAT, ELOFSEQ
  • Conditional Operators , ?, lt, ,gt,

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
  • lttype_definitionsgt
  • ltvariable_declarationsgt
  • ltprocedure_declarationsgt
  • ltoptional_main_programgt
  • 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 variables type

14
CAL Program Structure (contd.)
  • Procedure declaration Procedures name followed
    by its parameters 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
Global Variables
Procedure Symbol Tables
Procedure entries
20
Symbol Table
Symbol Table Entries
21
THE COMPILER - Implementation
  • The compiler translate any CAL program into C
    code
  • The generated C code can be plugged into larger
    programs
  • The front end consists of Lexer.java and
    translator.java
  • The back end uses the editor.java and
    FileCreator.java in conjugation with
    translator.java
  • Data structures like sets are converted to linked
    lists
  • A header file setheader.h takes care of the
    structures and functions required for this

22
GUI AND WEB-ENABLING
  • Graphical User Interface
  • Has options for invoking the interpreter and the
    compiler
  • Has an editor for the programs and provides
    simple file handling and editing options
  • Web enabling
  • Uses the Apache Tomcat server
  • Requests are received via a Form and then the
    output is sent back to the client after
    interpretation or translation

23
References
  • 1 Steven S. Muchnick, Advanced Compiler Design
    and Implementation, Morgan Kauffman Publishers
    Inc.
  • 2 Ronald Mak, Writing Compilers and
    Interpreters, John Wiley and Sons, Inc., 1996
  • 3 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman,
    Compilers, Principles, Techniques and Tools
  • 4 Robert B. K. Dewar, The SETL Programming
    Language
  • 5 http//jflex.de/index.html
  • 6 http//www.cs.princeton.edu/appel/modern/java
    /
  • CUP/

24
THANK YOU !!
  • Questions??
Write a Comment
User Comments (0)
About PowerShow.com