Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Size: px
Start display at page:

Download "Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011"

Transcription

1 Lecture 4: The Declarative Sequential Kernel Language September 5th

2 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 2

3 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 3

4 Scanning contd Try it! The file code/mdc-scanner-manual.oz contains an implementation of the mdc scanner, and the file code/mdc-scanner-manual-wrapper.oz contains a functor wrapper needed for compilation. Compile the files: ozc -x mdc-scanner-manual-wrapper.oz -o mdc-scanner Execute the binary:./mdc-scanner -e "1+f*pep" What happens? 1 In practice, we would want the tokenizer to recover and proceed with the rest of the input. 4

5 Scanning contd Try it! The file code/mdc-scanner-manual.oz contains an implementation of the mdc scanner, and the file code/mdc-scanner-manual-wrapper.oz contains a functor wrapper needed for compilation. Compile the files: ozc -x mdc-scanner-manual-wrapper.oz -o mdc-scanner Execute the binary:./mdc-scanner -e "1+f*pep" What happens? There is no lexeme starting with the character e, and an error is reported. 1 The output is: [int(1) op( + ) cmd(f) op( * ) cmd(p) error] 1 In practice, we would want the tokenizer to recover and proceed with the rest of the input. 4

6 Scanning contd Example (A manual scanner for mdc code fragment) fun {Iterate State Seen Input Output} case Input of nil then if State == start then Output elseif State == int then int(seen) Output end [] Char Rest then if State == start then if Char == 32 then {Iterate start nil Rest Output} elseif {List.member Char "pf"} then {Iterate start nil Rest cmd(char) Output} elseif {List.member Char "+-*/"} then {Iterate start nil Rest op(char) Output} elseif {List.member Char " "} then {Iterate int Char Seen Rest Output} else error Output end elseif State == int then if {List.member Char " "} then {Iterate int Char Seen Rest Output} else {Iterate start nil Input int(seen) Output} end end end 5

7 Scanning contd Manual implementation of scanner DFAs is tedious and error-prone. We need to design a DFA corresponding to a set of regular expressions, then hard-code it using nested conditionals. There is a way out: we can use a scanner-builder tool. One of the most known scanner-builder tools is... 6

8 Scanning contd Manual implementation of scanner DFAs is tedious and error-prone. We need to design a DFA corresponding to a set of regular expressions, then hard-code it using nested conditionals. There is a way out: we can use a scanner-builder tool. One of the most known scanner-builder tools is lex. lex takes as input a lexical specification of a language in the form of a set of regular expressions and necessary bits of source code, and returns source code implementing a scanner DFA. Originally, lex produced C code, but versions for many other languages exist now (e.g., plex for Pascal, jflex for Java, flex++ for C++, etc.). yacc ( yet another compiler-compiler ) is an accompanying tool used to build compilers. Other scanner- and parser-builder tools include ANTLR, which can produce source code in C, C++, C#, Java, and Python; and Gump, the Oz tool for building scanners and parsers. 6

9 Scanning contd Try it! The file code/mdcscanner.ozg contains a specification of the mdc scanner, written as Oz/Gump code. The file code/mdc-scanner-gump.oz provides a simple wrapper that uses the scanner, and a few test cases. Open the files in the OPI (oz & and C-x C-f). Execute the code in mdc-scanner-gump.oz (C-. C-b). Consider the input 1+f*pep. What happens? 7

10 Scanning contd Try it! The file code/mdcscanner.ozg contains a specification of the mdc scanner, written as Oz/Gump code. The file code/mdc-scanner-gump.oz provides a simple wrapper that uses the scanner, and a few test cases. Open the files in the OPI (oz & and C-x C-f). Execute the code in mdc-scanner-gump.oz (C-. C-b). Consider the input 1+f*pep. What happens? The character e is not part of any valid lexeme, and an error is reported. The scanner recovers from the error and scans the rest of the input. The output is: [int(1) op( + ) cmd(f) op( * ) cmd(p) error(e) cmd(p)] 7

11 Scanning contd Example (A scanner specification for mdc code fragment) lex digit = <[0-9]> end lex command = <[pf]> end lex operator = <[\+\-\*\/]> end lex integer = <{digit}+> end lex <{command}> C in GumpScanner. class, getatom(?c) GumpScanner. class, puttoken( cmd C) end lex <{operator}> O in GumpScanner. class, getatom(?o) GumpScanner. class, puttoken( op O) end lex <{integer}> I in GumpScanner. class, getstring(?i) GumpScanner. class, puttoken( int {String.toInt I}) end Read more about the Oz scanner- and parser-builder in the Oz docs

12 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 9

13 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 10

14 The Declarative Model of Computation Models of Computation A model of computation is a programming language with formally defined syntax and semantics. (This definition is taken from the pensum book, and is not necessarily widely accepted.) The declarative sequential language defined in Ch. 2 of CTMCP is a subset of Oz. The declarative sequential kernel language is a subset of the declarative language. All models of computation presented further in CTMCP are extensions of the declarative model, and are realized as extensions of the declarative kernel language. 11

