Survey of Programming Language Assignment1.PDF
Survey of Programming Language Assignment1.PDF
Programming languages, like human languages, have rules governing their structure and
meaning. These rules are classified into syntax and semantics, which together define how
programs are written and interpreted.
Syntax
Syntax refers to the formal rules that specify the structure of valid statements in a programming
language. It determines how keywords, operators, symbols, and expressions must be arranged
to form correctly structured programs.
Types of Syntax
• Example: In Python, variable names cannot start with a number (1var is invalid).
• Concrete Syntax: Specifies how tokens are combined to form valid expressions
and statements.
if (x > 0) {
System.out.println("Positive");
Syntax Errors
A syntax error occurs when code violates the formal grammar of the language.
Example (Python):
Semantics defines the meaning of syntactically correct statements. It ensures that the program
behaves as intended.
Types of Semantics
1. Static Semantics: Rules that are checked before execution, such as type checking.
● Example: In Java, assigning a string to an int variable causes an error:
2. Dynamic Semantics: Describes how statements execute and affect the program’s state.
● Example: In Python, x = x + 1 updates the value of x dynamically at runtime.
Semantic Errors
Semantic errors occur when a statement is syntactically correct but does not produce the
expected result.
Example (Python):
Conclusion
Both syntax and semantics are crucial in programming. While syntax ensures that code follows
formal rules, semantics ensures that the code performs meaningful operations. A well-formed
program must be both syntactically correct and semantically sound to execute successfully.