SEN308 Lecture 3 1
SEN308 Lecture 3 1
.
• The Building Blocks of Programming Language
• Syntax which defines the form and structure of codes
• semantics which gives meaning to that structure
.
• Syntax defines the rules that determine what is considered valid code in a language.
• It encompasses the lexicon (keywords, symbols) and how they can be combined.
• Key Elements of Syntax
• Keywords: Reserved words with special meanings (e.g., "if", "for", "while")
• Identifiers: Programmer-chosen names for variables, functions, etc.
• Operators: Symbols for calculations, comparisons, logic (+, -, *, >, etc.)
• Punctuation: Semicolons, commas, braces, etc., to separate elements.
• Program Structure: How code is organised into functions, modules, etc.
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Syntax Errors
.
Semantics determines the meaning and behaviour of syntactically correct code.
It tells the computer how to execute the instructions.
Focuses on logic, computations, and intended results.
Types of Semantic
• Operational Semantic
• Operational semantics describes how code executes.
• Explains how a program's state changes as each instruction is carried out.
• Involves concepts like variables, memory, and control flow.
• Denotational Semantics
.
• Uses mathematical models to describe program meaning.
• Maps code to mathematical objects and functions.
• Helps prove correctness and reason about program behaviour
The General Problem of Describing Syntax
.
• Token: A category of lexemes (e.g., "keyword", "identifier", "operator").
• Recognizer:
• A recognizer is a theoretical machine, or a more practical program, that can
determine if a given string of characters belongs to a specific language.
• It acts as a gatekeeper – if it accepts the code, the code is syntactically correct
according to the rules.
• Recognizers are fundamental parts of compilers and interpreters
• Generator:
• A device that generates sentences of a language
• One can determine if the syntax of a particular sentence is syntactically correct
Formal Method of Describing Syntax
• Context-free grammars (CFGs): The most common way to define syntax,
using rules to describe how to build valid sentences in the language.
• Developed by Noam Chomsky in the mid-1950s
• Language generator meant to describe the syntax of natural languages
• Define a class of languages called context-free languages
.
• Regular Expressions: Used to define patterns for matching text (like finding all
phone numbers in a document).
.
• Multiple definitions can be written as a single rule, with the different definitions
separated by the symbol |, meaning logical OR
• A derivation is a repeated application of rules, starting with the start symbols and
ending with a sentence (all terminal symbols)
Grammars and Derivations
• A derivation is a step-by-step process of starting with a grammar's start symbol (like
<program> in our example) and applying production rules to generate a valid sentence
in the language (i.e., a concrete piece of code).
• Example Derivation
• Start with: <program>
.
• Apply rule: <program> ::= <program> <statement>
• Now we have: <program> <statement>
• Apply rule to <statement>: <program> <if_stmt>
• Apply rule to <if_stmt> : <program> "if" "(" <expression> ")" <statement>
• Focus on <expression>: <program> "if" "(" <identifier> ")" <statement>
• Apply rule to <identifier>: <program> "if" "(" "x" ")" <statement> ... Continue until
only terminal symbols remain
• Final Result of Derivation: if (x) y = 20;
An Example Grammar (2)
• <program> begin <stmt_list> end
• <stmt_list> <stmt>
| <stmt>; <stmt_list>
• <var> . A|B|C
if
/ \
<condition> <statement>
/\
x > 5
. \
print
\
<expression>
|
x