270 likes | 484 Views
Chapter 2.2. Programming Language Definition. Syntax and Semantics Semantics definition Denotational Semantics Elementary actions Syntax definitions Backus-Naur Formalism Syntax diagrams. Summary. Syntax = form Semantics = meaning Example My beer is on the table Syntax : OK
E N D
Chapter 2.2 Programming Language Definition
Syntax and Semantics Semantics definition Denotational Semantics Elementary actions Syntax definitions Backus-Naur Formalism Syntax diagrams Summary
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 !!! Syntax and Semantics
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 Summary
Terminal Symbols Appear literally in program text Examples : “a”, “3”, “END”, “>”, “:=” Non-terminal Symbols Name given to set of terminal symbols Written between < and > Defined by a metalinguistic expression Examples : <Letter> , <Digit>,<Expression>,<Module> Metalinguistic expression Formal definition (::=) of a non-terminal symbol Contains terminal and non-terminal symbols Expresses juxtaposition or choice (the | operator) Example : <Digit> ::= 0|1|2|3|4|5|6|7|8|9 Backus Naur Formalism (BNF)
<UpperCaseLetter> ::= A|B|C|D|E| |X|Y|Z <LowerCaseLetter> ::= a|b|c|d|e| |x|y|z <Letter> ::= <UpperCaseLetter> | <LowerCaseLetter> <Digit> ::= 1|2|3|4|5|6|7|8|9|0 <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber> <IntegerNumber> ::= <CardinalNumber> | + <CardinalNumber> | - <CardinalNumber> <Identifier> ::= <Letter> | <Identifier><Letter> | <Identifier> <Digit> Backus Naur Formalism (BNF)Examples
Is 123 a CardinalNumber ? Is it a digit ? Certainly not <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a CardinalNumber ? Is it Digit followed by a CardinalNumber ? Is 23 a Cardinal Number ? Is it a digit ? Certainly not <CardinalNumber> ::= <Digit> | <Digit><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 Cardinal Number ? <CardinalNumber> ::= <Digit> | <Digit><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 Cardinal Number ? Is it a digit ? YES <CardinalNumber> ::= <Digit> | <Digit><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 <CardinalNumber> ::= <Digit> | <Digit><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 <CardinalNumber> ::= <Digit> | <Digit><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 <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Correct identifiers Length, Width, Cost, ... MySalary, NumberOfStudents, Year1, … X1, Y2, A1, A5, … Incorrect identifiers My Salary, Number-of-students, … 1stYear, ... <Identifier> ::= <Letter> | <Identifier><Letter> | <Identifier> <Digit>
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [] 0, 1 or many occurrences {}
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [ ] 0, 1 or many occurrences {}
<UpperCaseLetter> ::= A|B|C|D|E| |X|Y|Z <LowerCaseLetter> ::= a|b|c|d|e| |x|y|z <Letter> ::= <UpperCaseLetter> | <LowerCaseLetter> <Digit> ::= 1|2|3|4|5|6|7|8|9|0 <CardinalNumber> ::= <Digit> {<Digit>} <IntegerNumber> ::= [+|-]<CardinalNumber> <Identifier> ::= <Letter> {<Letter>|<Digit>} Backus Naur Formalism (BNF)Examples
Syntax and Semantics Semantics definition Denotational Semantics Elementary actions Syntax definitions Backus-Naur Formalism Syntax diagrams Summary
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. Backus Naur Formalism (BNF) Terminal symbol Non-terminal symbol
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [] 0, 1 or many occurrences {}
UpperCaseLetter LowerCaseLetter y A B C D E V W X Y a b c d e v w x Z z UpperCaseLetter LowerCaseLetter Syntax DiagramsExamples Letter
6 8 5 4 3 7 1 0 9 2 Digit Syntax DiagramsExamples Digit <Digit> ::= 0|1|2|3|4|5|6|7|8|9 CardinalNumber <CardinalNumber> ::= <Digit> {<Digit>} Digit Digit
+ - Syntax DiagramsExamples IntegerNumber <IntegerNumber> ::= [ + | - ] <CardinalNumber> CardinalNumber
Letter Digit Letter Syntax DiagramsExamples Identifier <Identifier> ::= <Letter> {<Letter>|<Digit>}