Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Certainly, here's a combined summary of the various aspects of semantic analysis in Fortran along with their respective descriptions and example code snippets:
- 1. **Variable Declaration and Scope**:
- - **Description**: Fortran enforces explicit variable declarations to ensure that all variables have defined types and scopes.
- - **Example Code**:
- ```fortran
- implicit none
- integer :: num1, num2
- real :: result
- ```
- 2. **Type Compatibility**:
- - **Description**: Fortran performs strict type checking to ensure that operations involve compatible data types.
- - **Example Code**:
- ```fortran
- num1 = 10
- num2 = 3
- result = divide(num1, num2)
- ```
- 3. **Array Bounds**:
- - **Description**: Fortran checks that array indices fall within declared bounds and are of integer type.
- - **Example Code**:
- ```fortran
- real, dimension(3) :: numbers
- numbers = [1.0, 2.0, 3.0]
- ```
- 4. **Function and Subroutine Calls**:
- - **Description**: Fortran checks that function and subroutine calls match declared signatures in terms of arguments.
- - **Example Code**:
- ```fortran
- result = divide(num1, num2)
- call print_numbers(numbers)
- ```
- 5. **Use of Intrinsic Functions**:
- - **Description**: Fortran uses intrinsic functions with specific type requirements.
- - **Example Code**:
- ```fortran
- result = sqrt(result)
- ```
- 6. **Labels and Branching**:
- - **Description**: Fortran checks that labels used in `goto` statements exist and that branching statements target the correct labels.
- - **Example Code**:
- ```fortran
- if (result > 0.0) then
- print *, "Result is positive"
- else
- print *, "Result is non-positive"
- end if
- ```
- 7. **Derived Types**:
- - **Description**: Fortran supports user-defined data types called derived types.
- - **Example Code**:
- ```fortran
- type Student
- character(30) :: name
- integer :: age
- end type Student
- type(Student) :: john
- john%name = "John Doe"
- john%age = 25
- ```
- 8. **Pointer and Allocatable Variables**:
- - **Description**: Fortran supports dynamic memory allocation through pointers and allocatable variables.
- - **Example Code**:
- ```fortran
- real, pointer :: dynamic_var
- allocate(dynamic_var)
- dynamic_var = 42.0
- deallocate(dynamic_var)
- ```
- 9. **Error Handling**:
- - **Description**: Fortran's semantic analysis includes error handling, such as identifying undefined variables, mismatched types, or unreachable code.
- - **Example Code**: Below is an example demonstrating an undefined variable error:
- ```fortran
- implicit none
- integer :: a, b, c
- c = a + b + d ! 'd' is undefined, causing an error
- ```
- 10. **Common Blocks and Modules**:
- - **Description**: Common blocks and modules allow data sharing between program units. Semantic analysis ensures that variables used in these constructs are properly declared and accessible.
- - **Example Code**: Demonstrating the use of a common block:
- ```fortran
- program CommonBlockExample
- implicit none
- integer :: num1, num2
- common /myblock/ num1, num2 ! Common block declaration
- num1 = 10
- num2 = 3
- end program CommonBlockExample
- ```
- 11. **Interface Blocks**:
- - **Description**: In modern Fortran (Fortran 90 and later), interface blocks are used to explicitly define the interfaces of functions and subroutines. Semantic analysis checks that these interfaces match the actual implementations.
- - **Example Code**: Demonstrating an interface block:
- ```fortran
- interface
- subroutine my_subroutine(x, y)
- real, intent(in) :: x, y
- end subroutine my_subroutine
- end interface
- ```
- 12. **Inclusion of External Libraries**:
- - **Description**: Checks may involve verifying that external libraries or modules are correctly included and used.
- - **Example Code**: Including an external module:
- ```fortran
- use my_module ! Assuming 'my_module' is an external module
- ```
- These aspects and their associated descriptions and code examples provide a comprehensive overview of semantic analysis in Fortran, emphasizing code correctness, type compatibility, and proper usage of language constructs.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement