pl10ch3 Update
pl10ch3 Update
pl10ch3 Update
Describing Syntax
and Semantics
Chapter 3 Topics
• Introduction
• The General Problem of Describing Syntax
• Formal Methods of Describing Syntax
• Attribute Grammars
• Describing the Meanings of Programs:
Dynamic Semantics
• Recognizers
– A recognition device reads input strings over the alphabet
of the language and decides whether the input strings
belong to the language
– Example: syntax analysis part of a compiler
- Detailed discussion of syntax analysis appears in
Chapter 4
• Generators
– A device that generates sentences of a language
– One can determine if the syntax of a particular sentence
is syntactically correct by comparing it to the structure of
the generator
• Context-Free Grammars
– Developed by Noam Chomsky in the mid-1950s
– Language generators, meant to describe the
syntax of natural languages
– Define a class of languages called context-free
languages
<stmts>
<stmt>
<var> = <expr>
a <term> + <term>
<var> const
b
Copyright © 2012 Addison-Wesley. All rights reserved. 1-17
Ambiguity in Grammars
<expr> <expr>
<expr>
<expr> - <term>
const const
Copyright © 2012 Addison-Wesley. All rights reserved. 1-20
Associativity of Operators
<expr>
<expr>
<expr> + const
<expr> + const
const
Copyright © 2012 Addison-Wesley. All rights reserved. 1-21
Extended BNF
• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var>
and <expr>
• expected_type: inherited for <expr>
<expr>.actual_type <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type