0% found this document useful (0 votes)
58 views

Compilerchapter 4

- Bottom-up parsing starts with the input symbols and constructs the parse tree from the bottom up. - The main bottom-up parsing techniques are shift-reduce parsing and LR parsing. Shift-reduce parsing uses two steps - shift and reduce. LR parsing builds the parse tree bottom-up using shift and reduce actions. - LR parsers can handle a larger class of grammars than LL parsers but not all grammars. The main LR parsing techniques are SLR, LR, and LALR which can handle grammars of increasing sizes.

Uploaded by

Fitawu Tekola
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Compilerchapter 4

- Bottom-up parsing starts with the input symbols and constructs the parse tree from the bottom up. - The main bottom-up parsing techniques are shift-reduce parsing and LR parsing. Shift-reduce parsing uses two steps - shift and reduce. LR parsing builds the parse tree bottom-up using shift and reduce actions. - LR parsers can handle a larger class of grammars than LL parsers but not all grammars. The main LR parsing techniques are SLR, LR, and LALR which can handle grammars of increasing sizes.

Uploaded by

Fitawu Tekola
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Bottom-up Parsing

- starts with the input symbols and tries to construct the


parse tree up to the start symbol.

The bottom-up parsers available :


Shift-Reduce Parsing
It uses two unique steps for bottom-up parsing.
-shift-step
-reduce-step.
Stack Implementation of Shift-Reduce Parsing
A general schematic for a successful bottom-up parse is

n(steps) Parsing stack Input buffer Action

1 $ Input String$  
... ... ... ...
N $ Start symbol $ Accept
Example: Use the shift-reduce to parse the string
id1+id2*id3 using the grammar

E  E  E
E  E*E
E  (E)
E  id
n(steps) Parsing stack Input buffer Action

1
$ $ Shift
2
$ $ Reduce
3
$ $ Shift
4
$ $ Shift
5
$ $ Reduce
6
$ $ Shift
7
$ $ Shift
8
$ $ Reduce
9
$ $ Reduce
10
$ $ Reduce
11
$ $ Accept
Example Use the shift-reduce to parse the string
id1*id2 using the grammar
Exercise : Apply shift-reduce to the following input: 000111
and the following productions:
S-> 0S1 | 01

LR Parser
It is a class of bottom-up methods for parsing that accept a
much larger class of grammars than LL(1) parsing, though
still not all grammars.

There are three widely used algorithms(techniques) available


for constructing a parsing table for LR parser:
-SLR(1) – Simple LR Parser
-LR(1) – LR Parser
-LALR(1) – Look-Ahead LR Parser
SLR(1) – Simple LR Parser:
o Works on smallest class of grammar
o Few number of states, hence very small table
o Simple and fast construction
The easiest to implement ,but the least powerful of the three
It may fail to produce a parsing table for certain grammars on
which the other methods succeed.

LR(1) – LR Parser:
o Works on complete set of LR(1) Grammar
o Generates large table and large number of states
o Slow construction
The most powerful, and the most expensive
 
LALR(1) – Look-Ahead LR Parser:
o Works on intermediate size of grammar
o Number of states are same as in SLR(1)
LR parsers are table-driven bottom-up parsers and use two kinds of
“actions” involving the input stream and a stack:
-Shift
-Reduce
LR parsers are also called shift-reduce parsers.

Example Consider the grammar


abbcde
S  aABe aAbcde ; A  b
A  Abc | b aAde ; A  Abc
Bd aABe ;B  d
S ; S  aABe
Note that these reductions, in fact, trace out the following right
most derivation in reverse:

S  aABe  aAde  aAbcde  abbcde


LL vs. LR
LL LR
Does a leftmost derivation. Does a rightmost derivation in reverse.
 
Starts with the root nonterminal on the Ends with the root nonterminal on the
stack. stack.
 
Ends when the stack is empty. Starts with an empty stack.
   
Builds the parse tree top-down. Builds the parse tree bottom-up.
   
Expands the non-terminals. Reduces the non-terminals.
 
