Saturday 21 February 2015

3.0, Semantics and semantics

Semantics is the meaning of expressions, statements, and program units. (i,e this is an if statement)


Syntax is the form of its expressions, statements, and program units.  (ie. the if statement syntax is different in C# and Vb.net)

If(<boolean_expre> ) { Statement ; }

If (<boolean_expre> ) Then Statement End If

------------


index = 2 * count = 12


Lexeme - Small units that don't offer a description (A basic unit of meaning) (programming units)
Token = A category of lexemes

Lexemes       Token
Index             indentfier

=                    eqaul_sign
2                    int_literal
*                    mult_op
Count            identifier+                   plus_op
17                 int_literal
;                    semicolon or end_of_statement
   
---------

Backus naur form (BNF) is a metalanguage (language to describe a language) for programming languages

ie. a BNF description of a if and  a if else statement would be 

<if_stmt> -> if <logic_expr> then <stmt>
                 |  
if <logic_expr> then <stmt>else <stmt>


The
<if_stmt> symbol on the left hand side (LHS) of the  -> arrow, is the abstraction being defined.

The text to the right of the arrow (RHS) is the definition of the LHS. it consists of of some mixture of tokens lexemes and references to other abstractions.

Multiple definitions can be written as a single rule, with different definition separated by the    |     symbol


When describing lists for example a variable list of identifiers
It is common to use recursion
A rule is recursive if the LHS appears in its RHS

<indent_list> => identifier
                          | identifier, <indent_list>


--------

Operator Precedence

A = B + C * D 

When an expression includes two different operations for example in the above * and +,
 one obvious semantic issue is the order of evaluation of the two operators (is it add then multiply or is it visa versa in the above express. This semantic question can be answer by assignment different precedence levels to operators. If * is higher in precedence, then it will be the one used first.

Associativity of Operators

When an expression includes two operators that have the same precedence (as * and / usually do), a semantic rule is required to specify which should have precedence. The rule is named associativity.


A = A / B * C



Usually the correct order if left associative (the direction we read) so for this example in we would do the division first.


But it can also in times be right associative, to indicate right associativity right recursion can be used. (LSH right of the RHS) ie.

<factor> => <exp> ** <factor>



Parse Trees

 Parsing is the problem of transforming a linear sequence of characters into a syntax tree
 
Read from left most bottom most



Grammar example1 IPL
 
 





No comments:

Post a Comment