2.0 Declarative computation model Flashcards
What is a computation model?
formal system that defines a language and how expressions and statements of the language are executed by an abstract machine.
What is a programming model?
Programming techniques and principles used to write programs in the language of the computation model.
What is declarative programming?
Stateless programming
Evaluating functions over partial data structures.
Programming with functions, complete values, deterministic logic.
Can be made concurrent.
What is imperative programming?
Stateful programming
What is language syntax?
What are legal programs, programs that can be executed.
What is language grammars?
Rules on how to make statements (‘sentences’) out of tokens (‘words’).
What is a statement?
Sequence of tokens
What is a token?
Sequence of characters
What is a tokenizer?
A lexical analyser.
Program that takes a sequence of characters and returns a sequence of tokens.
What is a parser?
A program that takes a sequence of tokens and returns a parse tree.
What is a parse tree?
Shows the structure of statements.
What is the Extended Backus-Naur form (EBNF)?
Notation for defining grammar.
Uses terminal- and non-terminal symbols.
What is a terminal symbol?
Tokens
What is a non-terminal symbol?
Sequence of tokens.
Defined by a grammar rule, that show how to expand it into tokens.
Give an example of representing the nonterminal <digit> in EBNF</digit>
<digit> ::= 1|2|3|4|5|6|7|8|9|0
The nonterminal <digit> can be any of the ten tokens.
|: means or
</digit></digit>
How can an integer be represented using EBNF?
<int> ::= <digit> {<digit>}
An integer is a digit followed by any number of digits.
<digit>: Nonterminal
{...}: Means repeat whatever is inside, any number of times, including none.
</digit></digit></digit></int>
What is a formal language?
Set of all possible statements generated by a grammar.
What are context free grammars?
The expansion of a nonterminal is always the same no matter where it is used.
Nonterminals can only use already-declared variables.
For example, context free grammars does not allow using variables that is declared before they are used - this is a context dependency.
Can be ambiguous: There can be several parse trees corresponding to a given token sequence. This can cause different results depending on how an expression is evaluated.
Example: 23+4 can give trees representing (23) + 4, and 2 * (3+4)
What is context-sensitive grammar?
Grammar containing nonterminals whose use depends on the context where it is used.
How are the grammars of most practival programming languages defined?
Two parts, as a context free grammar, supplemented with a set of extra conditions composed by the language.
What does it mean to disambiguate a grammar?
Add extra conditions to grammars, that restrict the parser so that only one parse tree is possible.
What is a precedence condition?
A condition for expressions with multiple operators, e.g. (2+3*4).
Each operator is given a precedence level. The higher the level, the deeper into the parse tree they are put (away from the root).
When an operator is deeper in the pipeline than another, we say it binds tighter than the other operator.
What is a Associativity condition?
A condition for expressions with multiples of the same operators, e.g. (2-3-4).
Because all the operator would get the same precedence level, we add an associativity condition to be able to disambiguate them.
An associativity condition defines whether the leftmost or the rightmost operator binds tighter.
(2-3-4)
left: (2-3) - 4
right: 2- (3-4)
How can partial definitions of nonterminals be written?
Using ‘…’
<expression> ::= <variable> | <int> | ...
</int></variable></expression>