Search Header Logo
Lecture 1 - part 3 - CIS430

Lecture 1 - part 3 - CIS430

Assessment

Presentation

Computers

University

Medium

Created by

Sherif Abdelhamid

Used 4+ times

FREE Resource

26 Slides • 11 Questions

1

media
media

ISBN 0-321-49362-1

Chapter 1

Preliminaries

2

media

Copyright © 2015 Pearson. All rights reserved.

1-2

Chapter 1 Topics

Reasons for Studying Concepts of
Programming Languages

Programming Domains

Language Evaluation Criteria

Influences on Language Design

Language Categories

Language Design Trade-Offs

Implementation Methods

Programming Environments

3

media

Copyright © 2015 Pearson. All rights reserved.

1-3

Implementation Methods

• Compilation

– Programs are translated into machine language
– Use: Large commercial applications

• Pure Interpretation

– Programs are interpreted by another program known as

an interpreter

– Use: Small programs or when efficiency is not an issue

• Hybrid Implementation Systems

– A compromise between compilers and pure interpreters
– Use: Small and medium systems when efficiency is not the

first concern

4

media

Copyright © 2015 Pearson. All rights reserved.

1-4

Compilation

• Translate high-level program (source language)

into machine code (machine language)

• Slow translation, fast execution

• Compilation process has several phases:

– lexical analysis: converts characters in the source program

into lexical units

– syntax analysis: transforms lexical units into parse trees

which represent the syntactic structure of program

– Semantics analysis: generate intermediate code
– code generation: machine code is generated

5

Multiple Choice

A computer programming language, such as C and C++, that uses a software tool called a compiler to translate the code into binary machine language.

1

Encapsulation

2

Assembly Language

3

Compiled Language

4

Interpreted Language

6

media
media
media

Lexical Analysis

1-5

7

media
media

Copyright © 2015 Pearson. All rights reserved.

1-6

The Compilation Process

Pros
The program is immediately ready
Usually faster
Source code is private and safe

Cons
Not cross-platform (Mac-Windows)
Need to prepare for different CPU/Memory machine
(not flexible)

8

media

Copyright © 2015 Pearson. All rights reserved.

1-6

Code Optimization

Common Subexpression elimination
t1=x*z;
t2=a+b;
t3=p%t2;
t4=x*z;
t5=a-z;
//t4 and t1 is same expression, but evaluated twice by compiler.
// after Optimization


t1=x*z;
t2=a+b;
t3=p%t2;
t5=a-z;

9

media

Copyright © 2015 Pearson. All rights reserved.

1-6

Code Optimization

Constant Folding

//Code segment

int x= 5+7+c;

//Folding applied

int x=12+c;

10

media

Copyright © 2015 Pearson. All rights reserved.

1-6

Code Optimization

Dead Code Elimination
//Code
int x= a+23;
z=a+y;
printf("%d,%d".z,y);
//After Optimization and removing x

z=a+y;
printf("%d,%d".z,y);

11

media

Copyright © 2015 Pearson. All rights reserved.

1-6

Code Optimization

Copy Propagation

//Code segment

a=b;
z=a+x;
x=z-b;

//After Optimization

z=b+x;
x=z-b;

12

media

Copyright © 2015 Pearson. All rights reserved.

1-6

Code Optimization

Loop Optimizations

p=100 ;
for(i=0;i<p;i++)
{
a=b+40;
//loop invariant code
if(p/a==0)
printf("%d",p); }

// After code motion
p=100
a=b+40;
for(i=0;i<p;i++) {
if(p/a==0)
printf("%d",p);
}

13

Multiple Choice

All of these are examples of tokens except___

1

Comment

2

Keyword

3

Operator

4

Identifier

14

Multiple Choice

In this code, int x = 5; 'int' is considered____

1

identifier

2

keyword

3

Operator

4

None of these

15

Multiple Choice

Removing unused code from the program, is considered___

1

Machine dependent code optimization

2

Machine Independent code optimization

16

media
media

Syntax Analysis

1-7

The Java compiler checks if the keywords
(class, public, static, void, int), variable declarations,
assignments, arithmetic operations, and method invocations
are in the correct order and properly nested.
It also verifies that the parentheses, semicolons, and
quotation marks are used correctly.
If there are any syntax errors in the code, the Java compiler
will report them, and the program will not compile until the
errors are fixed.

17

Multiple Choice

Which of the following is a syntax error?

1

Attempting to divide by 0

2

Forgetting a colon at the end of a statement where one is required.

3

Forgetting to divide by 100 when printing a percentage amount.

18

Multiple Choice

The following is an example of what type of error?


