0% found this document useful (0 votes)
15 views

Pl12ch3 Part 2

Uploaded by

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

Pl12ch3 Part 2

Uploaded by

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

Chapter 3 Topics

• Introduction
• The General Problem of Describing Syntax
• Formal Methods of Describing Syntax
• Attribute Grammars
• Describing the Meanings of Programs:
Dynamic Semantics

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


Associativity of Operators

• Operator associativity can also be indicated by a


grammar

<expr> -> <expr> + <expr> | const (ambiguous)


<expr> -> <expr> + const | const (unambiguous)

<expr>
<expr>

<expr> + const

<expr> + const

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

• Optional parts are placed in brackets [ ]


<proc_call> -> ident [(<expr_list>)]
• Alternative parts of RHSs are placed
inside parentheses and separated via
vertical bars
<term> → <term> (+|-) const
• Repetitions (0 or more) are placed inside
braces { }
<ident> → letter {letter|digit}

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


BNF and EBNF

• BNF
<expr> ® <expr> + <term>
| <expr> - <term>
| <term>
<term> ® <term> * <factor>
| <term> / <factor>
| <factor>
• EBNF
<expr> ® <term> {(+ | -) <term>}
<term> ® <factor> {(* | /) <factor>}

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


Recent Variations in EBNF

• Alternative RHSs are put on separate lines


• Use of a colon instead of =>
• Use of opt for optional parts
• Use of oneof for choices

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


Static Semantics

• Nothing to do with meaning


• Context-free grammars (CFGs) cannot
describe all of the syntax of programming
languages
• Categories of constructs that are trouble:
- Context-free, but cumbersome (e.g.,
types of operands in expressions)
- Non-context-free (e.g., variables must
be declared before they are used)

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


Attribute Grammars

• Attribute grammars (AGs) have additions


to CFGs to carry some semantic info on
parse tree nodes

• Primary value of AGs:


– Static semantics specification
– Compiler design (static semantics checking)

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


Attribute Grammars : Definition

• Def: An attribute grammar is a context-free


grammar G = (S, N, T, P) with the following
additions:
– For each grammar symbol x there is a set A(x)
of attribute values
– Each rule has a set of functions that define
certain attributes of the nonterminals in the rule
– Each rule has a (possibly empty) set of
predicates to check for attribute consistency

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


Attribute Grammars: Definition

• Let X0 ® X1 ... Xn be a rule


• Functions of the form S(X0) = f(A(X1), ... ,
A(Xn)) define synthesized attributes
• Functions of the form I(Xj) = f(A(X0), ... ,
A(Xn)), for i <= j <= n, define inherited
attributes
• Initially, there are intrinsic attributes on the
leaves

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


Attribute Grammars: An Example

• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var>
and <expr>
• expected_type: inherited for <expr>

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


Attribute Grammar (continued)

• Syntax rule: <expr> ® <var>[1] + <var>[2]


Semantic rules:
<expr>.actual_type ¬ <var>[1].actual_type
Predicate:
<var>[1].actual_type == <var>[2].actual_type
<expr>.expected_type == <expr>.actual_type

• Syntax rule: <var> ® id


Semantic rule:
<var>.actual_type ¬ lookup (<var>.string)

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


Attribute Grammars (continued)

• How are attribute values computed?


– 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 that must be used.

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


Attribute Grammars (continued)

<expr>.expected_type ¬ inherited from parent

<var>[1].actual_type ¬ lookup (A)


<var>[2].actual_type ¬ lookup (B)
<var>[1].actual_type =? <var>[2].actual_type

<expr>.actual_type ¬ <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type

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

You might also like