Programming Language Technology Olaf Chitil o chitil kent ac by hubeybrown

VIEWS: 3 PAGES: 9

									                    CO524                          Overview
          Programming Language                     Example: simple arithmetic
            Technology 2008-9                      Strings and structure
                                                   Trees in PLT and computing
    Olaf Chitil               Simon Thompson       Representing trees in programs
o.chitil@kent.ac.uk      s.j.thompson@kent.ac.uk   Examples, examples, examples …
                                                   Trees in your favourite programming
                      Lecture 1                    language.
                                                   CO524-1                               2




An example: simple arithmetic


          What is the value of the following
                   expression … ?
                                                                 2
CO524-1                                        3   CO524-1                               4




                                                                                             1
          +                 3
CO524-1       5   CO524-1       6




          *                 4
CO524-1       7   CO524-1       8




                                    2
Strings and structure                          Two processes: first parse …
It is difficult to evaluate the string          Turn the string 2+3*4 into the tree:
                    2+3*4                                            +
working character by character …                                 2           *

           … much easier once we see the                                 3        4
                   +           structure:
                   2           *               This is called parsing.
                           3       4
CO524-1                                   9    CO524-1                                   10




Two processes: … then evaluate                 Two processes: … then evaluate
Turn the tree for 2+3*4 into the value:        Turn the tree for 2+3*4 into the value:
                       +                                             +

                   2           *                                 2           12

                           3       4



This is called evaluation.                     This is called evaluation.

CO524-1                                   11   CO524-1                                   12




                                                                                              3
Two processes: … then evaluate                 Trees
Turn the tree for 2+3*4 into the value:        Trees like             +

                     14                                           2           *

                                                                          3       4


                                               are one of the most important data types
                                               in computing.
This is called evaluation.
                                               They represent structure and hierarchy.
CO524-1                                   13   CO524-1                                              14




Example: files and directories                  Interpreter
                                                                      +
File system …
                                                 2+3*4            2           *                  14

                                                                          3       4
                                                 source   parse       tree            evaluate   value
                                                program



                                               Limitations: evaluation of tree
                                               • not so efficient
                                               • implemented in which language?
CO524-1                                   15   CO524-1                                              16




                                                                                                         4
                                                                    Programming Language
Hence: Compiler                                      PushLit 2      Technology
                          +                          PushLit 3
                                                     PushLit 4      Parse: uncover the structure in programs.
  2+3*4               2           *                  DoMult
                                                     DoAdd          Interpret or evaluate: run over the tree
                              3       4                             representing the program / expression.
  source     parse            tree        generate    target
 program                                   code      program        Type check: run over the tree.
                                                                    Optimise: turning one tree into another.
Target language usually closer to machine code
• more efficient                                                     Code generate: flatten the tree to byte
• directly executed or further interpreter / compiler               code, assembler code etc.
CO524-1                                                        17   CO524-1                                    18




Not just PLT … e.g. XML                                             Not just PLT … e.g. GUI input
XML technology: common interchange format:
<book>
<title>My First XML</title>
<prod id="33-657" media="paper"></prod>
<chapter>Introduction to XML
<para>What is HTML</para>
<para>What is XML</para>
</chapter>
<chapter>XML Syntax
<para>Elements must have a closing tag</para>
<para>Elements must be properly nested</para>
</chapter>
</book>
CO524-1                                                        19   CO524-1                                    20




                                                                                                                    5
Representing trees in programs                   Representing trees in programs
An arithmetical expression is                    An arithmetical expression is
• either a number: 24, -47, …                    • either a number: 24, -7, …
• or 2 expressions added together: e1+e2,        • or 2 expressions added together: e1+e2,
• or 2 expressions multiplied: e1*e2.            • or 2 expressions multiplied: e1*e2.

                                                 What it means to be an expression is
                                                 explained in terms of itself … recursion.
CO524-1                                     21   CO524-1                                     22




                                                  data Expr =
                                                    Num Integer |
Representing trees in programs                      Add Expr Expr |
                                                    Mult Expr Expr

An arithmetical       either a number: 24, -7,    Num 24
                                                                       either a number: 24, -7,
expression Expr is                                Num (-47)

                      or two expressions
 data Expr =                                                           or two expressions
   Num Integer |      added together: e1+e2,
                                                  Add (Num 2)
                                                                       added together: e1+e2,
   Add Expr Expr |
   Mult Expr Expr                                     (Mult (Num 3)
                      or two expressions                    (Num 4))
                      multiplied: e1*e2.
                                                                       or two expressions
CO524-1                                     23   CO524-1               multiplied: e1*e2. 24



                                                                                                  6
To evaluate an Expr                              To show an Expr
                      eval :: Expr -> Integer    Turn an Expr into     show ::   Expr -> String

 data Expr =          eval (Num n) = n
                                                 a string.             show (Num n) = show n
   Num Integer |      eval (Add e f)                                   show (Add e f)
                                                  data Expr =
   Add Expr Expr |      = eval e + eval f                                = "(" ++ show e ++ "+"
                                                    Num Integer |
   Mult Expr Expr     eval (Mult e f)                                       ++ show f ++ ")"
                                                    Add Expr Expr |
                        = eval e * eval f                               show (Mult e f)
                                                    Mult Expr Expr
                                                                         = exercise for you now



CO524-1                                     25   CO524-1                                      26




Class: Expr in Java                              Representing Expr in Java?
Implement a representation of Expr in            Need three different kinds of expression:
Java.                                            numbers, additions and multiplications.

Show how you would represent 2+3*4 as            In evaluating, do different things with
an Expr.                                         different kinds of expressions.

Implement a method to evaluate an Expr.          In printing, ditto.


CO524-1                                     27   CO524-1                                      28




                                                                                                   7
Why study Programming                         Technology used in numerous
Language Technology?                          places
                                              Generating and transforming a complex data structure.
You use compilers/interpreters
(error messages, code efficiency, ...)                   music data               3D model

You use programming languages
(why types help)
                                                  sound          score

Methods applicable far beyond
programming languages                         XML: an interchange format for structured data.

CO524-1                                  29   CO524-1                                             30




How will the course continue?                 How will the course continue?
Simon: extracting and process the             Combine classes and lectures.
information: parsing, tree transformation …
                                              Integrate short exercises:
Olaf: working with trees, producing             low cost, high value.
machine code, abstract machines, … .
                                              Anonymous question pages … one each.
Simon Weeks 1-6, Olaf Weeks 8-12.
                                              Simon sound recording and experimental
                                              links with slides ("podcasting").
CO524-1                                  31   CO524-1                                             32




                                                                                                       8
Assessment pattern
1   Small assessments during the lectures 20%,
    plus longer assessments:

2   Monday Week 3 Thursday Week 4 sjt      15%
3   Monday Week 7 Thursday Week 8 sjt      15%

4   Monday Week 9 Thursday Week 10 oc 25%
5   Monday Week 11 Thursday Week 12 oc 25%
CO524-1                                      33




                                                  9

								
To top