print("Here is some text"

print(6/2)

1

Syntax Error

2

Run-time Error

3

Semantic Error

19

Multiple Choice

Program is translated line by line as the program is running

1

Assembler

2

Compiler

3

Interpreter

20

Multiple Choice

This phase scans the source code as a stream of characters and converts it into meaningful lexemes.

1

Lexical Analyzer

2

Syntax Analyzer

3

Semantic Analyzer

4

Code Generator

21

media

Abstract Syntax Tree - AST

1-8

ClassDeclaration (SyntaxExample)
├── MethodDeclaration (main)
│ ├── Modifier (public)
│ ├── Type (void)
│ ├── Identifier (main)
│ ├── FormalParameterList
│ │ └── FormalParameter (String[] args)
│ └── Block
│ └── Statement
│ ├── VariableDeclaration (int x = 5)
│ ├── VariableDeclaration (int y = 10)
│ ├── VariableDeclaration (int sum = x + y)
│ └── ExpressionStatement (System.out.println("The sum is: " + sum))
└── PrimitiveType (int)

22

media
media

Abstract Syntax Tree - AST

1-9

23

media

Semantic Analysis

1-10

Type Checking:
Ensure that expressions, variables, and operations are used correctly
based on their types. It verifies that compatible types are used together
and reports any type mismatches.

Scope and Declaration Resolution:
Ensures that variables are declared before they are used and that they
are accessible within their respective scopes.

Control Flow Analysis:
It ensures that the control flow is well-formed and that there are no
unreachable statements or potential infinite loops.

24

media

Copyright © 2015 Pearson. All rights reserved.

1-11

Pure Interpretation

• No translation
• Easier implementation of programs (run-time

errors can easily and immediately be displayed)

• Slower execution (10 to 100 times slower than

compiled programs)

• Often requires more space
• Now rare for traditional high-level languages
• Significant comeback with some Web scripting

languages (e.g., JavaScript, PHP)

25

Multiple Choice

An infinite loop is an example of what type of error?

1

Syntax Error

2

Run-time Error

3

Semantic Error

26

media
media

Copyright © 2015 Pearson. All rights reserved.

1-12

Pure Interpretation Process

Pros
Cross-platform and portable
Easier to test because no compiled step
When things go wrong, easier to debug since having
access to the code

Cons
Need interpreter on the user machine
At least ten times slower than compiled languages
Source code is public

27

media

Copyright © 2015 Pearson. All rights reserved.

1-13

Hybrid Implementation Systems

• A compromise between compilers and pure

interpreters

• A high-level language program is translated

to an intermediate language that allows
easy interpretation

• Faster than pure interpretation

28

media
media

Copyright © 2015 Pearson. All rights reserved.

1-14

Hybrid Implementation Process

29

media

Examples

Compiled Language

C, C++, and Objective-C

Interpreted Language

PHP, Ruby, and Javascript

Hybrid Language

Java, C#, Perl, and Python

1-15

30

media

Generations of programming language

1-15

media

​Machine Code Assembly Java/Python/C SQL Prolog

31

Multiple Choice

C is a ___________generation programming language.

1

Third

2

First

3

Second

4

Fourth

32

Multiple Choice

  • Language the consists of binary codes (0s and 1s)___

1

Assembly

2

Machine Language

3

3G

4

4G

33

media

Copyright © 2015 Pearson. All rights reserved.

1-16

Programming Environments

• A collection of tools used in software development
• Microsoft Visual Studio.NET

– A large, complex visual environment

Used to build Web applications and non-Web applications in
any .NET language

• NetBeans

– Related to Visual Studio .NET, except for applications in

Java

• PyCharm

– Used to build Python applications

34

media

Copyright © 2015 Pearson. All rights reserved.

1-17

Programming Environments

• Online Environments

https://www.onlinegdb.com/

35

media
media

Copyright © 2015 Pearson. All rights reserved.

1-18

Programming Environments

• Online Environments

https://repl.it/

36

media
media

Programming Environments

• Online Environments
https://swish.swi-prolog.org

1-19

37

media

Copyright © 2015 Pearson. All rights reserved.

1-20

Summary

• The study of programming languages is valuable for

a number of reasons:
– Increase our capacity to use different constructs
– Enable us to choose languages more intelligently
– Makes learning new languages easier

• Most important criteria for evaluating programming

languages include:
– Readability, writability, reliability, cost

• Major influences on language design have been

machine architecture and software development
methodologies

• The major methods of implementing programming

languages are: compilation, pure interpretation, and
hybrid implementation

media
media

ISBN 0-321-49362-1

Chapter 1

Preliminaries

Show answer

Auto Play

Slide 1 / 37

SLIDE