You are on page 1of 32

Chapter Three : Syntax Analysis

oParsing

oContext Free Grammars

oLeft Recursion (Reading Assignment)

oDerivation Trees

oSyntax Error Handling

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Syntax Analyzer

o By design every programming language has precise rules that


prescribe the syntactic structure of well-formed programs.

o Syntax Analyzer creates the syntactic structure of the given source


program.

o The syntax of a programming is described by a context-free


grammar (CFG). We will use BNF (Backus-Naur Form) notation
in the description of CFGs.

o This syntactic structure is mostly a parse tree. Syntax Analyzer is


also known as parser.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
oThe syntax analyzer (parser) checks whether a given

source program satisfies the rules implied by a

context-free grammar or not.

o If it satisfies, the parser creates the parse tree of that

program.

o Otherwise the parser gives the error messages.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
oParser works on a stream of tokens.

oThe smallest item is a token (in Syntax Analyzer).

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
o We categorize the parsers into two groups:
o Top-Down Parser : the parse tree is created top to bottom, starting from
the root.
o Bottom-Up Parser : the parse is created bottom to top; starting from
the leaves

o Both top-down and bottom-up parsers scan the input from left to
right (one symbol at a time).

o Efficient top-down and bottom-up parsers can be implemented


only for sub-classes of context-free grammars.
o LL for top-down parsing
o RL for bottom-up parsing

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
A context-free grammar
o Gives a precise syntactic specification of a programming
language.

o The design of the grammar is an initial phase of the


design of a compiler.

o A grammar can be directly converted into a parser by


some tools.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
• The use of CFGs has several advantages over BNF:
• helps in identifying ambiguities

• a grammar gives a precise yet easy to understand


syntactic specification of a programming language

• it is possible to have a tool which produces automatically


a parser using the grammar

• a properly designed grammar helps in modifying the


parser easily when the language changes

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis

Revision on a context-free grammar

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
In a context-free grammar, we have:
oA finite set of terminals (in our case, this will be the
set of tokens)
oA finite set of non-terminals (syntactic-variables)

oA finite set of productions rules in the following form :

o A → α where, A is a non-terminal and α is a string of


terminals and non-terminals (including the empty string)

o A start symbol (one of the non-terminal symbol)

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Right-Linear Grammar

oIn right-linear grammar, all productions have one of the


two forms:

or

oi.e., the left hand side should have a single variable and
the right hand side consists of any number of terminals
(members of T) optionally followed by a single variable.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Left-Linear Grammar

oIn left-linear grammar, all productions have one of the


two forms:

or

oi.e., the left hand side must consist of a single variable ,


and the right-hand side consists of an optional single
variable followed by one number of terminals.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Example 1.
Construct right-and left-linear grammars for the
language

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Derivation Trees

A ‘derivation tree’ is an ordered tree which the nodes


are labeled with the left sides of productions and in
which the children of a node represent its
corresponding right sides.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Definition of a Derivation Trees

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Right Most / Left Most / Mixed Derivation

aababbb

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Left Most Derivation

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Right Most Derivation

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Mixed Derivation

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Example 2

A grammar G which is context-free has the


Productions acbabc

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Parser Three

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
A CFG given by productions is :

Obtain the derivation tree of the word w = abaabaa.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Example 3 id*id +id and (id+id)*id

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Parsing

o“Parsing” a string is finding a derivation (or a


derivation tree) for that string. Parsing a string is
like recognizing a string. The only realistic way to
recognize a string of a context-free grammar is to
parse it.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Top down/Bottom up Parsing

oSequence of rules are applied in a leftmost


derivation in Top down parsing.

oSequence of rules are applied in a rightmost


derivation in Bottom up parsing.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Ambiguous Gammares /Ambiguous Languates

oSince derivation trees, leftmost derivations, and


rightmost derivations are equivalent rotations, the
following definitions are equivalent:

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Ambiguous Gammares /Ambiguous Languates

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
The Role of a Parser
• In our compiler model, the parser obtains a string of
tokens from the lexical analyzer, and verifies that the
string of token names can be generated by the source
language
• It is expected that the parser reports any syntax errors in
an intelligible fashion and to recover from commonly
occurring errors to continue processing the remainder of
the program

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
The Role of a Parser

• Conceptually, for well-formed programs, the parser


constructs a parse tree and passes it to the rest of the
compiler for further processing

• The methods commonly used in compilers can be


classified as being either top-down or bottom-up

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
The Role of a Parser

• Top-down methods build parse trees from top (root)


to the bottom (leaves), while bottom-up methods
start from the leaves and work their way up to the
root

• In either case, the input to the parser is scanned


from left to right, one symbol at a time

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Syntax Error Handling
o The error handler should be written with the following goals in
mind:
• errors should be reported clearly and accurately
• the compiler should recover and detect other errors
• it should not slow down the whole process (of compilation)
significantly
o Fortunately most errors are straight forward and easy to detect.
E.g. of common errors:
missing ; missing : in :=
o The error handler should report:
• the place in the source program where the error is detected
• the type of error (if possible)

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU


Chapter Three : Syntax Analysis
Syntax Error Handling
oThere are four main strategies in error handling:
• Panic mode recovery : discards all tokens until a
synchronization token is found.
• Phrase level recovery: the parser makes a local
correction so that it can continue to parse the rest of the
input.
• Error productions: augment the grammar to capture the
most common errors that programmers make.

• Global correction: makes as few changes as possible in


the program so that a globally least cost correction
program is obtained.

Prepared by Befkadu (MSc) : 2012 E.C\2018-2020 A.C in AASTU

You might also like