Chapter 2.2 Programming Language Definition
Summary Syntax and Semantics Semantics definition –Denotational Semantics –Elementary actions Syntax definitions –Backus-Naur Formalism –Syntax diagrams
Syntax and Semantics Syntax = form Semantics = meaning Example –My beer is on the table Syntax : OK Semantics : OK –My table is on the beer Syntax : OK Semantics : You had too many beers !!!
Summary Syntax and Semantics Semantics definition –Denotational Semantics –Elementary actions Syntax definitions –Backus-Naur Formalism –Syntax diagrams
Summary Syntax and Semantics Semantics definition –Denotational Semantics –Elementary actions Syntax definitions –Backus-Naur Formalism –Syntax diagrams
Backus Naur Formalism (BNF) Terminal Symbols –Appear literally in program text –Examples : “a”, “3”, “END”, “>”, “:=” Non-terminal Symbols –Name given to set of terminal symbols –Written between –Defined by a metalinguistic expression –Examples :,,, Metalinguistic expression –Formal definition (::=) of a non-terminal symbol –Contains terminal and non-terminal symbols –Expresses juxtaposition or choice (the | operator) –Example : ::= 0|1|2|3|4|5|6|7|8|9
Backus Naur Formalism (BNF) Examples ::= A|B|C|D|E| |X|Y|Z ::= a|b|c|d|e| |x|y|z ::= | ::= 1|2|3|4|5|6|7|8|9|0 ::= | ::= | + | - ::= | |
::= | Is 123 a CardinalNumber ? Is it a digit ? Certainly not
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ?
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it a digit ? Certainly not
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it Digit followed by a CardinalNumber ? Is 3 a Cardinal Number ?
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it Digit followed by a CardinalNumber ? Is 3 a Cardinal Number ? Is it a digit ? YES
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it Digit followed by a CardinalNumber ? Is 3 a CardinalNumber ? Is it a digit ? YES As a consequence, 3 is a CardinalNumber
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it Digit followed by a CardinalNumber ? Is 3 a CardinalNumber ? Is it a digit ? YES As a consequence, 3 is a CardinalNumber As a consequence, 23 is a CardinalNumber
::= | Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it Digit followed by a CardinalNumber ? Is 3 a CardinalNumber ? Is it a digit ? YES As a consequence, 3 is a CardinalNumber As a consequence, 23 is a CardinalNumber As a consequence, 123 is a CardinalNumber
::= | | Correct identifiers –Length, Width, Cost,... –MySalary, NumberOfStudents, Year1, … –X1, Y2, A1, A5, … Incorrect identifiers –My Salary, Number-of-students, … –1stYear,...
Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}
Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [ ] {}
Backus Naur Formalism (BNF) Examples ::= A|B|C|D|E| |X|Y|Z ::= a|b|c|d|e| |x|y|z ::= | ::= 1|2|3|4|5|6|7|8|9|0 ::= { } ::= [ +|- ] ::= { | }
Summary Syntax and Semantics Semantics definition –Denotational Semantics –Elementary actions Syntax definitions –Backus-Naur Formalism –Syntax diagrams
Backus Naur Formalism (BNF) Terminal Symbols Non-terminal Symbols Metalinguistic expression –Railroad diagrams with semaphores requiring specific symbols to grant passage. –The defined non-terminal symbol leaves all its symbols, in the order of occurrence, at appropriate semaphores and reaches the exit with none left. Terminal symbol Non-terminal symbol
Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}
Syntax Diagrams Examples UpperCaseLetter ABCDEVWXYZ Letter LowerCaseLetter abcdevwxyz UpperCaseLetter LowerCaseLetter
Syntax Diagrams Examples Digit CardinalNumber ::= { } ::= 0|1|2|3|4|5|6|7|8|9 Digit
Syntax Diagrams Examples IntegerNumber ::= [ + | - ] CardinalNumber + -
Syntax Diagrams Examples Identifier ::= { | } Letter Digit Letter
Syntax Diagrams Example from the Modula 2 syntax