ICON - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

ICON

Description:

assigns the string value ' Hello, world ' to the identifier line, which is a variable ... line:= 'Hello, world' all 256 characters may occur in a string ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 42
Provided by: davidbru
Category:
Tags: icon | hello

less

Transcript and Presenter's Notes

Title: ICON


1
ICON
2
Icon
  • Icon is a very-high-level, typeless, expression
    language withbacktracking.
  • "Icon" does not mean "picture".

3
Whats Icon good for?
  • Tools
  • Prototyping
  • Trying out ideas
  • Quick programming
  • Text processing

4
Topics of Discussion
  • Program structure
  • Data types
  • Control constructs

5
Program structure
  • Files compiled together
  • Declarations
  • Procedures
  • Variables
  • Records

6
Hello, world.
  • Must begin with procedure
  • must conclude with end
  • must have a procedure main
  • procedure main()write("Hello, world")end

7
Procedures
  • procedure main( )
  • hello( )
  • end
  • procedure hello( )
  • write ("Hello, world ")
  • end

8
Procedure Parameters
  • Given in a list enclosed in parentheses
  • procedure main()
  • greet("Hello, ", "world ")
  • end
  • procedure greet(what,who)
  • writes(what)
  • write(who)
  • end
  • Produces
  • Hello, world

9
Typelessness
  • procedure main()
  • x "Example "
  • writes(x)
  • x 1
  • write(x)
  • end

10
Variable Values
  • ICON has both values and variables that have
    values
  • procedure main( )
  • line "Hello, world "
  • write (line)
  • end
  • assigns the string value " Hello, world " to the
    identifier line, which is a variable

11
String Variables
  • May be written literally
  • line "Hello, world"
  • all 256 characters may occur in a string
  • empty string, given by " " contains no characters
    and its length is zero

12
Strings
  • s"ab"
  • ss"cd"
  • write(s)
  • "abcd".
  • s"abc"
  • write(s)
  • s"a"
  • write(s)
  • s""
  • write(s)
  • 3
  • 1
  • 0

13
Strings
  • s"find"
  • write(s3)
  • s4 "e"
  • write(s)
  • will write out
  • n
  • fine

14
Identifiers
  • Must begin with a letter or underscore
  • May be followed with other letters, digits and
    underscore
  • Upper and lowercase are distinct
  • comp Label test10 entry_value
  • There is no declaration for identifier line
  • Scope declarations are possible but optional
  • ICON has no type or storage declarations
  • Correctness of types is checked when operations
    are performed
  • Storage for values in provided automatically

15
Comments
  • The character in a program signals the
    beginning of a comment
  • Both the number sign and the remaining characters
    in the line are ignored
  • End of line terminates comment
  • If appears in quotes, it will be displayed
  • write ("Apt 23 ")

16
read ( )
  • Reads a line
  • write (read( ))
  • reads a line and displays it
  • This will read an expression in your program and
    write it
  • You will need to do some work on the line
  • Try
  • lineread( )
  • write(line)

17
Expression success or failure
  • Many expressions in ICON will either succeed or
    fail
  • If succeeds, produces a value, if fails produces
    no value
  • read( ), failure occurs when end of line or file
    is reached
  • What happens at end of line or end of file?
  • the statement
  • lineread( )
  • failure does not affect the execution of the next
    line so write(line) displays current line

18
Control Structures
  • While
  • while line read( ) do
  • write(line)
  • repeatedly evaluates read( ) in a loop
  • each time read succeeds the value is assigned to
    line and write(line) is evaluated to display that
    value
  • when read( ) fails the assignment operator fails
    and loop terminates
  • success or failure of the expression that follows
    while controls evaluation of the expression that
    follows do
  • while and do are reserved words

19
Control Structures
  • If
  • if count 0 then sign 1 else sign -1
  • else clause is optional
  • What about if-then-else ambiguity?
  • if find("Mr. ", line) then
  • if find("Mrs. ",line)
  • then mmmm1
  • else mrmr1
  • If there is a"dangling else" in nested
    if-then-else expressions, then else is grouped
    with nearest preceding if

    if find("Mr. ",line) then

    if find("Mrs. ",line) then mmmm1
    else mrmr1
  • Control structures in ICON are expressions

20
Control Structures
  • Case statement
  • case expr of
  • case-clause1
  • case-clause2
  • .
  • each case-clause has the form
  • expr1 expr2
  • there is an optional default clause
  • default expr3

21
Case
  • case s of
  • "begin" depthdepth1
  • "end" depthdepth-1
  • expression fail if s is neither "begin" or "end"
  • case i of
  • j1 write("high")
  • j-1 write("low")
  • j write("equal")
  • default write("out of range")
  • displays one of four strings, depending on the
    relative values of i and j

