Escolar Documentos
Profissional Documentos
Cultura Documentos
Example:
if (a > b) a = a + 1;
else b = b + 1;
Describing Syntax
A language is a set of strings of
characters from some alphabet.
Examples:
English (using the standard alphabet)
binary numbers (using the alphabet
{0, 1})
LANGUAGE RECOGNIZERS
Languages can be defined in two
ways: by recognition and by
generation
A language generator is a device that
can be used to generate the
sentences of a language
BNF Notation
Parse Tree
A parse tree is a graphical way of
representing a derivation.
the root of the parse tree is always the
start symbol
each interior node is a nonterminal
each leaf node is a token
the children of a nonterminal (interior
node) are the RHS of some rule whose
LHS is the nonterminal
PARSE TREES
Most attractive feature of grammars is that they
describe the hierarchical syntactic structure of
sentences of the languages they define. These
hierarchical structures are called parse trees
A grammar that generates a sentence for which
there are two or more distinct parse trees is said to
be ambiguous
Syntactic ambiguity of language structures is a
problem because compilers often base the
semantics of those structures on their syntactic
form
SYNTAX GRAPHS
A graph is a collection of nodes, some of which
are connected by lines, called edges
A directed graph is one in which the edges are
directional; they have arrowheads on one end
to indicate a direction
The information in BNF rules can be
represented in a directed graph, such graphs
are called syntax graphs. These graphs use
rectangles for non-terminals and circles for
terminals
GRAMMARS AND
RECOGNIZERS
One of the most widely used of the syntax
analyzer generators is named yacc - yet another
compiler compiler Syntax analyzers for
programming languages, which are often called
parsers, construct parse trees for given
programs
The 2 broad classes of parsers are top-down, in
which the tree is built from the root downward to
the leaves, and bottom-up, in which the parse
tree is built from the leaves upward to the root.
RECURSIVE DECENT
PARSING
Context-free grammar can serve as the
basis for the syntax analyzer, or parser, of a
compiler
A simple kind of grammar-based top-down
parser is named recursive decent
Parsing is the process of tracing a parse
tree for a given input string
The basic idea of a recursive decent parser
is that there is a subprogram for each nonterminal in the grammar
ATTRIBUTE GRAMMARS
An attribute grammar is a device
used to describe more of the
structure of
a programming language than is
possible with a context-free grammar
An attribute grammar is an extension
to a context-free grammar