Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Survey of Programming Language Assignment1.PDF

The document explains the concepts of syntax and semantics in programming languages, highlighting their roles in defining the structure and meaning of code. It details types of syntax (lexical, concrete, abstract) and semantics (static, dynamic), along with examples and error types. The conclusion emphasizes the importance of both syntax and semantics for successful program execution.

Uploaded by

demiladeoso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Survey of Programming Language Assignment1.PDF

The document explains the concepts of syntax and semantics in programming languages, highlighting their roles in defining the structure and meaning of code. It details types of syntax (lexical, concrete, abstract) and semantics (static, dynamic), along with examples and error types. The conclusion emphasizes the importance of both syntax and semantics for successful program execution.

Uploaded by

demiladeoso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME: OSO DEMILADE GOODNESS

MATRIC NUMBER: 22/PTS/SCI01/017

Syntax and Semantics in Programming Languages

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

• Lexical Syntax: Defines the valid characters, tokens, and identifiers in a


language.

• 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.

• Example: In Java, an if statement must have parentheses and curly braces:

if (x > 0) {

System.out.println("Positive");

• Abstract Syntax: Represents the essential structure of a program without


unnecessary details like punctuation or formatting. It is used in compilers for parsing code.

Syntax Errors

A syntax error occurs when code violates the formal grammar of the language.

Example (Python):

print "Hello" # Syntax error: Missing parentheses


Semantics

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:

int num = "hello"; // Type 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):

x = 10 / 0 # Runtime error: Division by zero

Syntax vs. Semantics: Key Differences

Feature Syntax Semantics

Definition Structure and rules of a Meaning and behavior of


language code

Checked By Compiler or interpreter Compiler (static) and runtime


(dynamic)

Error Type Syntax errors (missing Semantic errors (type


brackets, incorrect keywords) mismatches, logic issues)

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.

You might also like