jimin-kiim / Programming-Language-and-Compiler

formal languages, automata, concepts of programming languages
0 stars 0 forks source link

Describing Syntax and Semantics #2

Closed jimin-kiim closed 1 year ago

jimin-kiim commented 1 year ago
jimin-kiim commented 1 year ago

Introduction

$Syntax$ : while(bool_expr) statement $Semantics$ : "when the current value of the Boolean expression is true, the embedded statement is executed. If the Boolean expression if false, control transfers to the statement following the while construct"

jimin-kiim commented 1 year ago

the General Problem of Describing Syntax

sentence index = 2 * count + 17;

lexemes index, =, 2, *, count, +, 17, ;

tokens - lexemes identifier - index, count int_literal - 2, 17 equal_sign - = mult_op - * plus_op - + semicolon - ;

jimin-kiim commented 1 year ago

Formal Methods of Describing Syntax(1)

BNF(Backus-Naus Form)



-> begin end -> | ; -> = -> a|b|c|d -> + | - -> | const ``` ``` Derivation a = b + const => begin end => begin end => begin = end => begin a = end => begin a = + end => begin a = b + end => begin a = b + const end ``` - ambiguity in grammar - grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees - if we use the parse tree to indicate precedence levels of the operators, there will be no ambiguity. The higher precedence operator should be lower in the parse tree - associativity of operators : when an expression includes two operators that have the same precedence, a semantic rule is required to specify which should have precedence. ``` Ambiguous -> if () then | if () then else ``` ``` Unambiguous -> | -> a non-if statement | if () then else -> if () then | if () then else There is just one possible parse tree, using this grammar, for the following sentential form; if () if () else ```
jimin-kiim commented 1 year ago

Formal Methods of Describing Syntax(2)

EBNF(Extended BNF)

for

→ if () | if () else ``` ``` {, } ``` ``` (* | / | %) for * | / | % ``` - recent variations in EBNF - a colon is used instead of the arrow - the RHS is placed on the next line - in the place of square brackets to indicate something being optional, the subscript 'opt' is used. - rather than using the | symbol in a parenthesized list of elements to indicate a choice, the word "oneof" are used.
jimin-kiim commented 1 year ago

Semantics

Static Semantics and Attribute Grammars

Static Semantics

Semantics

.expected_type <- .actual_type ``` ``` -> + | Syntax -> Semantics .actual_type <- .actual_type Syntax -> [1] + [2] Semantics .actual_type <- [1].actual_type predicate [1].actual_type == [2].actual_tye .expected_type == .actual_type ``` ``` Syntax -> A | B | C Semantics .actual_type <- look-up(.string) ``` actual_ type : a synthesized attribute related to \ and \ expected_type : an inherited attribute related to \ computing attributing values - if all attributes were inherited, the tree could be decorated in top-down order. - if all attributes were synthesized, the tree could be decorated in bottom-up order. - in many cases, both kinds of attributes are used, and it's some combination of top-down and bottom-up that must be used.
jimin-kiim commented 1 year ago

Dynamic Semantics

Operational Semantics

스크린샷 2022-10-18 오후 8 19 52

a = b + 1 (a > 1) one possible precondition : b > 10 weakest precondition : b > 0

sum = 2 * x + 1 (sum > 1) one possible precondition : x > 10 weakest precondition : x > 0

스크린샷 2022-10-18 오후 7 55 23

weakest precondition P is defined by the axiom which means that P is computed as Q with all instances of x replaced by E.

Sequences

Selection

Logical Prestest Loops

Axioms