Top Banner
Programowanie obiektowe dr inż. Marcin Pietroo
48

Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Aug 31, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programowanie obiektowe

dr inż. Marcin Pietroo

Page 2: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

History of programming languages

Page 3: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Języki programowania

TIOBE LIST: 1. C 2. C++ 3. Objective-C 4. Java 5. C# 6. Visual Basic 7. PHP 8. Python 9. JavaScript 10.Visual Basic .NET 11. Ruby 12. Transact-SQL 13. Perl 14. F# 15. Język asemblera 16. Lisp 17. PL/SQL 18. MATLAB 19. Delphi 20. Object Pascal

Page 4: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages

Page 5: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages

• set of syntax rules

• semantic

Choosing language for implementation:

• efficiency

• available libraries

• experience

• domain of implementation

Page 6: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages

• Newer languages with higher abstraction

• old languages were low level languages strictly based on CPU architecture

• portability and abstraction shorten time of software development, error risk (very often worse efficiency)

Page 7: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Elements of programming lanaguages

• syntax

• semantic

• data types

• standard libraries

Page 8: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages – syntax and semantic

• Lexems (token) definitions

• Syntax rules

Syntax defined by regular expressions and BNF notation

Page 9: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Gramar definition

BNF notation is a production rules set:

• <symbol> ::= <expression with symbols>

Semantic of used symbols is as follows:

• < – left boundary of symbol

• > – right boundary of symbol

• ::= – is defined as

• | – or

Above symbols are meta language symbols.

Page 10: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

BNF – liczba naturalna

• <zero>::= 0

• <non zero numbers>::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

• <digit>::= <zero> | <non zero number>

• <digits> ::= <digit> | <digit><digits>

• <positive integer number>::= <digit> | <non zero digit><digits>

Page 11: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

BNF – C language

conditional_expression : logical_or_expression ('?'^ expression ':' conditional_expression)? ; logical_or_expression : a=logical_and_expression ('||'^ b=logical_and_expression)*; logical_and_expression : inclusive_or_expression ('&&'^ inclusive_or_expression)* ; inclusive_or_expression : exclusive_or_expression ('|'^ exclusive_or_expression)* ;

Page 12: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

BNF – C language

compound_statement : '{' declaration* statement_list? '}'; statement_list : statement+ -> ^(STMT statement+); FLOATING_POINT_LITERAL : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix? | '.' ('0'..'9')+ Exponent? FloatTypeSuffix? | ('0'..'9')+ Exponent FloatTypeSuffix? | ('0'..'9')+ Exponent? FloatTypeSuffix ;

Page 13: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages – data types

• integers

• floating points (float, double)

• text data (char)

Page 14: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Standard libraries

• input/output functions

• files system handling

• multithreading

• operating memory handling

• basic data types and procedures to processing them

• text data structures

Page 15: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Types of errors

• lexical error

• syntax error

• casting error

• semantic error

• execution error

• logic error

Page 16: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Errors

• foor (int i = 0; i < 100; i++) - lexical

• x+1 = x - syntax

• for (int i = 0, i < 100, i++) - syntax

• main() {i = 0;} - semantic

Page 17: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Programming languages processing

• compilation – source code is translated to machine code. Compiled programming languages

• interpretation – source code is translated online and executed by special program called interpreter. Interpretated programming languages

Page 18: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Classification of programming languages

• programming paradigms (imperative, functional, declarative)

• type of generating machine code • control of data types compatibility (e.g. dynamic

typed) • type of execution (compiled, interpretated) • level of abstraction (low or high level) • purpose (database modification, markup

languages, bash languages, hardware languages etc.)

Page 19: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Compilation - lexer

• The process of converting a sequence of characters (such as a computer program or web page) into a sequence of tokens

• A program that performs lexical analysis may called lexer, tokenizer, or scanner ("scanner" is also used to refer to the first stage of a lexer).

Page 20: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Compilation - lexer

• lexer is generally combined with a parser, which together analyze the syntax of programming languages, web pages.

• Regular expressions (identify tokens)

• finite automata (finite state machine) with state table

Page 21: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Lexer

• sum = i + 8;

Lexem Token category

sum "Identifier"

= "Assignment"

i „identifier"

+ "Addition"

8 "Integer literal"

; "End of statement"

Page 22: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Compilation - parser

• Parsing or syntactic analysis is the process of analysing a string of symbols, either in natural language or in computer languages, conforming to the rules of a formal grammar.

