Sebesta's Chapter 3.
- Syntax: a set of rules that determine what is a legal
string---which strings belong to the language, which don't
- Semantics: a set of rules that associate a meaning
with each legal string of the language.
- Syntactic structure is defined by:
- An alphabet (what are legal characters)
- Lexical structure: formation of strings
of legal characters into lexical units (legal tokens)
- Parse structure: formation of sequences of
legal tokens into grammatical units (legal constructs)
- Type checking might be considered part of
syntactic structure rules, but most authors call it part
of ``static semantics''
-
These notes are based in part on material in
Chapter 10 of R. Sethi, Programming Languages: Concepts
and Constructs
- Main part of syntax rules of a programming language is usually
described with a context-free grammar (see book
for definition), also called a Backus-Naur Form (BNF) grammar
- Other, non-context-free rules are required: no practical
programming language has a syntax that can be fully described without
context-sensitive rules
- Example:
- The nonterminals are E, T, and F.
- The terminals are +, -, *, /
and (the lexical classes) number and identifier
- Any of the nonterminals could be chosen as the starting
nonterminal
- There might be additional rules about identifiers
having to be declared---these are not context-free rules
- A parse tree for a context-free grammar is a tree satisfying
the following:
- Each leaf is labeled with a terminal.
- The label of a nonleaf node must be the label of
a nonleaf production and the labels of the children of the node,
from left to right, must form the right side of that production.
- The root is labeled with the starting nonterminal.
- A parse tree generates the string formed by reading
the terminals at its leaves from left to right.
- The construction of a parse tree from a string is called
parsing.
- Additional syntax rule: match an else
with the nearest unmatched if.
- Which of the two parse trees does this rule select?
- Pictorial technique, alternative to BNF grammars.
- See Chapter 26 of Ullman for syntax diagrams for ML.
raju.cs.rpi.edu% sml
Standard ML of New Jersey, Version 75, November 11, 1991
Arrays have changed; see Release Notes
val it = () : unit
- exception EmptyList;
exception EmptyList
- fun reduce(F,nil) = raise EmptyList
= | reduce(F,[a]) = a
= | reduce(F,x::xs) = F(x,reduce(F,xs));
val reduce = fn : ('a * 'a -> 'a) * 'a list -> 'a
- reduce(op +, [1,3,5]);
val it = 9 : int
- reduce(op +, nil);
std_in:6.8-6.11 Error: overloaded variable "+" cannot be resolved
- fun foo(x,y) = (x:int)+y;
val foo = fn : int * int -> int
- reduce(foo,[1,3,5]);
val it = 9 : int
- reduce(foo,nil);
uncaught exception EmptyList
- fun reduce1(F,m) = reduce(F,m) handle EmptyList => 0;
val reduce1 = fn : (int * int -> int) * int list -> int
- reduce1(foo,[1,3,5]);
val it = 9 : int
- reduce1(foo,nil);
val it = 0 : int
-