Advertisement
Forceisthop

Untitled

Sep 19th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. Certainly, here's a combined summary of the various aspects of semantic analysis in Fortran along with their respective descriptions and example code snippets:
  2.  
  3. 1. **Variable Declaration and Scope**:
  4. - **Description**: Fortran enforces explicit variable declarations to ensure that all variables have defined types and scopes.
  5. - **Example Code**:
  6. ```fortran
  7. implicit none
  8. integer :: num1, num2
  9. real :: result
  10. ```
  11.  
  12. 2. **Type Compatibility**:
  13. - **Description**: Fortran performs strict type checking to ensure that operations involve compatible data types.
  14. - **Example Code**:
  15. ```fortran
  16. num1 = 10
  17. num2 = 3
  18. result = divide(num1, num2)
  19. ```
  20.  
  21. 3. **Array Bounds**:
  22. - **Description**: Fortran checks that array indices fall within declared bounds and are of integer type.
  23. - **Example Code**:
  24. ```fortran
  25. real, dimension(3) :: numbers
  26. numbers = [1.0, 2.0, 3.0]
  27. ```
  28.  
  29. 4. **Function and Subroutine Calls**:
  30. - **Description**: Fortran checks that function and subroutine calls match declared signatures in terms of arguments.
  31. - **Example Code**:
  32. ```fortran
  33. result = divide(num1, num2)
  34. call print_numbers(numbers)
  35. ```
  36.  
  37. 5. **Use of Intrinsic Functions**:
  38. - **Description**: Fortran uses intrinsic functions with specific type requirements.
  39. - **Example Code**:
  40. ```fortran
  41. result = sqrt(result)
  42. ```
  43.  
  44. 6. **Labels and Branching**:
  45. - **Description**: Fortran checks that labels used in `goto` statements exist and that branching statements target the correct labels.
  46. - **Example Code**:
  47. ```fortran
  48. if (result > 0.0) then
  49. print *, "Result is positive"
  50. else
  51. print *, "Result is non-positive"
  52. end if
  53. ```
  54.  
  55. 7. **Derived Types**:
  56. - **Description**: Fortran supports user-defined data types called derived types.
  57. - **Example Code**:
  58. ```fortran
  59. type Student
  60. character(30) :: name
  61. integer :: age
  62. end type Student
  63.  
  64. type(Student) :: john
  65. john%name = "John Doe"
  66. john%age = 25
  67. ```
  68.  
  69. 8. **Pointer and Allocatable Variables**:
  70. - **Description**: Fortran supports dynamic memory allocation through pointers and allocatable variables.
  71. - **Example Code**:
  72. ```fortran
  73. real, pointer :: dynamic_var
  74. allocate(dynamic_var)
  75. dynamic_var = 42.0
  76. deallocate(dynamic_var)
  77. ```
  78.  
  79. 9. **Error Handling**:
  80. - **Description**: Fortran's semantic analysis includes error handling, such as identifying undefined variables, mismatched types, or unreachable code.
  81. - **Example Code**: Below is an example demonstrating an undefined variable error:
  82. ```fortran
  83. implicit none
  84. integer :: a, b, c
  85. c = a + b + d ! 'd' is undefined, causing an error
  86. ```
  87.  
  88. 10. **Common Blocks and Modules**:
  89. - **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.
  90. - **Example Code**: Demonstrating the use of a common block:
  91. ```fortran
  92. program CommonBlockExample
  93. implicit none
  94. integer :: num1, num2
  95. common /myblock/ num1, num2 ! Common block declaration
  96. num1 = 10
  97. num2 = 3
  98. end program CommonBlockExample
  99. ```
  100.  
  101. 11. **Interface Blocks**:
  102. - **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.
  103. - **Example Code**: Demonstrating an interface block:
  104. ```fortran
  105. interface
  106. subroutine my_subroutine(x, y)
  107. real, intent(in) :: x, y
  108. end subroutine my_subroutine
  109. end interface
  110. ```
  111.  
  112. 12. **Inclusion of External Libraries**:
  113. - **Description**: Checks may involve verifying that external libraries or modules are correctly included and used.
  114. - **Example Code**: Including an external module:
  115. ```fortran
  116. use my_module ! Assuming 'my_module' is an external module
  117. ```
  118.  
  119. 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