• Parser get lexer output as an input

• Generate Abstract Syntax Tree (AST)

Page 23: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Parser

• Top-down parsing - top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right.

• Bottom-up parsing - a parser can start with the input and attempt to rewrite it to the start symbol. Intuitively, the parser attempts to locate the most basic elements, then the elements containing these, and so on. LR parsers are examples of bottom-up parsers. Another term used for this type of parser is Shift-Reduce parsing.

Page 24: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Proces kompilacji

• lexer –> tokens –> syntactic analysis –>

• parse tree (AST, intermadiate representation) –> compiler, interpreter, translator ->

• other language, virtual machine ->

• machine code etc.

Page 25: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Abstract Syntax Tree

FUNC_DEF

DECL DECL

FUNC_BODY

COMPOUND_STMT

EXPR EXPR EXPR

EXPR EXPR

EXPR

EXPR

COMPOUND_STMT

i = 6

Time_START Time_END

Page 26: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Compilation – tools for lexer and parser generating

• Yacc

• ANTLR

• SUIF

• Flex

• Ragel

Page 27: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

C language

• variables

• basic data types (int, float, double, char etc.)

• arrays (static and dynamic)

• structures

Page 28: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

C language

• pointers (int*, double* etc.)

• references (int & etc.)

Page 29: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

C language

• functions (parameters by value and addresses, local variables, list of parameters etc.)

• conditional instructions (if etc.)

• loops (for, while etc.)

Page 30: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

C language

Memory allocation:

- malloc, realloc …

- free

Page 31: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Struktury danych

• preprocesor

• define (literal values, macros)

• include

• ifdef

• …

Page 32: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Data structures

• List

• Vektor

• Hashmap

• Stack

• Set

Page 33: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Data structures

• Binary trees

• Red black trees

• Heap

Page 34: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

List implementation

• How list can be implemented in C?

Page 35: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

List

struct element { int value; struct element *next; }; struct element* head = malloc(sizeof(struct element)); head->value = 1; head->next = NULL;

Page 36: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Lista

struct element* addLastElement(struct element* tail, int val) { struct element* new_tail = malloc(sizeof(struct element)); new_tail->value = val; new_tail->next = NULL; tail->next = new_tail; return new_tail; }

struct element* addFirstElement(struct element* head, int val) { struct element* new_head = malloc(sizeof(struct element)); new_head->value = val; new_head->next = head; return new_head; }

Page 37: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Lista

struct element* removeFirstElement(struct element* head) { struct element* new_head = head->next; free(head); return new_head; }

void print_list(struct element * head) { struct element * current = head;

while (current != NULL) { printf("%d\n", current->value); current = current->next; } }

Page 38: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

List

struct element* removeElement(struct element* head, int nr) { struct element * current = head; struct element * previous = head; int count = 0;

while (current != NULL) { count++; if (count == nr) { current = current->next; previous->next = current; } previous = current; } }

Page 39: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Task

• Bidirectional list

Page 40: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Data structures

• Graph

Task – tree implementation

Page 41: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Data structures

• Tree traverse

• Methods of tree traverse?

Page 42: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Tree traverse

PRE-ORDER(node_v) { print node_v.value jeżeli node_v.left_child != null to PRE-ORDER(node_v.left_child) jeżeli node_v.right_child != null to PRE-ORDER(node_v.right_child) } IN-ORDER(node_v) { jeżeli node_v.left_child != null to IN-ORDER(node_v.left_child) wypisz node_v.value jeżeli node_v.right_child != null to IN-ORDER(node_v.right_child) } POST-ORDER(node_v) { jeżeli node_v.left_child != null to POST-ORDER(node_v.left_child) jeżeli node_v.right_child != null to POST-ORDER(node_v.right_child) wypisz node_v.value }

Page 43: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Tree traverse

• Depth first search

• Width first search

Page 44: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Basic algorithms

- Sorting

- Text algorithms

- Tree and graph algorithms

Page 45: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Algorithms - sorting

• Insertion

• Bubble

• Quick sort

• Merge sort

• Counting sort

• Heap sort

Page 46: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Algorithms

• Time complexity

• Memory complexity

Page 47: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Text algorithms

• KMP

• Naive

• Boyer Moore

• Aho-Corasick

Page 48: Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages . Programming languages •set of syntax rules •semantic Choosing language for implementation:

Text algorithms

• Regular expressions

• Network intrusion

• Databases