complement notes 10.14
This commit is contained in:
58
notes/5.md
Normal file
58
notes/5.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# Semantic Analysis
|
||||
|
||||
* after passing the lexical and syntax analysis, there are still errors. so correcting usage of variables, objects and function... are needed.
|
||||
|
||||
**Semantic Analysis** ensures the program satisfies a set of rules regarding the usage of programming constructs:
|
||||
* identifiers declared before used
|
||||
* types
|
||||
* inheritance relationships
|
||||
* single definition
|
||||
|
||||
## Categories of Semantic Analysis
|
||||
|
||||
* Scopes
|
||||
* Types
|
||||
|
||||
### Scope
|
||||
|
||||
**Lexical Scope**: textual region in the program
|
||||
|
||||
**Symbol Tables**
|
||||
|
||||
Symantic checks refer to properties of identifier in the program; it need an environment to store identifier info: **symbol table**.
|
||||
|
||||
| name | kind | type |
|
||||
| ---- | ---- | ------------ |
|
||||
| foo | func | `int -> int` |
|
||||
| m | arg | `int` |
|
||||
| n | arg | `int` |
|
||||
| tmp | var | char |
|
||||
|
||||
Each scope has symbol tables.
|
||||
And program has hierachy of symbol tables(scope).
|
||||
|
||||
if the identifier is used traverse the hierachy of symbol tables upward until finding the identifier with the same name to determine the declaration from the current scope.
|
||||
|
||||
#### Build a Symbol Table
|
||||
|
||||
there are five operations:
|
||||
* insert scope
|
||||
* exit scope
|
||||
* find symbol(x)
|
||||
* add symbol(x)
|
||||
* check scope(x)
|
||||
|
||||
### Type
|
||||
|
||||
* Type checking
|
||||
* Type inferencing
|
||||
|
||||
Three Language Types:
|
||||
* Statically typed
|
||||
* Dynamically typed
|
||||
* Untyped(machine code)
|
||||
|
||||
**Static Type Checking**
|
||||
Does not require additional type checking instructions at runtime.
|
||||
and guarantees that the executions are safe at compile time.
|
||||
modern languages require both static and dynamic type checking(union, void ptr)
|
||||
Reference in New Issue
Block a user