You are on page 1of 21

TWELFTH EDITION

GLOBAL EDITION

Chapter 3

Describing Syntax
and Semantics

Copyright © 2023 Pearson Education Ltd. All Rights Reserved.


Chapter 3 Topics

• Formal Methods of Describing Syntax


(Grammar) Using BNF
• Attribute Grammars
• Describing the Meanings of Programs:
Dynamic Semantics

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-2


Introduction

• Syntax: the form or structure of the


expressions, statements, and program
units
• Semantics: the meaning of the expressions,
statements, and program units
• Syntax and semantics provide a language’s
definition
– Users of a language definition
• Other language designers
• Implementers
• Programmers (the users of the language)

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-3


BNF – A Formal Notation

• Backus-Naur Form (1959)


– Invented by John Backus to describe the syntax
of Algol 58
• A formal notation widely used to specify
the syntax of a programming language

• BNF is a metalanguage for programming


languages

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-6


BNF Fundamentals

• In BNF, abstractions are used to represent syntactic


structures--they act like syntactic variables (also
called nonterminal symbols, or just nonterminals)

– Example: <assignment> -> <var> = <expression>;

mark = 2 + 3 + final;
• Terminals are lexemes or tokens
• A rule has a left-hand side (LHS), which is a
nonterminal, and a right-hand side (RHS), which is
a string of terminals and/or nonterminals

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-7


BNF Fundamentals (continued)

• Nonterminals are often enclosed in angle brackets

– Examples of BNF rules:


<ident_list> → identifier | identifier, <ident_list>
<if_stmt> → if <logic_expr> then <stmt>

• Grammar: a finite non-empty set of rules

• A start symbol is a special element of the


nonterminals of a grammar

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-8


BNF Rules

• An abstraction (or nonterminal symbol)


can have more than one RHS
<stmt> ® <single_stmt>
The Left-Hand- | begin <stmt_list> end
Side
The Right-Hand-Side

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-9


Describing Lists

• Syntactic lists are described using


recursion
<ident_list> ® ident
| ident, <ident_list>

• A derivation is a repeated application of


rules, starting with the start symbol and
ending with a sentence (all terminal
symbols)
• A rule is recursive if its LHS appears in its
RHS.
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-10
An Example Grammar

<program> ® <stmts>
<stmts> ® <stmt> | <stmt> ; <stmts>
<stmt> ® <var> = <expr>
<var> ® a | b | c | d
<expr> ® <term> + <term> | <term> - <term>
<term> ® <var> | const

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-11


An Example Derivation
Each of the strings in the
derivation, including
<program> ® <stmts> <program>, is called a
sentential form.
<stmts> ® <stmt> | <stmt> ; <stmts>
<stmt> ® <var> = <expr> The symbol => reads
<var> ® a | b | c | d “derives”.
<expr> ® <term> + <term> | <term> - <term> Derivations that use this
order of replacement are
<term> ® <var> | const called leftmost
derivations.
<program> => <stmts> => <stmt>
=> <var> = <expr>
=> a = <expr>
The leftmost derivation of this => a = <term> + <term>
grammar has generated a valid
sentence of the language => a = <var> + <term>
=> a = b + <term>
=> a = b + const

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-12


Derivations
• Each of the string in a derivation is a
sentential form
• A sentence is a sentential form that has
only terminal symbols
• A leftmost derivation is one in which the
leftmost nonterminal in each sentential
form is the one that is expanded
• A derivation may be either leftmost or
rightmost, or none of these.
• The order has no effect on the language
generated by a grammar.
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-13
Derivations (continued)

• By choosing alternative RHSs of rules with


which to replace nonterminals in the
derivation, different sentences in the
language can be generated

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-14


Parse Tree

• A hierarchical representation of a derivation


<program>

<stmts>

<stmt>

<var> = <expr>

a <term> + <term>

<var> const

b
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-15
Ambiguity in Grammars

• A grammar is ambiguous if and only if it


generates a sentential form that has two
or more distinct parse trees

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-16


Ambiguity in Grammars (continued)

The grammar is ambiguous


because the sentence

A=B+C*A

has two distinct parse trees

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-17


Ambiguity in Grammars (continued)

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-18


Ambiguity in Grammars (continued)

The grammar is ambiguous


because the abstraction
<expr> can be replaced by
any of the two rules.

Consequently, a ‘+’ can be


generated lower in the parse
tree => it will be evaluated by
the compiler before the
multiplication operator ‘*’
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-19
Specifying Precedence in Grammars

• The correct ordering is specified by using separate


nonterminal symbols
• Representing the precedence of different operators
• additional nonterminals and some new rules
required
• This would allow the grammar to force different
operators to different levels in the parse tree

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-20


Specifying Precedence in Grammars
(continued)
• If we use the parse tree to indicate
precedence levels of the operators, we
won't have ambiguity
<expr> ® <expr> + <term> | <term>
<term> ® <term> / <id> | <id>

<expr>

<expr> + <term>

<term> <term> / id

id id
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-21
Specifying Precedence in Grammars
(continued)

<expr> ® <expr> + <term> | <term>


<term> ® <term> / id | id

<expr> => <term> <expr> => <term>


=> <id> => <term> / <id>
=> A => <id> / <id>
=> A / <id>
=> A / B
<expr> => <term>
=> <term> / <id>
=> <term> / <id> / <id>
=> <term> / <id> / <id> / <id>

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-22


Specifying Precedence in Grammars
(continued)

<expr> ® <expr> + <term> | <term>


<term> ® <term> / id | id

<expr> => <expr> + <term> <expr> => <expr> + <term>


=> <term> + <term> => <term> + <term>
=> <id> + <term> => <term> / <id> + <term>
=> A + <term> => <id> / <id> + <term>
=> A + <id> => A / <id> + <term>
=> A + B => A / B + <term>
=> A / B + <id>
=> A / B + C

Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-23

You might also like