15 The Declarative Model of Computation contd What are the properties of the declarative model? The model is sequential all computations are performed in a linear order. The model is declarative all computations are independent of any external state and are themselves stateless and deterministic. The model supports both pure functional programming and pure deterministic logic programming. 12

16 The Declarative Model of Computation contd Practical declarative sequential language vs. declarative sequential kernel language. We first define the syntax and semantics of a simple kernel language. The kernel language contains constructs essential for the desired model of computation. Programs in the kernel language are typically very verbose. 3 We thus define a practical language by providing syntactic translations from its constructs to constructs in the kernel language. The practical language does not extend the model of computation, but makes programming more convenient just sue to the additional useful (though not essential) syntactic constructs. 3 Another example of the tradeoff between the implementer s and the user s convenience. 13

17 The Declarative Model of Computation contd Practical language vs. kernel language Practical language translation Kernel language semantics Benefits of the stratified approach: Specification of the semantics is easier for the kernel language than for the practical language. Additional constructs in the practical language are translated syntactically, and their semantics is thus already defined by the semantics of the kernel language constructs to which they translate. 14

18 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 15

19 Syntax of the Declarative Kernel Language Note: Most of the time you will be programming in the practical language, not in the kernel language. All syntactic constructions of the kernel language are valid in the practical language, but not vice versa. It is easier to write programs in the practical language using its additional features, but such programs are not valid in the kernel language. Programs in the practical language can be automatically translated into the kernel language by the compiler (compile with the option -E or --core); within the OPI (menu Oz Core syntax). 16

20 Syntax of the Declarative Kernel Language contd Kernel language syntax (CTMCP Tab. 2.1, p. 49) 4 statement ::= skip statement statement local id in statement end id = id id = value if id then statement else statement end case id of pattern then statement else statement end { id { id } } Be careful to distinguish between 5 { and } used as metalanguage symbols 6 and used as part of the language specified. 7 4 CTMCP makes an effort to discern between variable names (identifiers) and variables: the former are part of the syntax, the latter are part of the semantics. Thus we use id rather than variable. 5 One example of where the authors are not careful enough. 6 The EBNF symbols for denoting the number of occurences of a sequence. 7 The Oz symbols for procedure application. 17

21 Syntax of the Declarative Kernel Language contd Kernel language syntax (Tab. 2.2, p. 50) contd value ::= number record procedure number ::= integer float pattern ::= record record ::= literal literal ( { feature : id } ) procedure ::= proc { $ { id } } statement end literal ::= atom bool feature ::= atom bool integer bool ::= true false The variables (non-terminals) id, atom, integer, and float have microsyntactic (lexical) definitions. 8 The latter two are intuitive, the former two can very roughly be characterized as words that start with an upper- or lower-case letter, respectively. 8 See App. B and C in CTMCP. 18

22 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a do nothing statement: statement ::= skip The rule is non-recursive. Example (Statements) skip The code above has one parse tree, with a unique, one-step derivation. 9 9 One step, two sentential forms. 19

23 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a sequence of statements: statement ::= statement statement The rule is recursive, and leads to an exponential expansion of the sentential form. 10 Example (Statements) skip skip skip skip The code above has many parse trees, and all its derivations are based on just two rules. 10 The rule is in fact tree-recursive. 20

24 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a variable declaration: statement ::= local id in statement end The rule is (linearly) recursive. Example (Statements contd) local X in local Y in skip end end The code above has one parse tree, and can be derived using three rules. 21

25 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a variable-variable unification: statement ::= id = id The rule is non-recursive. Example (Statements contd) local X in local Y in X = Y end end The code above has one parse tree. 22

26 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a variable-value unification: statement ::= id = value The rule is potentially recursive. 11 Example (Statements contd) local X in X = 1 end The code above has one parse tree. 11 Through the sequence value procedure statement. 23

27 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a conditional: statement ::= if id then statement else statement end The rule is recursive. Example (Statements contd) if X then skip else if Y then local X in skip end else skip end end The code above has one parse tree. 24

28 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A value can be a record: record ::= literal literal ( { feature : id } ) The rule is non-recursive. Example (Record values) Literal = literal Empty = empty() Record = record(feature:field) Course = course(title:title subject:subject lecturer:lecturer) The code above has more than one parse tree. (Why?) 25

29 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A value can be a record: record ::= literal literal ( { feature : id } ) The rule is non-recursive. Example (Record values) Literal = literal Empty = empty() Record = record(feature:field) Course = course(title:title subject:subject lecturer:lecturer) The code above has more than one parse tree. (It is a sequence of statements; but each single statement in the sequence has one parse tree.) 25

30 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a pattern matching statement: statement ::= case id of pattern then statement else statement end The rule is recursive. Example (Statements contd) case X of record(feature:x) then X = 1 else Y = r(f:x) end The code above has one parse tree It also makes perfect sense wrt. the semantics; more on this later. 26

31 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A value can be a procedure: procedure ::= proc { $ { id } } statement end The rule is potentially recursive. Example (Procedure values) Skip = proc {$} skip end Unify = proc {$ X Y} X = Y end Both Skip and Unify are applicable see next slide. 27

32 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A statement can be a procedure application: statement ::= { id { id } } Example (Statements) local X in local Y in X = proc {$ Z} X = Z end {X Y} {Y X} end end X is applied to Y, and Y is applied to X. 28

