0% found this document useful (0 votes)
97 views13 pages

Language Translational Issues: CPT 211 - Programming Language Concepts & Paradigms

This document discusses attribute grammars, which extend context-free grammars to allow the representation of static semantic rules of programming languages. Attribute grammars associate attributes, such as type information, with grammar symbols. Semantic rules compute the values of attributes using semantic functions. For example, an attribute grammar can check that the types are compatible in a simple assignment statement by computing the expected and actual types of expressions and variables.

Uploaded by

Ahmad Mohamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views13 pages

Language Translational Issues: CPT 211 - Programming Language Concepts & Paradigms

This document discusses attribute grammars, which extend context-free grammars to allow the representation of static semantic rules of programming languages. Attribute grammars associate attributes, such as type information, with grammar symbols. Semantic rules compute the values of attributes using semantic functions. For example, an attribute grammar can check that the types are compatible in a simple assignment statement by computing the expected and actual types of expressions and variables.

Uploaded by

Ahmad Mohamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CPT 211 –

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.

 Therefore, the need for static semantic rules


 Static semantics: legal form of syntax – type constraints
 Dynamic semantics: the meaning of expression,
statements and program unit.

 The additions are:


 Attributes (terminal and non-terminal symbols)
 attribute computation functions (Semantic functions: how
attribute values are computed)
 predicate functions (the static semantic rules of the
language)
Attribute Grammars

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))

A predicate function has the form of a Boolean


expression on the union of the attribute set
{A(X0), …, A(Xn)} 3-6
Attribute Grammars

Example
 Syntax rule:

<proc_def> → procedure <proc_name>[1]


<proc_body> end <proc_name>[2];
 Predicate:

<proc_name>[1].string == <proc_name>[2].string

 I.e. the predicate rule states that the name string


attribute of the <proc_name> nonterminal in the
subprogram header must match the name string
attribute of the <proc_name> nonterminal
following the end of the subprogram.
1-7
1-8 Attribute Grammars

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

Syntax – parse tree


<assign>

<expr>

<var> <var>[2] <var>[3]

A = A + B
1-10
Attribute Grammars

Computing Attribute Values


 If all attributes were inherited, the tree could be
decorated in top-down order.
 If all attributes were synthesized, the tree could be
decorated in bottom-up order.
 In many cases, both kinds of attributes are used,
and it is some combination of top-down and
bottom-up orders. E.g.:
1. <var>.actual_type  look-up(A) (Rule 4)
2. <expr>.expected_type  <var>.actual_type (Rule 1)
3. <var>[2].actual_type  look-up(A) (Rule 4)
<var>[3].actual_type  look-up(B) (Rule 4)
4. <expr>.actual_type  either int or real (Rule 2)
5. <expr>.expected_type == <expr>.actual_type is either TRUE or
FALSE (Rule 2) 1-11
Attribute Grammars

Computing Attribute Values


<assign>

expected_type actual_type
<expr>

<var> actual_type <var>[2] actual_type <var>[3] actual_type

A = A + B
1-12
Attribute Grammars

Computing Attribute Values


<assign>

expected_type actual_type expected_type=real_type


<expr> actual_type= real_type

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

You might also like