Chapter 02
Chapter 02
a production
<NP> ::= <A> <N>
tokens
Karadeniz Teknik Üniversitesi Chapter Two Programming Languages 11
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
Karadeniz Teknik Üniversitesi Chapter Two Programming Languages 12
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 symbols on the right-
hand side, in order, as its children in a parse tree
<exp>
<exp> + <exp>
a <exp> * <exp>
b c
( <exp> ) c
<exp> + <exp>
a b
a. a+b
b. a*b+c
c. (a+b)*c
a. a+b
b. a*b+c
c. (a+b)*c
Karadeniz Teknik Üniversitesi Chapter Two Programming Languages 22
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
Karadeniz Teknik Üniversitesi Chapter Two Programming Languages 23
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...
float a;
boolean a,b,c;
int a=1, b, c=1+2;
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<unsigned> ::= <digit>+
<signed> ::= (+|-)<unsigned>
if-stmt
if expr then stmt else stmt
if-stmt
if expr then stmt else stmt
exp * exp
exp ( exp )
exp
addend
DoStatement:
do Statement while ( Expression ) ;
ForStatement:
for ( ForInitopt ; Expressionopt ; ForUpdateopt)
Statement