Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages ....
Transcript of Programowanie obiektowe - fpga.agh.edu.pl fileDelphi 20. Object Pascal . Programming languages ....
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
Programming languages
• set of syntax rules
• semantic
Choosing language for implementation:
• efficiency
• available libraries
• experience
• domain of 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)
Programming languages – syntax and semantic
• Lexems (token) definitions
• Syntax rules
Syntax defined by regular expressions and BNF notation
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.
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>
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)* ;
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 ;
Standard libraries
• input/output functions
• files system handling
• multithreading
• operating memory handling
• basic data types and procedures to processing them
• text data structures
Types of errors
• lexical error
• syntax error
• casting error
• semantic error
• execution error
• logic error
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
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
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.)
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).
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
Lexer
• sum = i + 8;
Lexem Token category
sum "Identifier"
= "Assignment"
i „identifier"
+ "Addition"
8 "Integer literal"
; "End of statement"
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)
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.
Proces kompilacji
• lexer –> tokens –> syntactic analysis –>
• parse tree (AST, intermadiate representation) –> compiler, interpreter, translator ->
• other language, virtual machine ->
• machine code etc.
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
C language
• variables
• basic data types (int, float, double, char etc.)
• arrays (static and dynamic)
• structures
C language
• functions (parameters by value and addresses, local variables, list of parameters etc.)
• conditional instructions (if etc.)
• loops (for, while etc.)
List
struct element { int value; struct element *next; }; struct element* head = malloc(sizeof(struct element)); head->value = 1; head->next = NULL;
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; }
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; } }
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; } }
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 }