33 Syntax of the Declarative Kernel Language contd Kernel language syntax contd A value can be an identifier, a number, a record, or a procedure: value ::= number record procedure In addition, we will want to use arithmetic operators, etc., with the following syntax (table 2.3 page 55): value ::= id operator id... but the operations these denote are, in fact, already handled by procedure application. These are syntactic extensions and strictly speaking are not valid in the kernel language. But we will use them for the simplicity of notation. 29

34 Syntax of the Declarative Kernel Language contd Example (Arithmetic operations as values) This simple code: X = is equivalent to this convoluted kernel language code: 13 local Add in local A in local B in Add = Number. + A = 1 B = 2 {Add A B X} end end end 13 If you use automatic translation of code into the kernel language, the result will differ. The translator is not fully compliant with the book s specification. 30

35 Syntax of the Declarative Kernel Language contd Additional specifications Variables must be declared before they are used. Example (Variable declarations) The following is a valid program: local X in local Y in X = Y end end The following is not: local X in X = Y end 31

36 Syntax of the Declarative Kernel Language contd Additional specifications contd Features in a record must be unique no two fields in a record can have the same feature. Example (Record features) The following is a valid program: local X in local Y in local Z in X = label(feature1:y feature2:z) end end end The following is not: local X in local Y in local Z in X = label(feature:y feature:z) end end end 32

37 Syntax of the Declarative Kernel Language contd Browse and Show The procedures Browse and Show do not belong to the declarative sequential kernel language. They arenot declarative they perform side effects (I/O). Browse is not sequential it starts a separate thread each time it is called. We will use them extensively to be able to see what our programs do. Example (Browse) local X in X = proc {$ X} case X of variable(value:y) then {Browse Y} else {Browse X} end end end 33

38 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 34

39 Types What is a type? A type is a set of values together with a set of operations on these values. We can define, for example, the type integer as the set of all integer numbers together with the operations of addition, subtraction, multiplication, and division. In programming languages, types can be built-in (e.g., int, double, etc.); user-defined (e.g., using the keyword typedef in C or C++). 35

40 Types contd Built-in types in Oz (fig. 2.16, p. 51) Value Number Record Procedure... Int Float Tuple... Char Literal List... Bool Atom... String True False 36

41 Types contd Questions What about the truth values true and false in other languages? 37

42 Types contd Questions What about the truth values true and false in other languages? A language where there are no separate Boolean values? 37

43 Types contd Questions What about the truth values true and false in other languages? A language where there are no separate Boolean values? A language where 0 means false and 1 means true? 37

44 Types contd Questions What about the truth values true and false in other languages? A language where there are no separate Boolean values? A language where 0 means false and 1 means true? A language where 0 means true and 1 means false? 37

45 Types contd Questions What about the truth values true and false in other languages? A language where there are no separate Boolean values? A language where 0 means false and 1 means true? A language where 0 means true and 1 means false? A language where 0 means true and 1 means true? 37

46 Types contd What is type checking? Type checking is validation of programs aimed at detecting illegal application of operations (application of operations not supported for a specific type or types). There are two variants of type checking dynamic and static type checking: Static: Performed before a program is executed (i.e., at compile-time). Dynamic: Performed during the execution of a program, before the execution of each individual operation (run-time type-checking). Types and type checking are not particularly well covered in the book. This is a large topic and we will not go into details, and will return to these issues only occasionally during later lectures. 38

47 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 39

48 Kernel Language and Practical Language How to define a practical language? We can define a practical language on top of the kernel language by: adding syntactic sugar syntactic conveniences that allow for a more concise and readable syntax without introducing abtractions; adding linguistic abstractions syntactic conveniences that not only allow for more concise programs, but also introduce new programming structures, without extending the model of computation. In practice, distinguishing between syntactic sugar and linguistic abstractions may not be easy. 40

49 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 41

50 Syntactic Sugar Nested values Allow the syntax of records and patterns to include as arguments not only identifiers, but also values. Example (Nested values) Instead of X = 1 Y = y(x:x) Z = z(y:y) write 42

51 Syntactic Sugar Nested values Allow the syntax of records and patterns to include as arguments not only identifiers, but also values. Example (Nested values) Instead of X = 1 Y = y(x:x) Z = z(y:y) write Z = z(y:y(x:1)) 42

52 Syntactic Sugar contd Multiple variable declarations Allow the syntax of variable declaration to declare more than one variable at a time Example (Multiple variable declarations) Instead of local X in local Y in... end end write local X Y in... end 43

53 Syntactic Sugar contd Variable initialization Allow the syntax of variable declaration to initialize variables at the same time. Example (Variable initialization) Instead of local X in X = 1... end write local X = 1 in... end 44

54 Syntactic Sugar contd Nesting markers Allow the use of nesting markers to turn statements into expressions. Example (Nesting markers) Instead of local Z in {Procedure X Y Z} {Browse Z} end write local Z = {Procedure X Y $} in {Browse Z} end 45

55 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 46

