Language Translational Issues: CPT 211 - Programming Language Concepts & Paradigms
Language Translational Issues: CPT 211 - Programming Language Concepts & Paradigms
Programming
Language Concepts
& Paradigms
Language
Translational
Issues
By Dr Hadeel Saleh Haj Aliwi
Extended BNF
Improves readability and writability of BNF
Three common extensions are:
1. Optional parts of an RHS, delimited by square
brackets. E.g.
<if_stmt> → if (<exp>) <stmt> [else <stmt>]
2. The use of curly braces in an RHS to indicate that
the enclosed part can be repeated indefinitely
or left out altogether.
<ident_list> → <identifier> {, <identifier>}
3. For multiple choice options, the options are
placed inside parentheses and separated by the
OR operator.
<term> → <term> (*|/|%) <factor>
BNF vs EBNF
BNF EBNF
<expr> <expr> + <term>
Try this out
| <expr> - <term>
| <term>
<term> <term> * <factor>
| <term> / <factor>
| <factor>
<factor> <exp> ** <factor>
| <exp>
<exp> ( <expr> )
| id
Attribute Grammars
Semantic analyser:
Attribute grammar
An extension to CFG that allows some characteristics of
the structure of programming languages that are either
difficult or impossible to be described using BNF.
Definition
Associated with each grammar symbol X is a
set of attributes A(X).
The set A(X) consists of two disjoint sets,
S(X), synthesized attributes, used to pass
semantic information up a parse tree and
I(X), inherited attributes, used to pass semantic
information down and across a tree.
1-5
Attribute Grammars
Definition
For a rule X0 X1 ... Xn, the synthesised attributes of
X0 are computed with semantic functions of the
form:
S(X0) = f(A(X1), ... , A(Xn))
Likewise, inherited attributes of symbols Xj, 1 ≤ j ≤ n
are computed with a semantic function of the
form:
I(Xj) = f(A(X0), ... , A(Xn))
Example
Syntax rule:
<proc_name>[1].string == <proc_name>[2].string
Syntax vs Semantics
Syntax Semantics
<assign> → <var> = <expr> actual_type
<expr> → <var> + <var> Synthesized attribute
associated with
|<var>
nonterminal <var>
<var> → A|B|C and <expr>
Int or real?
Expected type
Inherited attribute
associated with
nonterminal <expr>
Type determined by
the type of variable
on the left side of
assignment statement
How attribute grammar check the
type rules of a simple assignment
statement
Attribute Grammars
<expr>
A = A + B
1-10
Attribute Grammars
expected_type actual_type
<expr>
A = A + B
1-12
Attribute Grammars
actual_type=
real_type <var> actual_type=
real_type <var>[2] actual_type <var>[3] actual_type
actual_type=
int_type
A = A + B
1-13