0% found this document useful (0 votes)
18 views28 pages

Intro to Structured Programming Concepts

The document provides an outline of topics covered in an introductory structured programming class, including hardware/software interfaces, computer languages, syntax, semantics, grammars, compilation, and software categories. It discusses machine language, assembly language, high-level languages, compilers, and running programs. It also briefly covers Python's history, installation, IDLE development environment, basic syntax like variables, naming rules, and assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views28 pages

Intro to Structured Programming Concepts

The document provides an outline of topics covered in an introductory structured programming class, including hardware/software interfaces, computer languages, syntax, semantics, grammars, compilation, and software categories. It discusses machine language, assembly language, high-level languages, compilers, and running programs. It also briefly covers Python's history, installation, IDLE development environment, basic syntax like variables, naming rules, and assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

MIS 4395.

A
Structured Programming
Language
Ahmed Imran Kabir
Week 1, Introductory Class
Outline of Topics
• Hardware/Software interface
– Layers of the Machine
– Kinds of Software
• Computer Languages
• Syntax, Semantics, Grammars
• What happens to your program?
– Compilation, Linking, Execution
– Program errors
• Compilation vs. Interpretation etc.
Software Categories

• System SW
– Programs written for computer systems
• Compilers, operating systems, …

• Application SW
– Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,Preprocessors,
etc.
Operating System, Device Drivers
Machine with all its hardware
Operating System (OS)
 Provides several essential services:
– Loading & running application programs
– Allocating memory & processor time
– Providing input & output facilities
– Managing files of information
Programs
• Programs are written in programming languages
– PL = programming language
– Pieces of the same program can be written in
different PLs
• Languages closer to the machine can be more efficient
• As long as they agree on how to communicate
• A PL is
– A special purpose and limited language
– A set of rules and symbols used to construct a
computer program
– A language used to interact with the computer
Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
• Assembly Language
– Uses mnemonics
– Machine-dependent
– Not usually portable
• High-Level Language (HLL)
– Uses English-like language
– Machine independent
– Portable (but must be compiled for different platforms)
– Examples: Pascal, C, C++, Java, Fortran, . . .
Machine Language
• The representation of a computer program which is
actually read and understood by the computer.
– A program in machine code consists of a sequence of machine
instructions.
• Instructions:
– Machine instructions are in binary code
– Instructions specify operations and memory cells involved in the
operation
Example: Operation Address

0010 0000 0000 0100

0100 0000 0000 0101

0011 0000 0000 0110


Assembly Language
• A symbolic representation of the machine language of a
specific processor.
• Is converted to machine code by an assembler.
• Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
• Programming in assembly language is slow and error-
prone but is more efficient in terms of hardware
performance.
• Mnemonic representation of the instructions and data
• Example:
Load Price
Add Tax
Store Cost
High-level language
• A programming language which use statements
consisting of English-like keywords such as "FOR",
"PRINT" or “IF“, ... etc.
• Each statement corresponds to several machine
language instructions (one-to-many correspondence).
• Much easier to program than in assembly language.
• Data are referenced using descriptive names
• Operations can be described using familiar symbols
• Example:
Cost := Price + Tax
Syntax & Semantics
• Syntax:
– The structure of strings in some language. A
language's syntax is described by a grammar.
– Examples:
• Binary number
<binary_number> = <bit> | <bit> <binary_number>
<bit> =0|1
• Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter> =a|b|...|z
<digit =0|1|...|9
• Semantics:
– The meaning of the language
Syntax & Grammars
• Syntax descriptions for a PL are
themselves written in a formal language.
– E.g. Backus-Naur Form (BNF)
• The formal language is not a PL but it can
be implemented by a compiler to enforce
grammar restrictions.
• Some PLs look more like grammar
descriptions than like instructions.
Compilers & Programs
• Source program
– The form in which a computer program,
written in some formal programming
language, is written by the programmer.
– Can be compiled automatically into object
code or machine code or executed by an
interpreter.
– Pascal source programs have extension
‘.pas’
Compilers & Programs
• Object program
– Output from the compiler
– Equivalent machine language translation of the
source program
– Files usually have extension ‘.obj’