56 Linguistic Abstractions Functions Add syntax that allows to define functions. The application of a procedure is a statement there is no return value. The application of a function is an expression there is a return value. Example (Functions) Instead of Procedure = proc {$ Argument Result} Result = 2*Argument end {Procedure 1 X} write Function = fun {$ Argument} 2*Argument end X = {Function 1} 47

57 Linguistic Abstractions contd In the practical language, procedures can in fact be used as if they were functions. Example (Procedures as functions) Double = proc {$ Input Output} Output = 2 * Input end local Four in {Double 2 Four}... end local Four = {Double 2 $} in... end local Four = {Double 2} in... end The last form (without the nesting marker) will work only if it is the last argument which is bound to the return value In the relational progamming model where we add non-deterministic choice and search spaces, we can treat, in principle, each procedure parameter as the return value. 48

58 Linguistic Abstractions contd Record field accession Add syntax that allows partial matching of record fields. Example (Record field accession) Instead of 15 case R of label(feature1:_ feature2:_ feature3:value3) then X = Value3 end write case R of label(feature3:value...) then X = Value end 15 The underscore ( ) denotes an unnamed variable, and is used to keep values we do not care about. 49

59 Linguistic Abstractions contd Record field accession Add syntax that allows direct access to records fields. Example (Record field accession) Instead of case R of record(feature:value...) then X = Value end write X = R.feature 50

60 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 51

61 Further Extensions What if we do not know the label of a record? Can we pattern-match it? Example (Record labels) The following does not work: case R of Label(...) then X = Label end We can use built-in procedures available from the module Record: X = {Record.label R} Note: The above is of course not in the kernel language, but it can be easily translated into a kernel language statement. 52

62 Further Extensions Example (Investigating the properties of a record) code/records.oz local X = x(id:x) Y = y(id:y x:x) proc {Investigate R} if {Record.is R} then {Browse record(label:{record.label R} features:{record.arity R})} end end in {Investigate X} {Investigate Y} end Note: Record.arity returns a list of features sorted alphanumerically. 53

63 Further Comments Keywords and reserved words A keyword is a lexeme that has some special meaning in the language, and may be related to some specific syntactic and semantic rules. Most languages have keywords. A reserved word is a lexeme that can be used only as a keyword. Some languages treat all keywords as reserved words, others do not. 54

64 Further Comments contd Example (Keywords and reserved words) code/real-integer.f95 program realinteger real integer! real variable named integer integer real! integer variable named real real = 0 integer = 0 write (*,*) "real: ", real write (*,*) "integer: ", integer end program realinteger Here, both real and integer are used both as keywords and as variable identifiers they are not reserved words. 55

65 Further Comments contd Example (Keywords and reserved words) code/keywords.f95 program integer! program named integer integer program, subroutine, if, end! integer variables type :: type! complex type named type integer integer! integer member named integer end type type (type) :: else, write! variables of the type named type program = 1 subroutine = 2 if = 3 end = 4 else%integer = 5! assign to the integer member of else write%integer = 6 write (*,*) program, subroutine, if, end, else, write end program integer In Fortran, program, integer, subroutine, if, end, type, and write are keywords, but not reserved words. 56

66 Further Comments contd Try it! Try out the above examples by compiling and executing them: 16 $ gfortran keywords.f95 -o keywords &&./keywords 16 You will need a Fortran compiler, e.g., gfortran, the GNU Fortran compiler. 57

67 Further Comments contd Example (Keywords and reserved words) code/keywords.scm ;; a simple procedure for displaying values (define (test value) (display value) (newline)) ;; some fancy definitions (define x 1) (define $?=... +) (define + 1) (define define 2) ;; and the testing (test x) (test ($?=... x 1)) (test ($?=... + x x)) (test ($?=... define define)) In Scheme, virtually no keyword is reserved, but details depend on the implementation. 58

68 Further Comments contd Try it! Try out the above examples by feeding them into a Scheme interpreter (or by compiling them first): 17 $ guile keywords.scm 17 Try, e.g., Guile, the GNU Scheme interpreter. 59

69 Further Comments contd In Oz, keywords are reserved words. But quoting a keyword creates an atom or variable identifier: 18 An atom is any lexeme matching the regular expression [a-z][a-za-z0-9_]*, except for reserved words such as local, true etc. But any lexeme matching [^ \\]+ is also an atom! Example (Quoted atoms) The following is perfectly legal (but try removing the quotes!): X = local X in end? ( if : then ) 18 Apostrophes ( ) for atoms, backticks/grave accents (`) for identifiers. 60

70 Further Comments contd In Oz, keywords are reserved words. But quoting a keyword creates an atom or variable identifier: 18 An atom is any lexeme matching the regular expression [a-z][a-za-z0-9_]*, except for reserved words such as local, true etc. But any lexeme matching [^ \\]+ is also an atom! Example (Quoted atoms) The following is perfectly legal (but try removing the quotes!): X = local X in end? ( if : then ) Note: x == x is true; true == true is false; local == local is invalid. 18 Apostrophes ( ) for atoms, backticks/grave accents (`) for identifiers. 60

71 Comments to Lecture 3 contd Identifiers in Oz using X_ is a valid identifier; _X is not an identifier: it is parsed as two separate tokens. 61

72 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 62

