0% found this document useful (0 votes)
154 views3 pages

Lab 06 - Python Plus Syntax and Semantics (Answers) PDF

This document provides an overview of Lab 6 for the ITECH5403 Comparative Programming Languages course. The lab focuses on Python syntax, semantics, and the Python programming language. It includes questions to review syntax and semantics, and directions on installing Python and going through Python tutorials to learn the basics of Python programming.

Uploaded by

li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
154 views3 pages

Lab 06 - Python Plus Syntax and Semantics (Answers) PDF

This document provides an overview of Lab 6 for the ITECH5403 Comparative Programming Languages course. The lab focuses on Python syntax, semantics, and the Python programming language. It includes questions to review syntax and semantics, and directions on installing Python and going through Python tutorials to learn the basics of Python programming.

Uploaded by

li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

ITECH5403 Comparative Programming Languages

School of Science, Engineering and Information Technology

Lab 6 – Python + Syntax & Semantics


Introduction

As this is an 'even' week we'll be investigating the Python programming language (which you will need to have
a basic knowledge of to complete your second assignment), as well as doing some review questions and
problems and the topic of syntax and semantics. Remember – in these labs you aren't expected to know every
single answer from memory – sometimes it's best to do a little research / reading / re-reading of materials and
come up with a great, and correct, answer rather than just 'taking a stab' at the question and hoping you're
right!

Python Investigation

Python comes installed on the university machines and can be found in the start menu – specifically, you will
be looking for the IDLE Python integrated debugging environment:

You will likely also want to install Python on your personal machine.
Python is freely available from: https://www.python.org/downloads/

Be sure to install a version of Python which is at least 3.x (i.e. 3-point-


something) – older 2-point-something versions of Python have slightly
different syntax and are not interchangeable (that is, Python 2 code will not work in a Python 3 interpreter and
vice versa). Once you have a version of Python to work with, you need to learn some Python. To do this, you
can either:

- Go through the official Python tutorial from section 1 though to and including section 5
(https://docs.python.org/3/tutorial/index.html),

Or:

- Go watch a series of Python tutorial videos, for example this series up to tutorial video 16 or 17:
https://www.youtube.com/playlist?list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_

You don't have to dwell on everything in the Python tutorial / videos – but you should use them as a "quick-
start" guide to get up to speed as you will need the ability to perform basic Python programming for the second
assignment in this course!

CRICOS Provider No. 00103D Page 1 of 3


ITECH5403 Comparative Programming Languages

School of Science, Engineering and Information Technology

Review Questions

1. Define syntax and semantics.[Q1]

Syntax is the form of a programming language’s expressions, statements, and program units. A program must be
presented for interpretation or compilation in a syntactically correct form in order to be considered valid / legal – which
is required for the program to operate.

Semantics is the meaning of expressions, statements and program units. Semantics considers the meaning of syntactically
legal strings defined by a specific programming language, showing the computation involved. Semantics describes the
processes a computer follows when executing a program in that specific language.

2. Describe the operation of a general language generator. [Q3]

A language generator is a device that can be used to generate sentences in a language. The generator uses the grammar
of the programming languages to create these valid sentences. A language generator can be used to provide examples of
valid statements in a given programming language.

3. Describe the operation of a general language recogniser. [Q4]

A general language recogniser works from the other end of the scale to a language generator. A language recogniser
takes the sentences of a sentence in a programming language and uses the grammar rules of the language to determine
whether or not the sentence is valid / legal. This is an essential part of a compiler or interpreter.

The language recogniser may work with context-free grammars or regular grammars – and use the BNF (Backus-Naur
Form) grammar rules to determine the validity of any statement.

4. Explain what static semantics and dynamic semantics are, and explain how they differ. [Q8-Mod]

The static semantics of a language defines restrictions on the structure of valid texts that are hard or impossible to
express in standard syntactic forms. For compiled languages, static semantics essentially include those semantic rules
that can be checked at compile time. Examples include checking that every identifier is declared before it is used (in
languages that require such declarations) or that the labels on the arms of a case statement are distinct.Many important
restrictions of this type, like checking that identifiers are used in the appropriate context (e.g. not adding an integer to a
function name), or that subroutine calls have the appropriate number and type of arguments, can be enforced by defining
them as rules in a logic called a type system. Other forms of static analyses like data flow analysis may also be part of
static semantics. Static semantics is so named because the analysis required to check these specifications can be done at
compile time.

The dynamic semantics (also known as execution semantics) of a language defines how and when the various constructs
of a language should produce a specific program behaviour. There are many ways of defining execution semantics.
Natural language is often used to specify the execution semantics of languages commonly used in practice. A significant
amount of academic research went into formal semantics of programming languages, which allow execution semantics to
be specified in a formal manner. Results from this field of research have seen limited application to programming
language design and implementation outside academia.

Static semantics is more concerning about legal form of program, while dynamic semantics is related to meaning of
expressions, statements and program units.

CRICOS Provider No. 00103D Page 2 of 3


ITECH5403 Comparative Programming Languages

School of Science, Engineering and Information Technology

5. What is an attribute grammar, and what is their primary use? [Q12-Mod]

Attribute grammars are an approach to both describing and checking the correctness of the static semantics rules of a
program. Although they are not always used in a formal way in compiler design, the basic concepts of attribute
grammars are a least informally used in every compiler.

Attribute grammars can be used by compilers to check the validity of statements during the compilation process by
following the grammar rules of the language and ensuring that each program statement adheres to the valid, legal
syntax.

Attribute grammars are a type of context-free grammars to which have been added:

- Attributes, which are associated with grammar symbols (the terminal and nonterminal symbols), are similar to
variables in the sense that they can have values assigned to them.
- Attribute computation functions (sometimes called semantic functions) which are associated with grammar rules.
They are used to specify how attribute values can be computed.
- Predicate functions which state the static semantic rules of the language, and are associated with grammar rules.

6. Explain the purpose of predicates in attribute grammars. [Q9-Mod]

Predicates (or assertions as they are also known) are used to state the static semantic rules of a language. An assertion
immediately before a program statement describes the constraints on the program variables at that point in the program.

An assertion immediately following a statement describes the new constraints on those variables (and possibly others)
after execution of the statement. These assertions are called the precondition and postcondition, respectively, of the
statement. For two adjacent statements, the postcondition of the first statement serves as the precondition of the second
statement. Developing an axiomatic description or proof of a given program requires that every statement in the program
has both a precondition and a postcondition.

For example, in the statement and postcondition: sum = 2 * x + 1 {sum > 1}

{x > 10}, {x > 50} and {x > 1000} are all valid preconditions, and the weakest precondition is {x > 0}

If the weakest precondition can be computed from the most general postcondition of each of the statement types of a
language, then the processes used to compute the preconditions provide a concise description of the semantics of that
language. Furthermore, correctness proofs can be constructed for programs in that language.

7. With regard to attribute grammars, discuss the role of synthesized and inherited attributes. [Q10-
Mod]

An attribute grammar is a grammar with the following additional features. Associated with each grammar symbol X is a
set of attributes A(X). The set A(X) consists of two disjoint sets S(X) and I(X), called synthesized and inherited attributes.

- Synthesized attributes are used to pass semantic information up a parse tree, while
- Inherited attributes pass semantic information down and across a tree.

Problem Set

There are no problem set questions this week.

CRICOS Provider No. 00103D Page 3 of 3

You might also like