Download Defining Program Syntax - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!
Defining Program Syntax
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 1
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
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 2
An English Grammar
Chapter Two Modern Programming Languages, 2nd ed. 4
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
Docsity.com
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 >
Chapter Two Modern Programming Languages, 2nd ed. 5
< S > ::= < NP > < V > < NP >
Docsity.com
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
Chapter Two Modern Programming Languages, 2nd ed. 7
< exp > ::= < exp > + < exp > | < exp > ***** < exp > | ( < exp > ) | a | b | c
Docsity.com
A Parse Tree
Chapter Two Modern Programming Languages, 2nd ed. 8
< exp >
< exp > + < exp >
( < exp > )
< exp > ***** < exp >
( < exp > )
a b
((a+b)c)*
c
Docsity.com
Chapter Two Modern Programming Languages, 2nd ed. 10
< S > ::= < NP > < V > < NP >
< NP > ::= < A > < N >
< V > ::= loves | hates | eats
< A > ::= a | the
< N > ::= dog | cat | rat
tokens
non-terminal symbols
start symbol
a production
Docsity.com
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
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 11
Definition, Continued
The productions are the tree-building rules
Each one has a left-hand side, the separator ::= ,
and a right-hand side
- The left-hand side is a single non-terminal
- The right-hand side is a sequence of one or more things, each of which can be either a token or a non-terminal
A production gives one possible way of building a
parse tree: it permits the non-terminal symbol on
the left-hand side to have the things on the right-
hand side, in order, as its children in a parse tree
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 13
Alternatives
When there is more than one production
with the same left-hand side, an abbreviated
form can be used
The BNF grammar can give the left-hand
side, the separator ::= , and then a list of
possible right-hand sides separated by the
special symbol |
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 14
Empty
The special nonterminal < empty > is for
places where you want the grammar to
generate nothing
For example, this grammar defines a typical
if-then construct with an optional else part:
Chapter Two Modern Programming Languages, 2nd ed. 16
< if-stmt > ::= if < expr > then < stmt > < else-part > < else-part > ::= else < stmt > | < empty >
Docsity.com
Parse Trees
To build a parse tree, put the start symbol at
the root
Add children to every non-terminal,
following any one of the productions for
that non-terminal in the grammar
Done when all the leaves are tokens
Read off leaves from left to right—that is
the string derived by the tree
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 17
Compiler Note
What we just did is parsing : trying to find a
parse tree for a given string
That’s what compilers do for every program
you try to compile: try to build a parse tree
for your program, using the grammar for
whatever language you used
Take a course in compiler construction to
learn about algorithms for doing this
efficiently
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 19
Language Definition
We use grammars to define the syntax of
programming languages
The language defined by a grammar is the
set of all strings that can be derived by some
parse tree for the grammar
As in the previous example, that set is often
infinite (though grammars are finite)
Constructing grammars is a little like
programming...
Chapter Two Modern Programming Languages, 2nd ed. (^) Docsity.com 20