73 Declarative Programming We have introduced the declarative sequential kernel language (DSKL), which is expressive enough for some interesting applications. We have specified the syntax formally. We have introduced the meaning of the constructs by handwaving. We will discuss the semantics of DSKL, but first have a look at some practical examples. You will really need to have your Mozart environment up and running now. The declarative sequential language Declarativeness: An operation is declarative if, whenever called with the same arguments, it returns the same results independent of any other computation state. [CTMCP] Alternatively: A program is declarative if it specifies what should be achieved, not how it should be achieved. 63

74 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 64

75 Variables and Identifiers Variable declarations All variables have to be declared before they are used. In other words, any identifier has to be mapped to a variable before it appear in statements other than variable declarations. Example (Declaring a variable) Local scope: local X in... end Global scope: declare X in... More on declare, local, and lexical scoping later. 65

76 Variables and Identifiers contd Identifiers and variables How do identifiers and variables relate? An identifier is a syntactic entity, it is part of the program as a sequence of tokens. A variable is a semantic entity, it is part of the execution of a program. During an execution of a program, a variable declaration statement results in the creation of a new variable, the creation of a mapping from the identifier to the variable. The mapping is stored in an environment. More on this later. 66

77 Variables and Identifiers contd Example (Declared, unbound variable) We can declare a variable without binding it to a value: declare Identifier Identifier Environment Variable 67

78 Variables and Identifiers contd Example (Declared, bound variable) We can declare a variable and bind it to a value: declare Identifier in Identifier = value Environment Identifier Variable value binding The identifier is mapped to the variable. The variable is bound to a value. 68

79 Variables and Identifiers contd Example (Declared, bound variable alternative explanation) We can declare a variable and bind it to a value: declare Identifier in Identifier = value Identifier Environment value During the binding, the unbound variable is replaced with the value. The identifier is mapped onto the value. We don t care much, as this is an implementational detail. 69

80 Variables and Identifiers contd Lexical scope How long is an identifier-variable mapping valid? local declares a variable, introduces a new local environment, and maps the identifier to the variable in that environment. Outside of this local statement the environment is inaccessible, and the mapping is not valid. declare declares a variable, and maps the identifier to the variable in the global environment. The mapping is valid until the end of the program, or until another declaration using the same identifier. Declarations can also be implicit (e.g., in pattern matching or procedure bodies). More on lexical scoping, dynamic scoping, and macro expansion later. 70

81 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 71

82 Dataflow Variables Declarative (dataflow) variables In Oz, variables are declarative after the initial binding, they cannot be reassigned another value. CTMCP calls such variables dataflow variables. Variables are thus not particularly variable this may be counterintuitive, but it s not an Oz idiosyncrasy (e.g., Erlang employs the same design and terminology). Example (Declarative variables) declare X in X = hello X = dolly % (1) 72

83 Dataflow Variables Declarative (dataflow) variables In Oz, variables are declarative after the initial binding, they cannot be reassigned another value. CTMCP calls such variables dataflow variables. Variables are thus not particularly variable this may be counterintuitive, but it s not an Oz idiosyncrasy (e.g., Erlang employs the same design and terminology). Example (Declarative variables) declare X in X = hello X = dolly % (1) (1) This is an illegal attempt to reassign a new value to the variable which X is mapped to. 72

84 Dataflow Variables contd Example (Declarative variables contd) declare X in X = hello declare X in X = dolly % (1) 73

85 Dataflow Variables contd Example (Declarative variables contd) declare X in X = hello declare X in X = dolly % (1) (1) This is a legal first-time binding of the variable which X is mapped to; the second declare re-maps X to a new unbound variable, and the first variable becomes inaccessible. 73

86 Dataflow Variables contd Partial values Complex values (records) may contain unbound variables. Such values are called partial values. Example (Partial values) declare X Y in % (1) Y = label(feature:x) % (2) (1) Both X and Y are mapped to unbound variables. (2) Y is mapped to a variable bound to partial value a record containing the unbound variable which X is mapped to. 74

87 Dataflow Variables contd Example (Declarative variables contd) declare A B X X = x(a:1 b:b) % (1) X = x(a:a b:2) % (2) (1) We bind the variable which X is mapped to to a partial value. (2) We make an attempt to bind the variable which X is mapped to to another partial value. What happens to X? What happens to A and B? 75

88 Dataflow Variables contd Example (Declarative variables contd) declare A B X X = x(a:1 b:b) % (1) X = x(a:a b:2) % (2) (1) We bind the variable which X is mapped to to a partial value. (2) We make an attempt to bind the variable which X is mapped to to another partial value. What happens to X? What happens to A and B? Statements of the type id = value are actually not assignments, but unifications. In a unification, we (recursively) check whether two values are compatible, and perform nested bindings if necessary. Thus, A becomes bound to 1, and B to 2. 75

89 Dataflow Variables contd Note: Identifiers are mapped within environments onto variables. Variables are bound to values. In X = 1, the variable which X is mapped onto is bound to the value 1. 76

90 Dataflow Variables contd Note: Identifiers are mapped within environments onto variables. Variables are bound to values. In X = 1, the variable which X is mapped onto is bound to the value 1. For convenience, we will say that X is bound to 1. Strictly speaking, this is incorrect. 76

