Topic 2 - Syntax and Semantics Lecture Notes
Topic 2 - Syntax and Semantics Lecture Notes
Semantics
IS 214 - Principles of Programming Languages - 1st Sem AY
2020-2021
Based on Asst. Prof. Reinald Adrian Pugoy’s lecture notes on Syntax and Semantics
Objectives
At the end of this chapter, you should be able to:
• Describe syntax by generating derivations given a certain grammar.
• Examine issues involving syntax.
• Describe semantics using some informal methods.
Syntax and Semantics
• Syntax
– “What its program looks like.”
– Refers to ways symbols may be combined to create well-formed sentences in the language.
• Semantics
– “What its program means.”
– Much more difficult to describe.
– Informal methods and suggestive examples are normally used.
Syntax
Specifying Syntax
• Language consists of a set of strings (syntactically correct programs) of characters from some
alphabet of symbols.
• Strings of a language are called sentences or
statements.
• Syntax rules specify which strings of characters from the language’s alphabet are in the language.
Specifying Syntax
• Grammar
– Formal definition of the syntax of the language.
– It naturally defines the hierarchical structure of many programming languages.
Sample English Grammar
• subject / verb / object
– The girl / ran / home.
– The boy / cooks / dinner.
• article / noun / verb / object
• auxiliary verb / subject / predicate
– Did / the girl / run home?
– Is / the boy / cooking dinner ?
Sample English Grammar
• Rules
– <sentence> := <declarative> | <interrogative>
– <declarative> := <subject><verb><object>
– <subject> := <article><noun>
– <interrogative>:=<auxillaryverb><subject><predicate>
Grammar
• Definition: A grammar < ∑, N, P, S> consists of
four parts:
<B>
0 <B>
1 <B>
0
Example
• Consider this sample PL grammar:
– <expression> ::= <term> |
<expression><addoperator><term>
– <term> ::= <factor> | <term><multoperator><factor>
– <factor> ::= <identifier> | <literal> | (<expression>)
– <identifier> ::= a | b | c | … | z
– <literal> ::= 0 | 1 | 2 | … | 9
– <addoperator> ::= + | - | or
– <multoperator> ::= * | / | div | mod | and
Left Recursive
• A BNF rule has its left hand side (LHS) appear at the beginning of its RHS.
Right Associativity
<assign> ::= <id> = <expr>
<id> ::= A | B | C
<expr> ::= <expr> + <term> | <term>
<term> ::= <term> * <factor> | <factor>
<factor> ::= (<expr>) | <id>
Right Recursive
• A BNF rule has its left hand side (LHS) appear right at the end of its RHS.
Operator Associativity
• In addition and multiplication, it does not matter what kind of associativity to be used. Why?
• But in other operations, like exponentiation, the kind of associativity should be defined.
• In most PL’s, the exponentiation operator is right
associative.
Syntax Diagrams
Syntax Diagrams
• A graphical way used to represent BNF rules.
• For each grammar rule, an equivalent syntax diagram can be drawn.
• This was popularized in the design of Pascal.
• Symbols
– Rectangle nodes for non-terminals.
– Circles for terminals.
Syntax Diagrams
<expression>
<term>
• This grammar is ambiguous. The ambiguity can be illustrated by this simple statement:
if <logic_expr> then if <logic_expr> then
<stmt> else <stmt>
if-then-else Grammar
• The general rule
– “Match each else with the closest previous unmatched then.”
if-then-else Grammar
• Take note of the following:
– Between a then and its matching else, a statement must be matched (i.e. an if
statement with an else).
– An unmatched statement is an else-less if.
– A matched statement is either an if-then-else statement containing no unmatched
statements or any other non-conditional statement.
if-then-else Grammar
• The problem? The given grammar treats all statements as if they were all matched.
if-then-else Grammar
• To reflect the different categories, grammar has to be rewritten:
<stmt> ::= <matched> | <unmatched>
<matched> ::= if <logic_expr> then <matched> else <matched> | any non-if statement
<unmatched> ::= if <logic_expr> then <stmt> |
if <logic_expr> then <matched> else <unmatched>