-
Program semantics
- The meaning of:
- - Expressions
- - Statements
- - Program units
-
Program Syntax
Forms and structures of constructs
-
How is a programming language defined?
- Language syntax
- Language semantics
-
Lexeme
- Lowest syntactic unit of a language
- Described and checked by lexical analyzer
- E.g +, *, =, etc...
- Lexemes are converted to tokens when the code is tokenized
-
Tokens
- Categorize lexemes from a program
- E.g new, int, <identifiers>, etc...
-
Meta language
Language used to describe another language
-
Standard form for describing programming language syntax
Backus-Naur form
-
Describe an assignment statement in BNF
<Assignment_Statement> → <var> = <expression>
-
Examples of terminals in BNF
-
Nonterminal symbols (BNF)
- Symbols that can be replaced
- In the expression,
- <assignment>→<var>=<expr>
- The LHS <assignment> is nonterminal
-
Put the following processes in order:
Lexical analyzer
Syntax analyzer
Tokenizer
Parse tree
Code Generator
Assembler
Linker
- Tokenizer
- Syntax analyzer
- Parse tree
-
What is a keyword
- Keywords have a special (contextual) meaning
- They can be used as idenfitiers
-
What is a reserved word
- Reserved words are reserved
- They cannot be used as identifiers
-
BNF for IF statement
<if_statement>→if<expr> then <stmt>
-
BNF for IF-ELSE statement
<if_statement> → if <expr> then <statement> else <statement>
-
BNF for a list
- <identifier_list>→<identifier>|<identifier_list>→<identifier>,<identifier_list>
- The | is an OR (choose the appropriate definition)
- Recursive
-
When is a grammar ambiguous
If two parse trees can be generated from it
-
Types of program semantics
- Operational - Compare difference between machine states
- Axiomatic - Define axioms for each statement type
-
Conditions in axiomic semantics
- Precondition
- Postcondition
- Weakest precondition
-
Precondition
- Before a statement
- Describes relationships and constraints of variables
-
Postcondition
- Occurs after a statement
- True after the statement is executed
-
Weakest precondition
The least restrictive precondition that garuntees the post condition
|
|