91 Dataflow Variables contd Unbound variables and freezing If an unbound variable is used in an operation that needs the value of the variable, the computation freezes. In the sequential model of computation it means that the whole execution of the program halts, as there is no way to provide the value. In a concurrent environment, it is just one thread that is kept waiting, while other threads may provide the needed value. 77

92 Dataflow Variables contd Example (Freezing) declare X Y in Y = X + 2 % (1) {Browse Y} (1) The computation freezes here neither X nor Y are bound. The interpreter waits for X to become bound because its value is needed to compute the arithmetic operation. There is no other thread which could do the binding, so we stop. 78

93 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the Declarative Kernel Language Types and Type Checking The Practical Language Extensions Syntactic Sugar Linguistic Abstractions Further Extensions and Comments Declarative Programming Variables and Identifiers Dataflow Variables Summary 79

94 Summary This time Next time Syntax of the Declarative Sequential Kernel Language, extensions, handwaving semantics. Declarative variables, dataflow behaviour (freezing). Operational semantics of the Declarative Sequential Kernel Language the Kernel Language Virtual Mahchine. Declarative programming in Oz. 80

95 Summary contd Homework Pensum Questions...?...?...? Examine and try out today s code, read Mozart/Oz documentation if necessary. Read Ch. 2 in CTMCP (focus on the kernel language). All of today s slides, except for implementational details of the mdc scanners and examples in other languages than Oz. 81

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011 Lecture 5: Declarative Programming. The Declarative Kernel Language Machine September 12th, 2011 1 Lecture Outline Declarative Programming contd Dataflow Variables contd Expressions and Statements Functions

More information

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011 Lecture 6: The Declarative Kernel Language Machine September 13th, 2011 Lecture Outline Computations contd Execution of Non-Freezable Statements on the Abstract Machine The skip Statement The Sequential

More information

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Language Concepts, cs2104 Lecture 04 ( ) Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1

More information

Lecture 21: Relational Programming II. November 15th, 2011

Lecture 21: Relational Programming II. November 15th, 2011 Lecture 21: Relational Programming II November 15th, 2011 Lecture Outline Relational Programming contd The Relational Model of Computation Programming with choice and Solve Search Strategies Relational

More information

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3) Declarative Computation Model Single assignment store (VRH 2.2) Kernel language syntax (VRH 2.3) Carlos Varela RPI October 6, 2009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL On Academic

More information

CSC 467 Lecture 3: Regular Expressions

