pl10ch3
pl10ch3
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 (grammar, BNF.. )
• Context-Free Grammars
– Developed by Noam Chomsky in the mid-1950s
– Language generators (classes), meant to
describe the syntax of natural languages
– Define a class of languages called context-free
languages
<program> <stmts>
<stmts> <stmt> | <stmt> ; <stmts>
<stmt> <var> = <expr>
<var> a | b | c | d
<expr> <term> + <term> | <term> - <term>
<term> <var> | const
• A hierarchical
representation of
a derivation <program>
<stmts>
<stmt>
<var> = <expr>
a <term> + <term>
<var> const
b
Copyright © 2012 Addison-Wesley. All rights reserved. 1-17
An Example Parse Tree
An example
grammar and the
hierarchical
representation of a
derivation
<expr> <expr>
A=B+C*A
<expr>
<expr>
<expr> + const
<expr> + const
const
Copyright © 2012 Addison-Wesley. All rights reserved. 1-23
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