• Executable program
– Output from linker/loader
– Machine language program linked with necessary
libraries & other files
– Files usually have extension ‘.exe’
Running Programs
• Steps that the computer goes through to run a
program:
Memory

Machine language
program
(executable file)
Input Data Data entered CPU
during execution

Computed results
Program Output
Program Execution
• Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
• Syntax Errors:
– Errors in grammar of the language
• Runtime error:
– When there are no syntax errors, but the program
can’t complete execution
• Divide by zero
• Invalid input data
• Logical errors:
– The program completes execution, but delivers
incorrect results
– Incorrect usage of parentheses
Brief History of Python
• Invented in the Netherlands, early 90s by Guido
van Rossum
• Named after Monty Python
• Open sourced from the beginning
• Considered a scripting language, but is much
more
• Scalable, object oriented and functional from the
beginning
• Used by Google from the beginning
• Increasingly popular
Python’s Benevolent Dictator For Life

“Python is an experiment in
how much freedom program-
mers need. Too much
freedom and nobody can read
another's code; too little and
expressive-ness is
endangered.”
- Guido van Rossum
Installing
• Python is pre-installed on most Unix systems,
including Linux and MAC OS X
• The pre-installed version may not be the most
recent one (2.6.2 and 3.1.1 as of Sept 09)
• Download from [Link]
• Python comes with a large library of standard
modules
• There are several options for an IDE
– IDLE – works well with Windows
– Emacs with python-mode or your favorite text editor
– Eclipse with Pydev ([Link]
IDLE Development Environment
• IDLE is an Integrated DeveLopment Environ-ment
for Python, typically used on Windows
• Multi-window text editor with syntax highlighting,
auto-completion, smart indent and other.
• Python shell with syntax highlighting.
• Integrated debugger
with stepping, persis-
tent breakpoints,
and call stack visi-
bility
Python Scripts
• When you call a python program from the
command line the interpreter evaluates each
expression in the file
• Familiar mechanisms are used to provide
command line arguments and/or redirect input
and output
• Python also has mechanisms to allow a python
program to act both as a script and as a module
to be imported and used by another python
program
The Basics
A Code Sample (in IDLE)
x = 34 - 23

z = 3.45

print x
print z
print (x + z)
print (z*x)
Enough to Understand the Code
• Indentation matters to code meaning
– Block structure indicated by indentation
• First assignment to a variable creates it
– Variable types don’t need to be declared.
– Python figures out the variable types on its own.
• Assignment is = and comparison is ==
• For numbers + - * / % are as expected
– Special use of + for string concatenation and % for
string formatting (as in C’s printf)
• Logical operators are words (and, or,
not) not symbols
• The basic printing command is print
Naming Rules
• Names are case sensitive and cannot start with
a number. They can contain letters, numbers,
and underscores.
bob Bob _bob _2_bob_ bob_2 BoB
• There are some reserved words:
and, assert, break, class, continue,
def, del, elif, else, except, exec,
finally, for, from, global, if, import,
in, is, lambda, not, or, pass, print,
raise, return, try, while
Assignment
• You can assign to multiple names at the same
time
>>> x, y = 2, 3
>>> x
2
>>> y
3
This makes it easy to swap values
>>> x, y = y, x
• Assignments can be chained
>>> a = b = x = 2
Accessing Non-Existent Name
Accessing a name before it’s been properly
created (by placing it on the left side of an
assignment), raises an error
>>> y

Traceback (most recent call last):


File "<pyshell#16>", line 1, in -toplevel-
y
NameError: name ‘y' is not defined
>>> y = 3
>>> y
3

Common questions

Powered by AI

Guido van Rossum developed Python with the philosophy of providing programmers with substantial freedom while maintaining code readability and simplicity. This vision manifests in Python's easy-to-read syntax, dynamic typing, and support for functional, object-oriented, and procedural programming paradigms. Python encourages clean and readable code with its use of whitespace and English-like commands, and the extensive standard library supports versatile functionality, reflecting van Rossum's aim for a balance between expressive power and code maintenance .

Backus-Naur Form (BNF) plays a crucial role in defining the syntax of programming languages by providing a formalism for describing the grammar rules. It allows the specification of language syntax in a concise and unambiguous manner, facilitating the development of compilers that can parse and validate source code effectively. BNF helps ensure consistent syntax across different environments, making it simpler to design compilers that accurately interpret languages and decide how to translate them into machine code .

Python's advantages as a scripting language include its ease of learning and use due to its simple syntax, its vast standard library that offers ready-made solutions across diverse domains, and its ability to run on various platforms, including Windows, Linux, and macOS. Limitations may involve execution speed compared to compiled languages and scenarios where Python's dynamic typing could lead to runtime errors not caught at compile time, which might be critical in systems requiring highly performant or strongly typed solutions .

Programmers face the challenge of ensuring that code remains clear and maintainable while allowing flexibility and power for problem-solving. Excessive freedom can lead to diverse coding styles and make it difficult for teams to collaboratively work on projects, causing readability issues. Conversely, overly rigid standards can stifle creativity and may prevent the use of efficient or innovative solutions. Achieving this balance requires guidelines on style and documentation, using tooling for code consistency, and fostering a team culture that values both expressive freedom and shared understanding .

The layered architecture of computer systems differentiates hardware and software into distinct levels: hardware, system software, and application software. System software is responsible for managing hardware resources and providing a platform for application software. It includes operating systems, compilers, and device drivers, which facilitate loading, executing programs, memory management, and input/output operations. Application software, on the other hand, is designed for specific user tasks like word processing and spreadsheets, interacting with system software to utilize hardware capabilities effectively .

The Python programming environment includes components like IDLE (an Integrated Development Environment), which features a multi-window text editor with syntax highlighting, auto-completion, and an integrated debugger. This enhances development by simplifying code writing and testing. Python's extensive standard library and options for using IDEs such as Eclipse with Pydev or Emacs with python-mode further streamline development by offering flexibility and powerful debugging and scripting capabilities .

Machine language consists of binary code that is specific to a processor's architecture, making it non-portable and difficult for humans to interpret. Assembly language provides a symbolic representation of machine code, using mnemonics, but remains machine-dependent and generally non-portable. High-level languages use English-like syntax, making them easier for humans to learn and use, and are generally portable across different hardware platforms through compilation .

Logical errors occur when a program executes without syntax errors or runtime crashes, but the output is incorrect due to flaws in logic or algorithm design. This contrasts with syntax errors, which arise from incorrect grammar and prevent program compilation, and runtime errors, which occur during execution and often cause the program to terminate unexpectedly. Mitigating logical errors involves thorough testing, code reviews, use of debugging tools, and adherence to good coding practices to ensure the program logic fulfills the intended task .

Compiling a high-level language involves translating the entire source code into machine code before execution, creating an object code or executable file. This approach allows for efficient execution but requires compiling each time source code changes. Interpreting executes the source code line by line, translating it into machine code at runtime, which offers flexibility and ease of debugging but may lead to slower execution as it doesn't produce a separate machine code file .

Syntax defines the structure and format of code, ensuring that it can be correctly interpreted by a compiler or interpreter, which makes the language reliably execute instructions. Semantics, on the other hand, ensures that these instructions produce the intended outcome by defining the meaning of valid syntax. Together, they are critical for the reliability and functionality of programming languages, as any errors in syntax will prevent compilation, while semantic errors can cause unexpected behavior or incorrect program output .

You might also like