Reads the terminals when it pops one Reads the terminals while it pushes them
off the stack. on the stack.
   
Limitations of Syntax Analyzers

Syntax analyzers have the following drawbacks:


 it cannot determine if a token is valid,
 it cannot determine if a token is declared before it is being
used,
 it cannot determine if a token is initialized before it is being
used,
 it cannot determine if an operation performed on a token
type is valid or not.

These tasks are accomplished by the semantic analyzer, which


we shall study
in Semantic Analysis.
Chapter 4
Syntax-Directed Translation
Semantics help interpret symbols, their types, and
their relations with each other.
Semantic analysis judges whether the syntax structure
constructed in the source program derives any
meaning or not.

Fig. Conceptual view of syntax – directed translation


A parse tree showing the values of attributes at each
node is called an annotated parse tree.
The following tasks should be performed in semantic
analysis:
-Scope resolution
-Type checking
-Array-bound checking
Semantic Errors
some of the semantic errors that the semantic analyzer is
expected to recognize:
-Type mismatch
-Undeclared variable
-Reserved identifier misuse
-Multiple declaration of variable in a scope
-Accessing an out of scope variable
-Actual and formal parameter mismatch
An attribute can represent any thing: a string, a
number, a type, a memory location whatever.
Terminals are assumed to have synthesized
attributes only.
An inherited attribute distributes type
information to the various identifiers in
declaration.
Uses of inherited attribute
- To keep track of whether an identifier appears
on the left or right side of an assignment in
order to decide whether the address or the value
of the identifier is needed.
Attribute Grammar
Attribute grammar is a special form of context-free grammar
where some additional information (attributes) are appended to
one or more of its non-terminals in order to provide context-
sensitive information.
Domain of values of attributes:
integer, float, character, string, expressions and memory location

E → E + T { E.value = E.value + T.value }

Semantic attributes
-may be assigned to their values from their domain at
the time of parsing and
-evaluated at the time of assignment or conditions.
Categories of Attributes:
-synthesized attributes and
-inherited attributes.
Synthesized Attributes
These attributes get values from the attribute
values of their child nodes.
-never take values from their parent nodes or
any sibling nodes.
A SDD that uses synthesized attributes exclusively is
said to be an S- attributed definition.
A parse tree for an S-attributed definition can always
be annotated by evaluating the semantic rules for the
attributes at each node bottom-up, from the leaves to
the root.
pr int( E.val )
E.val  E1.val  T .val
E.val  T .val
T .val  T1.val * F .val
T .val  F .val
F .val  E.val
F .val  digit.lexval

Figure: Annotated parse tree for 3*5+4 n


The program prints the value 19.
Inherited Attributes
Inherited attributes can take values from parent and/or
siblings.
Semantic analyzer receives AST (Abstract Syntax Tree)
from its previous stage (syntax analysis).
Example. Annotated parse tree for the sentence real
id1,id2,id3 D  TL
T  int
T  real
L  L1 , id
L  id

L.in  T .type
T .type  int eger
T .type  real
L1.in  L.in, addtype(id .entry, L.in)
L-attributed SDT
- uses both synthesized and inherited
attributes with restriction of not taking
values from right siblings.

No non-terminal can get values from the


sibling to its right.
S → ABC

S can take values from A, B, and C


(synthesized). A can take values from
S only. B can take values from S and
A. C can get values from S, A, and B.
No non-terminal can get values from the
sibling to its right.
Dependency Graphs
The interdependencies among the inherited and
synthesized attributes at the nodes in a parse tree can
depicted by a directed graph called a dependency
graph.

Fig. E.val is synthesized from E1.val and E2.val.


Figure: Dependency graph for the former parse tree
Evaluation Order: From the topological sort, we obtain
the following program. an : the attribute associated with the
node numbered n in the dependency graph.
Construction of Syntax Trees
Syntax trees

in the parse tree.


The parse tree of 3*5+4 n becomes the syntax tree

Constructing Syntax Trees For Expressions


Functions to create the nodes of syntax trees for
expressions with binary operators:
Fig. Syntax tree for a-4+c

You might also like