PYJ and JULIA in Python

Julia is an interesting language designed to do fast numerical computations while allowing the syntactic and modular flexibility of Python. The PyJ language is used specifically in this chapter, and it is a much-abbreviated version of Julia with a correspondingly simpler syntax. It is a programming language and can be used for calculations. PyJ produces C code, which can be compiled and executed.

Here is an example PyJ program:

function sqr (a)

return a*a

end

b = 2

while b < 20

b = read()

println( sqr(b) )

b = b + 2

end

println()

Most programmers could puzzle out the syntax quickly and figure out what the program should do. That’s not good enough to build a parser, though. A PyJ parser takes the input text and ends up at the “<program>” symbol. That’s where the grammar begins:

<program> ::= { <function> } (<statement>)

which says that a program is a collection of function definitions followed by a collection of statements.

<statement> ::= <ifstatement> | <whilestatement> | <forstatement> | <assignmentstatement> |

<callstatement> | <printstatement> | <printlnstatement> | <returnstatement> |

<breakstatement> | <readstatement>

This lists all of the statement types in the language, of which there are ten. There are no declarations, because, like Python and Julia both, PyJ defines vari­ables when they are used.

The statements are best illustrated by example first. An if statement is

if a<6

k = k + 1

println(k)

end

All if statements have a block of statements followed by an end. Options include elseif and else parts:

if a<6

k = k + 1

println(k)

elseif a>6

k = k – 1

println(k)

else

k = 0

a = 0

end

The indentation does not matter. A while statement is simple:

while a < b

a = a + 1

end

A while followed by an expression followed by some statements followed by end. A for statement is as follows:

for i= 1:5

print (i)

println(i*i)

end

Here, the variable i takes on the values 1,2,3,4,5 in sequence. The for loop variable i is not available outside the loop. Assignment statements have already been used, but can assign any expression using standard operators and functions:

a = 3*5*(pi-3)/6

All variables are floating point and are defined when first used. If the user defines a function sqr, then it can be used in an expression, too:

dist = sqr(a-b)

The print and println statements print a value; println prints an end of line at the end, and print does not. The read statement prompts the user for input of a collection of variables:

read (x, peanut, zz)

All will be floating point numbers.

There is more to the syntax, but we have enough to begin. A systematic ap­proach is important. When parsing any set of data, the process is first, to break up the input into symbols based on the original character data, and then to collect symbols into sets sequences that correspond to the language features.

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *