No.4 Hussain Ahmad Liprol

  • Upload
    daai123

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 No.4 Hussain Ahmad Liprol

    1/4

    LAYMAN INTERPRETED PROGRAMMING LANGUAGE

    LIPROL

    HUSSAIN AHMAD AND MUHAMMAD ZUBAIR ASGHARInstitute of Computing and Information Technology, Gomal University Dera Ismail Khan

    (NWFP) Pakistan

    ABSTRACTThis work deals with the development of language interpreter. Developing a complete language compiler is

    somewhat tedious, but creating a language Interpreter is quite simple and easy. The language interpreter

    that has been developed here can be used very easily even by those, who have no previous programming

    background. The major phases of LIPROL (layman interpreted programming language) are: Lexical

    analysis, Syntax analysis, Symbol table management, and Semantic analysis. Methods of creating

    language interpreters are rarely taught in computer science courses. This paper unleashes the mechanics of

    token extraction, expression composition and expression parsing. LIPROL provides the students/users an

    interactive environment, easier to use than Basic language interpreter. It also provides excellent interactive

    debugging facilities.

    INTRODUCTION:

    In case of interpretive process, no object

    program is generated. In microcomputer

    environment, interpreters are becoming

    more popular, because overhead of

    interpretation is less as compared tocompilation process (Trembly, 1987). Basic,

    Java, Allis, and small talk-80 are interpreted

    Languages (Herbert,1987;Trembly,1985).

    LIPROL stands for layman interpreted

    programming language. It is BASIC likeinterpreter with small instruction set.

    Commands used in LIPROL are very easy to

    understand. Even a layman having no

    previous programming experience can use

    this interpreter very easily. LIPROL

    recognizes the following constructs;

    Show, Get, If, Then, Next, To, Go to, Call

    sub, Return, End.

    METHODOLOGY

    Internal working of LIPROL interpreter isgiven as under.

    Token Handling : First phase in translation

    process, whether it is compilation or

    interpretation is token generation. It is also

    called lexical analysis or scanning process.

    In this phase input string is decomposed into

    small pieces. Each piece is called lexeme or

    token. For example, the expression X + Y -

    (Z +3) has following tokens X, +, Y, -, Z, +,

    3. The function that decomposes input

    expression into tokens has following tasks.

    a. Omit tabs and spaces

    b. Extraction of each token

    c. Each class of token is given a

    unique internal representation no.

    d. Determine the type of token.

    There are two formats of token

    representation: External format, internal

    format

    External Format, also called string

    representation of token. e.g. INPUT is the

    external format of INPUT command in

    BASIC language. Rules of Interpreter design

    say that external format for token

    representation should be avoided, because it

    is inefficient approach. In Internal Format,

    unique integer is assigned to each command

    e.g. INPUT command is given 1, FOR isrepresented by 2 etc. In internal

    representation, integers are used instead of

    strings. Using .this approach, quite faster

    and efficient routines can be written.

    Analysis:

    In LIPROL, each instruction of program is

    represented and stored as a string. A

    function named next_ token ( ) is used that

  • 8/8/2019 No.4 Hussain Ahmad Liprol

    2/4

    returns next token. In this function one

    character is scanned at a time. Pointer is

    used that points to the next character to be

    scanned (read). In order to omit leading

    spaces from expression, another routine is

    used, called space( ) .After skipping out the

    spaces, pointer points to a variable, number,reserved word, operator, linefeed, or null.

    Consider the following expression:

    Show X+Y+ 20-(M + N) / 3

    When above expression is fed into "

    next_token () " function, then each character

    is scanned and entire expression is broken

    down into tokens and corresponding token

    type.

    Following table shows token and token

    type.

    Token Token type

    Show Reserve word

    x Identifier

    + Operator

    y Identifier

    + Operator

    20 Number

    - Operator

    M Identifier

    + Operator

    N Identifier

    ) Special Character / Operator

    3 Number

    Null Delimiter

    Note: Every expression is terminated with

    null string.

    Expression Parsing And Evaluation

    In proposed interpreter, expressions are

    restricted to contain just +, -, *, / and

    parenthesis. Following production rules of

    context free grammar are used to define

    expression.

    E T [+T] [-]

    T F [*F] [/F]

    F V, N or (E)

    Where E Expression

    T Term

    F Factor

    V variable

    N Number

    The square brackets mean optional and

    ____ means produce

    Illustration of T F [*F] [/F]

    It means, "Tem1 produces factor times

    factor of factor divided by factor" for the

    definition of tem e.g. 3+6 / C is expression.

    It has two terms 3 and 6/C. It has threefactors: 3, 6, C, consisting of two numbers

    and one variable while the expression 5 *

    (3+2) has two factors 5 and (3+2),

    comprised of one number and one

    parenthetical expression.

    Overview of Parsing:

    There are different algorithms to parse and

    evaluate an expression. In parsing, overall

    structure of expression (or source program)

    is checked. This process is analogous to

    determining the structure of a sentence in

    the English language. In such an instance weare interested in identifying certain classes

    such as subject, verb, Noun,

    Adjective etc: In parsing, also called

    syntax analysis tokens are grouped together

    into larger syntactic classes such as

    expression, statement etc: Parser (or syntax

    analyzer) outputs a syntax Tree. Parsing can

    be accomplished through different

    algorithms. Categorically speaking, all

    algorithms belong either to top down parsing

    or bottom up parsing. Brute fore, recursive

    decent, partial backup and no backupmethods fall under top down parsing.

    Bottom up parsing includes shift reduce,

    operator precedence and LR parsing

    methods (Peterson, 1972).

    Parsing Mechanism in LIPROL : In

    LIPROL, recursive descent (type of Top

    down parsing) parser is used. Recursive

    descent parser used in LIPROL can handle

    the following operators: +, -, *, /, Integer

    exponentiation, and the unary minus.

    It also deals with parenthesis correctly.Various functions are used for different

    purposes.

    ADDSUB ( ) function is used to add or

    subtract two terms. Muldiv( ) function

    carries out multiplication or division of two

    factors. Int-exp () function processes integer

    exponent. While unary () function checks,

  • 8/8/2019 No.4 Hussain Ahmad Liprol

    3/4

    whether operator is unary + or - .Entry point

    into the parser is get _exp () function. It uses

    a pointer pt. In order to evaluate an

    expression, "pt" is set to the beginning of

    string that holds the expression.

    Show error ( ) function is used to reporterror messages. When some error is

    Detected, show _ error () is called with the

    number of the error. Following list shows

    error numbers and corresponding error

    messages:

    Error 0 Syntax error

    Error 1 No expression Available

    Error 2 Parenthesis Problem

    Error 3 = Sign missing

    Error 4 THEN expected

    Error 5 To expected

    Error 6 Next without for

    Error 7 To many proc

    Error 8 Return without proc

    Variable handling:

    The LIPROL interpreter only recognizes the

    variables A through Z. 26-element array of

    integers is used to store each variable. Each

    variable uses one location in this array. The

    function var _search () handles variable

    relevant activities.Liprol Vocabulary :

    LIPROL interpreter recognizes the

    following set of reserve words, SHOW,

    GET, IF, THEN, FOR, NEXT, TO, GOTO,

    CALLSUB, RETURN, and END.

    Internal representation of these commands is

    given as under.

    Reserve word Code

    SHOW 1

    GET 2

    IF 3

    THEN 4

    FOR 5

    NEXT 6

    TO 7

    GOTO 8

    CALLSUB 9

    RETURN 10

    END 11

    Both internal and external formats are stored

    in symbol table.

    Results of Discussions:

    A standard text editor is required to create

    LIPROL program. The program is then read

    in and executed by the interpreter, using

    upload () function.Let's see the different commands used in

    interpreter.Show command:

    The general form of LIPROL command is

    SHOW

    Where argument-list is a list of variable or

    quoted strings separated by commas or

    semicolon. The function SHOW () executes

    SHOW command. The show command can

    be used to print a list of variables and quoted

    strings on the screen.

    Following are the some example or validSHOW statement.

    SHOW A; B; "It's a message"

    SHOW 7/2

    SHOW

    Last example simply displays a new line.

    GET Command: In LIPROL, the GET

    command is used to read information from

    keyboard into a variable. General format of

    GET command is as under.

    Get ["string"],

    Entry in square brackets is optional; it

    displays a prompting message, as mentionedby programmer. Variable-name is variable

    in which user input is stored.

    e.g. GET "enter Its no", No1

    GOTO Command:

    GOTO command is an important member of

    many procedural Languages. Its general

    format is given below.

    GOTO

    e.g. GOTO 20

    GOTO command is comparatively complex

    than other commands. The routine written

    for GOTO command allows both forward

    and backward jumps. In this routine entire

    program is scanned prior o the execution of

    GOTO statement and the location of the

    target line can be looked up? Consequently

    program control is transferred to desired

  • 8/8/2019 No.4 Hussain Ahmad Liprol

    4/4

    point. This routine is named as label-search

    (). Duplicate labels" error massage is

    reported by label-search () routine, because

    in LIPROL no two labels can be the same. If

    the label is not found, "null-pointer" is

    returned and an "undefined label" message

    is issued (Reinhard Wilhelw and DieterMauter, 2003).

    IF Statement:

    In LIPROL if statement takes following

    foffi1at

    IF (condition) THEN

    Statement(s)

    ELSE

    Statement (s)

    If condition (Boolean expression) is truethen statement or group of statements after

    THEN are executed, otherwise statement (s)

    following the word ELSE are executed.

    RUN-if 0 function executes the IF

    statement.

    For Loop:The general form of for loop is;

    FOR=

    TOSTEP

    Statement (s)

    Like C, for loops may be nested to several

    levels in Liprol also. To do so, stack-basedapproach is adopted in the construction of

    for loop. The stack-like behavior of loop

    causes each NEXT to is associated with the

    proper FOR (Pam Balbine, 1985).

    Call Sub Statement.

    In LIPROL, stand-alone subroutines are not

    supported; rather segment of a program can

    be called and returned by using call sub and

    return statements. The general form of call

    sub and Return is:

    Call

    Subroutine code

    Return

    Call and Retune statements are implemented

    using stack. Because like FOR Loop, one

    subprogram can call another.

    CONCLUSIONS:

    The development of LIPROL like interpreter

    is not difficult. If you have strong

    background in compiler and interpreter

    design i.e. lexical analysis, syntax analysis,

    symbol table management, semantic

    analysis, code optimization and code

    generation, then you can invent even yourown language, which reflects your own

    programming style and personality. One

    final sentence: the types of statements that

    you can interpret are bounded only by your

    imagination.

    REFERENCES

    1. DeRemer, F.L (1969) Practicaltranslators for LR (k) Language Ph.D

    thesis, MIT, Cambridge , Mass.

    2. Herbert (1987), C: the completereference , 2nd ed. Osborne /

    MCGYAW-Hill; pp. 176-189.

    3. Horning, J.I, (1971) Empiricalcomparison of LR(k) and precedence

    parsers, Technical Report CSRG-1,.

    University of Toronto.

    4. LaLonde, W.R (1971): An EfficientLALR parser generator, Technical

    Report CSRG-2, University of Toronto.

    5. Pam Balbine, G (1985), Fileorganization and addressing (Ph.D.

    Thesis), California Institute ofTechnology.

    6. Peterson, T.G. (1972) Syntax ErrorDetection , Correction & Recovery in

    Parsers (Ph.D. Thesis ), Stevens

    institute of technology.

    7. Reinhard Wilhelw, Dieter Maurter,(2003)Compiler design, Addison-

    Wesley

    Publishing company.

    8. Trembly-JP, Sorenson.-PG (1985).Theory and practice of compiler writing.

    2nd ed. Mc Graw-Hill , pp 208-211.