CSC 467 Lecture 3: Regular Expressions CSC 467 Lecture 3: Regular Expressions Recall How we build a lexer by hand o Use fgetc/mmap to read input o Use a big switch to match patterns Homework exercise static TokenKind identifier( TokenKind token

More information

Languages and Compilers

Languages and Compilers Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 4. Lexical Analysis (Scanning) Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office: TA-121 For

More information

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. September 26th, 2010 Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (1/48) Lecture Outline Memory Management,

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 2: Lexical analysis Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 Basics of Lexical Analysis: 2 Some definitions:

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4 CS321 Languages and Compiler Design I Winter 2012 Lecture 4 1 LEXICAL ANALYSIS Convert source file characters into token stream. Remove content-free characters (comments, whitespace,...) Detect lexical

More information

SMURF Language Reference Manual Serial MUsic Represented as Functions

SMURF Language Reference Manual Serial MUsic Represented as Functions SMURF Language Reference Manual Serial MUsic Represented as Functions Richard Townsend, Lianne Lairmore, Lindsay Neubauer, Van Bui, Kuangya Zhai {rt2515, lel2143, lan2135, vb2363, kz2219}@columbia.edu

More information

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010 IPCoreL Programming Language Reference Manual Phillip Duane Douglas, Jr. 11/3/2010 The IPCoreL Programming Language Reference Manual provides concise information about the grammar, syntax, semantics, and

More information

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Compiler Passes Analysis of input program (front-end) character stream

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

Part 5 Program Analysis Principles and Techniques

Part 5 Program Analysis Principles and Techniques 1 Part 5 Program Analysis Principles and Techniques Front end 2 source code scanner tokens parser il errors Responsibilities: Recognize legal programs Report errors Produce il Preliminary storage map Shape

More information

Introduction to Lexical Analysis

Introduction to Lexical Analysis Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexical analyzers (lexers) Regular

More information

An Oz Subset. 1 Microsyntax for An Oz Subset. Notational Conventions. COP 4020 Programming Languages 1 January 17, 2012

An Oz Subset. 1 Microsyntax for An Oz Subset. Notational Conventions. COP 4020 Programming Languages 1 January 17, 2012 COP 4020 Programming Languages 1 January 17, 2012 An Oz Subset This document describes a subset of Oz [VH04] that is designed to be slightly larger than the declarative kernel language of chapter 2 (of

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1 CSEP 501 Compilers Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter 2008 1/8/2008 2002-08 Hal Perkins & UW CSE B-1 Agenda Basic concepts of formal grammars (review) Regular expressions

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming

More information

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards Language Reference Manual Introduction The purpose of

More information

Decaf Language Reference Manual

Decaf Language Reference Manual Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented

More information

Lexical Analysis. Chapter 2

Lexical Analysis. Chapter 2 Lexical Analysis Chapter 2 1 Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples

More information

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens Syntactic Analysis CS45H: Programming Languages Lecture : Lexical Analysis Thomas Dillig Main Question: How to give structure to strings Analogy: Understanding an English sentence First, we separate a

More information

The role of semantic analysis in a compiler

The role of semantic analysis in a compiler Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

Lexical Analysis. Lecture 2-4

Lexical Analysis. Lecture 2-4 Lexical Analysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 2 1 Administrivia Moving to 60 Evans on Wednesday HW1 available Pyth manual available on line.

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

Typescript on LLVM Language Reference Manual

Typescript on LLVM Language Reference Manual Typescript on LLVM Language Reference Manual Ratheet Pandya UNI: rp2707 COMS 4115 H01 (CVN) 1. Introduction 2. Lexical Conventions 2.1 Tokens 2.2 Comments 2.3 Identifiers 2.4 Reserved Keywords 2.5 String

More information

FRAC: Language Reference Manual

FRAC: Language Reference Manual FRAC: Language Reference Manual Justin Chiang jc4127 Kunal Kamath kak2211 Calvin Li ctl2124 Anne Zhang az2350 1. Introduction FRAC is a domain-specific programming language that enables the programmer

More information

Compiler phases. Non-tokens

Compiler phases. Non-tokens Compiler phases Compiler Construction Scanning Lexical Analysis source code scanner tokens regular expressions lexical analysis Lennart Andersson parser context free grammar Revision 2011 01 21 parse tree

More information

2.2 Syntax Definition

2.2 Syntax Definition 42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

More information

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Lexical Analysis Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Phase Ordering of Front-Ends Lexical analysis (lexer) Break input string

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

More information

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

Parsing and Pattern Recognition

Parsing and Pattern Recognition Topics in IT 1 Parsing and Pattern Recognition Week 10 Lexical analysis College of Information Science and Engineering Ritsumeikan University 1 this week mid-term evaluation review lexical analysis its

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott Introduction programming languages need to be precise natural languages less so both form (syntax) and meaning

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Statically vs. Dynamically typed languages

More information

easel LANGUAGE REFERENCE MANUAL

easel LANGUAGE REFERENCE MANUAL easel LANGUAGE REFERENCE MANUAL Manager Danielle Crosswell dac2182 Language Guru Tyrus Cukavac thc2125 System Architect Yuan-Chao Chou yc3211 Tester Xiaofei Chen xc2364 Table of Contents 1. Introduction...

More information

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization Second round: the scanner Lexical analysis Syntactical analysis Semantical analysis Intermediate code generation Optimization Code generation Target specific optimization Lexical analysis (Chapter 3) Why

More information

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler. More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical

More information

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing

More information

CS 242. Fundamentals. Reading: See last slide

CS 242. Fundamentals. Reading: See last slide CS 242 Fundamentals Reading: See last slide Syntax and Semantics of Programs Syntax The symbols used to write a program Semantics The actions that occur when a program is executed Programming language

More information

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis CS 1622 Lecture 2 Lexical Analysis CS 1622 Lecture 2 1 Lecture 2 Review of last lecture and finish up overview The first compiler phase: lexical analysis Reading: Chapter 2 in text (by 1/18) CS 1622 Lecture

More information

Software Engineering using Formal Methods

Software Engineering using Formal Methods Software Engineering using Formal Methods Introduction to Promela Wolfgang Ahrendt 03 September 2015 SEFM: Promela /GU 150903 1 / 36 Towards Model Checking System Model Promela Program byte n = 0; active

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

The Structure of a Syntax-Directed Compiler

The Structure of a Syntax-Directed Compiler Source Program (Character Stream) Scanner Tokens Parser Abstract Syntax Tree Type Checker (AST) Decorated AST Translator Intermediate Representation Symbol Tables Optimizer (IR) IR Code Generator Target

More information

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Conceptual Structure of a Compiler Source code x1 := y2

More information

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax

More information

PL Revision overview

PL Revision overview PL Revision overview Course topics Parsing G = (S, P, NT, T); (E)BNF; recursive descent predictive parser (RDPP) Lexical analysis; Syntax and semantic errors; type checking Programming language structure

More information

Introduction to Lexical Analysis

Introduction to Lexical Analysis Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples

More information

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer Assigned: Thursday, September 16, 2004 Due: Tuesday, September 28, 2004, at 11:59pm September 16, 2004 1 Introduction Overview In this

More information

UNIT -2 LEXICAL ANALYSIS

UNIT -2 LEXICAL ANALYSIS OVER VIEW OF LEXICAL ANALYSIS UNIT -2 LEXICAL ANALYSIS o To identify the tokens we need some method of describing the possible tokens that can appear in the input stream. For this purpose we introduce

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 18 November, 2014 1 / 18 Two parallel pipelines A large proportion

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Examples of attributes: values of evaluated subtrees, type information, source file coordinates, 1 2 3 Attributes can be added to the grammar symbols, and program fragments can be added as semantic actions to the grammar, to form a syntax-directed translation scheme. Some attributes may be set by

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 28 Mary Cryan School of Informatics University of Edinburgh mcryan@inf.ed.ac.uk 21 November 2018 1 / 18 Two parallel pipelines A large proportion

More information

Scheme: Expressions & Procedures

Scheme: Expressions & Procedures Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University

More information

Programming Assignment II

Programming Assignment II Programming Assignment II 1 Overview of the Programming Project Programming assignments II V will direct you to design and build a compiler for Cool. Each assignment will cover one component of the compiler:

More information

ASML Language Reference Manual

ASML Language Reference Manual ASML Language Reference Manual Tim Favorite (tuf1) & Frank Smith (fas2114) - Team SoundHammer Columbia University COMS W4115 - Programming Languages & Translators 1. Introduction The purpose of Atomic

More information

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler. Name: Midterm Exam PID: This is a closed-book exam; you may not use any tools besides a pen. You have 75 minutes to answer all questions. There are a total of 75 points available. Please write legibly;

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

CS 6353 Compiler Construction Project Assignments

CS 6353 Compiler Construction Project Assignments CS 6353 Compiler Construction Project Assignments In this project, you need to implement a compiler for a language defined in this handout. The programming language you need to use is C or C++ (and the

More information

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions 1 Agenda Overview of language recognizers Basic concepts of formal grammars Scanner Theory

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going

More information

Compiler construction

Compiler construction Compiler construction Martin Steffen March 13, 2017 Contents 1 Abstract 1 1.1 Symbol tables. 1 1.1.1 Introduction 1 1.1.2 Symbol table design and interface.. 2 1.1.3 Implementing symbol tables 3 1.1.4

More information

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; } Ex: The difference between Compiler and Interpreter The interpreter actually carries out the computations specified in the source program. In other words, the output of a compiler is a program, whereas

More information

CS Exam #1-100 points Spring 2011

CS Exam #1-100 points Spring 2011 CS 4700 - Exam #1-100 points Spring 2011 Fill in the blanks (1 point each) 1. syntactic sugar is a term coined for additions to the syntax of a computer language that do not affect its expressiveness but

More information

G Compiler Construction Lecture 4: Lexical Analysis. Mohamed Zahran (aka Z)

G Compiler Construction Lecture 4: Lexical Analysis. Mohamed Zahran (aka Z) G22.2130-001 Compiler Construction Lecture 4: Lexical Analysis Mohamed Zahran (aka Z) mzahran@cs.nyu.edu Role of the Lexical Analyzer Remove comments and white spaces (aka scanning) Macros expansion Read

More information

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement Outline Expression Evaluation and Control Flow In Text: Chapter 6 Notation Operator evaluation order Operand evaluation order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

More information

Software Engineering using Formal Methods

Software Engineering using Formal Methods Software Engineering using Formal Methods Introduction to Promela Wolfgang Ahrendt & Richard Bubel & Reiner Hähnle & Wojciech Mostowski 31 August 2011 SEFM: Promela /GU 110831 1 / 35 Towards Model Checking

More information

Functional programming in LISP

Functional programming in LISP Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of

More information

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc. Syntax Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal and exact or there will

More information

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1 Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind

More information

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008. Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008. Outline. Abstractions for telling a computer how to do things. Computational Paradigms. Language Definition, Translation.

More information

Structure of Programming Languages Lecture 3

Structure of Programming Languages Lecture 3 Structure of Programming Languages Lecture 3 CSCI 6636 4536 Spring 2017 CSCI 6636 4536 Lecture 3... 1/25 Spring 2017 1 / 25 Outline 1 Finite Languages Deterministic Finite State Machines Lexical Analysis

More information

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang CSE 341 Section 7 Eric Mullen Spring 2017 Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang Outline Interpreting LBI (Language Being Implemented) Assume Correct Syntax Check for Correct

More information

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567) 1 M/s Managing distributed workloads Language Reference Manual Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567) Table of Contents 1. Introduction 2. Lexical elements 2.1 Comments 2.2

More information

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

More information

ML 4 A Lexer for OCaml s Type System

ML 4 A Lexer for OCaml s Type System ML 4 A Lexer for OCaml s Type System CS 421 Fall 2017 Revision 1.0 Assigned October 26, 2017 Due November 2, 2017 Extension November 4, 2017 1 Change Log 1.0 Initial Release. 2 Overview To complete this

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions CSE 413 Programming Languages & Implementation Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions 1 Agenda Overview of language recognizers Basic concepts of formal grammars Scanner Theory

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Lexical Analysis. Lecture 3-4

Lexical Analysis. Lecture 3-4 Lexical Analysis Lecture 3-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 3-4 1 Administrivia I suggest you start looking at Python (see link on class home page). Please

More information

KU Compilerbau - Programming Assignment

KU Compilerbau - Programming Assignment 716.077 KU Compilerbau - Programming Assignment Univ.-Prof. Dr. Franz Wotawa, Birgit Hofer Institute for Software Technology, Graz University of Technology April 20, 2011 Introduction During this semester

More information

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis dministrivia Lexical nalysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Moving to 6 Evans on Wednesday HW available Pyth manual available on line. Please log into your account and electronically

More information