MODERN PROGRAMMING LANGUAGES A.Webber Cap2

MODERN PROGRAMMING LANGUAGES A.Webber Cap2 - Chapter 2...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 2 Defining Program Syntax 10 2.1 Introduction The simplest questions about a programming language are often questions of form. What does an expression, a state- ment, or a function definition look like? How are comments delimited? Where do the semicolons go? Questions like these are questions of programming-language syntax . The syntax of a programming language is the part of the language definition that says how programs look: their form and structure. The more difficult questions are often questions of behav- ior. What does a given expression, statement, or function do? How does it work? What can go wrong when it runs? Questions like these are questions of programming- language semantics . The semantics of a programming lan- guage is the part of the language defini- tion that says what programs do: their behavior and meaning.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This chapter introduces the formal grammar used to define programming- language syntax. (One of the formal techniques for defining programming-lan- guage semantics is introduced in Chapter 23.) By the end of the chapter, you should be able to understand these grammars and to write simple ones for yourself. 2.2 A Grammar Example for English Let’s start with an example of a grammar for a familiar language: English. An article can be the word a or the . We use the symbol < A > for article and express our definition this way: < A > ::= a | the A noun can be the word dog , cat , or rat : < N > ::= dog | cat | rat noun phrase is an article followed by a noun: < NP > ::= < A > < N > verb can be the word loves , hates , or eats : < V > ::= loves | hates | eats sentence is a noun phrase, followed by a verb, followed by another noun phrase: < S > ::= < NP > < V > < NP > Put them all together and you have a grammar that defines a small subset of unpunctuated English: < S > ::= < NP > < V > < NP > < NP > ::= < A > < N > < V > ::= loves | hates | eats < N > ::= dog | cat | rat < A > ::= a | the How does such a grammar define a language? Think of the grammar as a set of rules that say how to build a tree. Put < S > at the root of the tree, and the grammar tells how children can be added at any point (node) in the tree. Such a tree is called a parse tree. It is a convention that parse trees are drawn growing downward, with the root at the top, like this: 11 2.2—A Grammar Example for English
Background image of page 2
12 Chapter 2—Defining Program Syntax < S > < V > loves the dog the cat < NP > < NP > < N > < A >< N > < A > The children of each node in a parse tree must follow the forms specifically allowed by the grammar. For example, an < NP > node must have the children < A > and < N > , because the grammar includes only one rule for < NP > , namely < NP > ::= < A > < N > . Check the tree above; you will see that every node with a child or children follows one of the rules of the grammar. By reading the fringe of the tree from left to right, you get a sentence in the language defined by the grammar. For the parse tree above, it is the sentence “ the dog loves the cat .” Here is a different parse tree following the same grammar.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/12/2010 for the course ITC TC2006 taught by Professor Conant during the Spring '10 term at ITESM.

Page1 / 16

MODERN PROGRAMMING LANGUAGES A.Webber Cap2 - Chapter 2...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online