Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing and Engineering, University of Huddersfield CHA2545: WEEK 14 LECTURE: ABSTRACT SYNTAX SIMPLE EXAMPLE OF SEMANTICS DEFINITION TUTORIAL/PRACTICAL:

Similar presentations


Presentation on theme: "School of Computing and Engineering, University of Huddersfield CHA2545: WEEK 14 LECTURE: ABSTRACT SYNTAX SIMPLE EXAMPLE OF SEMANTICS DEFINITION TUTORIAL/PRACTICAL:"— Presentation transcript:

1 School of Computing and Engineering, University of Huddersfield CHA2545: WEEK 14 LECTURE: ABSTRACT SYNTAX SIMPLE EXAMPLE OF SEMANTICS DEFINITION TUTORIAL/PRACTICAL: - Last weeks exercise 5 - Haskell/Gofer revision..follow WEB NOTES/EXERCISES NB supporting notes for all these topics in web page

2 School of Computing and Engineering, University of Huddersfield ABSTRACT SYNTAX n concrete syntax: to write programs we need to know exactly how statements are formed and their exact written notation. n abstract syntax: to gain an understanding of the general structure of a language we only need to know how the components relate.

3 School of Computing and Engineering, University of Huddersfield ABSTRACT SYNTAX : assignment example JAVA / C: X = X + 1; or X++; BASIC: LET X = X + 1 COBOL: ADD 1 TO X GIVING X PASCAL: X := X + 1 HASKELL: NOT ALLOWED !! INCREMENT X

4 School of Computing and Engineering, University of Huddersfield ABSTRACT SYNTAX - DEFns: We tend to use abstract “algebraic” notation to abstract away from particular syntax… bit like BNF without the concrete bits: :  ABSTRACT SYNTAX: ( (“Y”), ( (“X”), (“1”)))

5 School of Computing and Engineering, University of Huddersfield ABSTRACT SYNTAX - Function heading example Bool greater_than(Int x, Int y) (JAVA-like) greater_than(x : integer; y : integer) : integer (PASCAL-like) ABSTRACT constructor: :  Haskell a bit different: greater_than: integer integer --> integer greater_than(x,y) =...

6 School of Computing and Engineering, University of Huddersfield ABSTRACT SYNTAX RELATED WORKS: - -a compiler starts compiling by turning characters in a program into TOKENS, then turning the token list into an ABSTRACT SYNTAX TREE. - -in Java and Haskell we can use CONSTRUCTORS to construct instances of data types – these are like abstract syntax definitions

7 School of Computing and Engineering, University of Huddersfield DENOTATIONAL SEMANTICS OF BINARY NUMERALS In a denotational defn, we MAP abstract syntax classes to meanings (explicit objects) in a well known language! e.g. binary, octal, roman numeral => base 10 natural numbers

8 School of Computing and Engineering, University of Huddersfield BINARY NUMERALS: ABSTRACT SYNTAX 1 :  BinDidgit 0 :  BinDidgit D: BinDigit  BinNum N: BinNum BinDigit  BinNum NB: alternative notation: BinDigit => 1 | 0 BinNum => BinDigit | BinNum; BinDigit

9 School of Computing and Engineering, University of Huddersfield SEMANTICS meaning function M: -- maps BinNum to Decimals M[D(1)] =1---Rule 1 M[D(0)] = 0 ---Rule 2 M[ N(n,0) ] = M[n] x 2---Rule 3 M[ N(n,1) ] = M[n] x 2 + 1---Rule 4

10 School of Computing and Engineering, University of Huddersfield Binary Numerals: Example Meaning of 101 = M[N(N(D(1),0),1)] = M[N(D(1),0)] x 2 +1 using Rule 4 = (M[D(1)] x 2) x 2 + 1 using Rule 3 = (1 x 2 ) x 2 + 1 using Rule 1 = 5

11 School of Computing and Engineering, University of Huddersfield BUT WHAT OF SEMANTICS OF NATURAL NUMBERS? We can give semantics via an algebraic specification (see later in the term) where we gave the essential meaning of “objects” in terms of a set of axioms which relate the syntactic expression -- the axioms say what expressions are equal.

12 School of Computing and Engineering, University of Huddersfield Giving semantics via algebra: Numbers zero :  Num successor : Num  Num add : Num Num  Num for all x,y in Num add(successor(x),y) = successor(add(x,y)) -- (1) add(zero,y) = y -- (2) add(x,y) = add(y,x) etc PROVE 1 + 1 =2 !!!!!

13 School of Computing and Engineering, University of Huddersfield Haskell The DENOTATIONS of Syntax Classes of Program Languages will be FUNCTIONS IN LAMBDA CALCULUS These can be written in Haskell (~= Gofer) Haskell is: The “standard” pure, lazy functional language Practical: Haskell revision

14 School of Computing and Engineering, University of Huddersfield Conclusion n Abstract Syntax / Abstract Syntax Definitions are important as we use them to decompose the language into different ‘constructs’ in a way that abstracts away particular syntax forms. n For each construct we can then give a (composite) semantic definition. n The semantics of the language is then defined in terms of the semantics of each individual construct.


Download ppt "School of Computing and Engineering, University of Huddersfield CHA2545: WEEK 14 LECTURE: ABSTRACT SYNTAX SIMPLE EXAMPLE OF SEMANTICS DEFINITION TUTORIAL/PRACTICAL:"

Similar presentations


Ads by Google