22
find ( ) and match( )
  • find (string1, string2)
  • succeeds if string1 occurs in string2
  • match ( string1, string2)
  • succeeds only if string1 is an initial substring
    that occurs in the beginning of string2
  • find("on", "slow motion") - succeeds
  • find("on", "noise") - fails
  • match("on", "slow motion") - fails
  • match("slo", "slow motion") - succeeds

23
  • If an expression that fails is an argument in
    another expression, the other expression fails
    also, since there is no value for its argument
  • value of find("on", "slow motion") is 10
  • procedure countm(s)
  • procedure produces a count of the lines that
    begin with the value of s
  • count 0
  • while line read( ) do
  • if match(s,line) then count count1
  • return count
  • end
  • return is a reserved word that indicate values to
    be returned from a procedure

24
find( )
  • The call countm("") returns a count of the
    comment lines in an ICON program
  • if i find(s1,s2) then write(i)
  • writes the location of s1 in s2 if s1 is in s2
  • find("i", "mills")
  • returns a value of 2
  • find("l", "mills")
  • returns a value of 3

25
Generators
  • ICON allows expressions to produce more than one
    result
  • these are called generators
  • example
  • every i find("l","mills") do writes(i)
  • displays 3 4

26
Elementary generators
  • every i 1 to 10 do e
  • equivalent of a for loop. The expression 1 to 10
    is a generator that generates the integers 1, 2,
    ..., 10. Each of the values is assigned to
    variable i and
  • the expression e is evaluated.
  • every i 1 to 10 j 1 to 10 do e
  • behaves like two nested for loops.
  • for i 1, j will iterate from one to 10, then
  • for i 2, j will go from 1 to 10, and so on.
  • every i 1 to 10 j 1 to 10 i j do e

27
Control constructs
  • Expression language
  • "Statements" are expressions
  • Expressions are generators
  • Expressions generate sequences of values
  • Expressions generate their sequences of values by
    backtracking

28
Backtracking
  • expressions can succeed or fail
  • backtracking into an expression generates its
    next value
  • backtracking is "cut off" between statements and
    in some other contexts

29
Relational operators
  • a relational operator is a binary operator
  • it fails if the relation does not hold
  • it returns the right operand if it succeeds
  • a than b and b is less than c

30
While expressions
  • while e1 do e2while e1
  • at most one value from each (e1 and e2) per
    iteration
  • e2 may succeed or fail
  • restarts e1 from the beginning
  • fails when e1 fails

31
Example File copy
  • procedure main()while write(read())end

32
Every expressions
  • every e1 do e2every e1
  • generates all values for e1
  • for each value of e1, evaluate e2 generate at
    most one value for e2
  • e2 may succeed or fail
  • fails when e1 fails

33
Why file copycouldnt use every
  • while write(read())
  • read() returns one line from the input
  • read() does not generate another line when backed
    into

34
Example write tableof max. values
  • procedure main()writes(" ") every writes(" ",1
    to 5) write()every i 1 to 5 do
    writes(i) every j 1 to 5 do writes("
    ",max2(i,j)) write()end

35
Example max2(x,y)
  • procedure max2(x,y)x x
  • procedure max2(x,y)x

36
Example table of max.values
  • 1 2 3 4 51 1 2 3 4 52 2 2 3 4 53 3 3 3 4
    54 4 4 4 4 55 5 5 5 5 5

37
If expressions
  • if e1 then e2 else e3if e1 then e2
  • if e1 succeeds, behave like e2 if e1 fails,
    behave like e3
  • do not generate more than onevalue from e1

38
Stack
  • Stack
  • push(Stack,"")
  • apop(Stack)
  • push(Stack,-)
  • aget(Stack)

39
Using case
  • procedure get_priority(Stack)
  • sypop(Stack)
  • push(Stack,sy) get() pops the stack, so return
    value to stack
  • case sy of
  • "" return -1
  • "" return 3
  • "(" return 0
  • "" return 2
  • "/" return 2
  • "" return 1
  • "-" return 1
  • end

40
Blocks
  • while priority incoming do
  • sympop(opStack)
  • writes(sym)
  • priorityget_priority(opStack)

41
Programming Assignment 1
  • Produce an ICON program that converts infix to
    postfix.
  • Assume
  • input is an arithmetic statement
  • a(b-c)
  • output is the statement in postfix form
  • abc-
  • no spaces in statement
  • operands are single characters
  • operators are - / ( )
  • Due 10/5/2001
Write a Comment
User Comments (0)
About PowerShow.com