02 - Defining Program Syntax Click to edit Master subtitle...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter Two Modern Programming Languages, 2nd ed. Defining Program Syntax 11
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter Two Modern Programming Languages, 2nd ed. Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind of formal grammar Programming language semantics: what programs do, their behavior and meaning Semantics is harder to define—more on this in Chapter 23 22
Background image of page 2
Chapter Two Modern Programming Languages, 2nd ed. Outline Grammar and parse tree examples BNF and parse tree definitions Constructing grammars Phrase structure and lexical structure Other grammar forms 33
Background image of page 3

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

View Full DocumentRight Arrow Icon
Chapter Two Modern Programming Languages, 2nd ed. An English Grammar 44 A sentence is a noun phrase, a verb, and a noun phrase. A noun phrase is an article and a noun. A verb is… An article is… A noun is. .. < S > ::= < NP > < V > < NP > < NP > ::= < A > < N > < V > ::= loves | hates | eats < A > ::= a | the < N > ::= dog | cat | rat
Background image of page 4
Chapter Two Modern Programming Languages, 2nd ed. How The Grammar Works The grammar is a set of rules that say how to build a tree—a parse tree You put < S > at the root of the tree The grammar’s rules say how children can be added at any point in the tree For instance, the rule says you can add nodes < NP >, < V >, and < NP >, in that order, as children of < S > 55 < S > ::= < NP > < V > < NP >
Background image of page 5

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

View Full DocumentRight Arrow Icon
Chapter Two Modern Programming Languages, 2nd ed. A Parse Tree 66 < S > < NP > < V > < NP > < A > < N > < A > < N > the dog the cat loves
Background image of page 6
Chapter Two Modern Programming Languages, 2nd ed. A Programming Language Grammar An expression can be the sum of two expressions, or the product of two expressions, or a parenthesized subexpression Or it can be one of the variables a , b or c 77 < exp > ::= < exp > + < exp > | < exp > * < exp > | ( < exp > ) | a | b | c
Background image of page 7

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

View Full DocumentRight Arrow Icon
Chapter Two Modern Programming Languages, 2nd ed. A Parse Tree 88 < exp > < exp > + < exp > ( < exp > ) < exp > * < exp > ( < exp > ) a b ((a+b)*c) c
Background image of page 8
Chapter Two Modern Programming Languages, 2nd ed. Outline Grammar and parse tree examples BNF and parse tree definitions Constructing grammars Phrase structure and lexical structure Other grammar forms 99
Background image of page 9

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

View Full DocumentRight Arrow Icon
Modern Programming Languages, 2nd ed. 1010 < S > ::= < NP > < V > < NP > < NP > ::= < A > < N > < V > ::= loves | hates | eats < A > ::= a | the < N > ::= dog | cat | rat tokens start symbol a production
Background image of page 10
Chapter Two Modern Programming Languages, 2nd ed. BNF Grammar Definition A BNF grammar consists of four parts: The set of tokens The set of non-terminal symbols The start symbol The set of productions 1111
Background image of page 11

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

View Full DocumentRight Arrow Icon
Chapter Two Modern Programming Languages, 2nd ed. Definition, Continued The tokens are the smallest units of syntax Strings of one or more characters of program text They are atomic: not treated as being composed from smaller parts The non-terminal symbols stand for larger pieces of syntax They are strings enclosed in angle brackets, as in < NP > They are not strings that occur literally in program text The grammar says how they can be expanded into strings of tokens The start symbol is the particular non-terminal that forms the root of any parse tree for the grammar 1212
Background image of page 12
Chapter Two Modern Programming Languages, 2nd ed.
Background image of page 13

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

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

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 48

02 - Defining Program Syntax Click to edit Master subtitle...

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

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