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

compiler_design- Module3

Unit III covers syntax analysis, focusing on the role of parsers, error handling, and the definitions of various parsing techniques including top-down and bottom-up parsing. It discusses context-free grammars, derivations, and the importance of grammar in programming language specification. Additionally, it addresses error recovery strategies and the distinction between regular expressions and context-free grammars.

Uploaded by

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

compiler_design- Module3

Unit III covers syntax analysis, focusing on the role of parsers, error handling, and the definitions of various parsing techniques including top-down and bottom-up parsing. It discusses context-free grammars, derivations, and the importance of grammar in programming language specification. Additionally, it addresses error recovery strategies and the distinction between regular expressions and context-free grammars.

Uploaded by

sherinthanseer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

UNIT III

SYNTAX ANALYSIS

Unit III - SYLLABUS:


Syntax analysis – role of parser – error handling and recovery – definitions of parsing, top-down parsing
and bottom-up parsing - context free grammars – derivations – parse tree – ambiguity – associativity and
precedence of operators - writing a grammar.

2.1 SYNTAX ANALYSIS


Syntax analysis is the second phase of the compiler. It gets the input from the tokens and
generates a syntax tree or parse tree.
Advantages of grammar for syntactic specification :
1 A grammar gives a precise and easy-to-understand syntactic specification of a programming
language.
2 An efficient parser can be constructed automatically from a properly designed grammar.
3 A grammar imparts a structure to a source program that is useful for its translation into object
code and for the detection of errors.
4 New constructs can be added to a language more easily when there is a grammatical description
of the language.

2.2 THE ROLE OF PARSER


The parser or syntactic analyzer obtains a string of tokens from the lexical analyzer and
verifies that the string can be generated by the grammar for the source language. It reports any syntax
errors in the program. It also recovers from commonly occurring errors so that it can continue
processing its input.

Fig. 2.1 Position of parser in compiler model


Functions of the parser :
1 It verifies the structure generated by the tokens based on the grammar.
2 It constructs the parse tree.
3 It reports the errors.
4 It performs error recovery.
Issues :
Parser cannot detect errors such as:
1 Variable re-declaration
2 Variable initialization before use.
3 Data type mismatch for an operation.
The above issues are handled by Semantic Analysis phase.

Types of parsing

1. Universal parsing
2. Topdown parsing
3. Bottom up parsing

Universal Parsing: It can parse any grammar by using Coke – Younger – Kasami algorithm. This
method is inefficient to be used in commercial compilers.

Topdown parsing:
The top-down parser is the parser that generates parse for the given input string with the help of
grammar productions by expanding the non-terminals i.e. it starts from the start symbol and ends on the
terminals. It uses left most derivation.
Further Top-down parser is classified into 2 types: A recursive descent parser, and Non-recursive
descent parser.
1. Recursive descent parser is also known as the Brute force parser or the backtracking parser. It
basically generates the parse tree by using brute force and backtracking.
2. Non-recursive descent parser is also known as LL(1) parser or predictive parser or without
backtracking parser or dynamic parser. It uses a parsing table to generate the parse tree instead
of backtracking.

Bottom-up Parser:

Bottom-up Parser is the parser that generates the parse tree for the given input string with the help of
grammar productions by compressing the terminals i.e. it starts from terminals and ends on the start
symbol. It uses the reverse of the rightmost derivation.
Further Bottom-up parser is classified into two types: LR parser, and Operator precedence parser.
Syntax error handling :
Planning the error handling right from the start can both simplify the structure of a compiler and
improve it’s response to errors.
Programs can contain errors at many different levels. For example :
1 Lexical, such as misspelling an identifier, keyword or operator.
2 Syntactic, such as an arithmetic expression with unbalanced parentheses.
3 Semantic, such as an operator applied to an incompatible operand.
4 Logical, such as an infinitely recursive call.

Functions of error handler :

The error handler in parser has some goals:


1 It should report the presence of errors clearly and accurately.
2 It should recover from each error quickly enough to be able to detect subsequent errors.
3 It should not significantly slow down the processing of correct programs.

Error recovery strategies :


The different strategies that a parser uses to recover from a syntactic error are:
1 Panic mode
2 Phrase level
3 Error productions
4 Global correction

Panic mode recovery:


On discovering an error, the parser discards input symbols one at a time until a synchronizing
token is found. The synchronizing tokens are usually delimiters, such as semicolon or end. It has
the advantage of simplicity and does not go into an infinite loop. When multiple errors in the same
statement are rare, this method is quite useful.

Phrase level recovery:


On discovering an error, the parser performs local correction on the remaining input that
allows it to continue. Example: Insert a missing semicolon or delete an extraneous semicolon etc.
This type of replacement can correct any input string and has been used in several error repairing
compilers.

Error productions:
The parser is constructed using augmented grammar with error productions. If an error
production is used by the parser, appropriate error diagnostics can be generated to indicate the
erroneous constructs recognized by the input.

Global correction:
There are algorithms for choosing a minimal sequence of changes to obtain a globally least
cost correction. Given an incorrect input string x and grammar G, these algorithms can be used to
find a parse tree for a related string y, such that the number of insertions, deletions and changes of
tokens required to transform x to y is as small as possible. However, these methods are in general too
costly in terms of time and space.

Context-Free Grammar (CFG)

Context-Free Grammar is a quadruple that consists of terminals (T) , non-terminals (NT),


start symbol (S) and productions (P).
 Terminals : These are the basic symbols from which strings are formed.
Eg: Statement →- if expr then stmt else stmt
Here if, then and else are terminals.
 Non-Terminals : These are the syntactic variables that denote a set of strings. These help to
define the language generated by the grammar. They also impose the hierarchical structure of the
language that is useful for both syntax analysis and translation.
In the above example, expr and stmt are non terminals.
 Start Symbol : One non-terminal in the grammar is denoted as the “Start-symbol” and the set of
strings it denotes is the language defined by the grammar.
 Productions : It specifies the manner in which terminals and non-terminals can be combined to
form strings. Each production consists of a non-terminal, followed by an arrow, followed by a
string of non-terminals and terminals.

Example of context-free grammar:

1)

<Sentence> → <Noun Phrase> <Verb Phrase>


<Noun Phrase>→<Article> <Noun>
<Verb Phrase> → <Verb> <Noun Phrase>
<Article> → a| an| the
<Noun> → boy | apple
<Verb>→ ate

In this grammar,

a) a , an, the,boy , apple, ate : are terminals.


b) <Sentence> ,<Noun Phrase>, <Verb Phrase>,<Article> , <Noun>,<Verb> :
are non-terminals
c) Each line is a production.
d) <Sentence> is the start symbol

2.The following grammar defines simple arithmetic expressions:


expr → expr op expr
expr → (expr)
expr → - expr
expr → id
op → +
op → -
op → *
op → /
op → ↑

In this grammar,
 id + - * / ↑ ( ) are terminals.
 expr , op are non-terminals.
 expr is the start symbol.
 Each line is a production.

Notational Conventions
The following are some notational conventions in CFG (for our reference):

1. These symbols are terminals:


 Lower case letters a, b,c, …….z
 Operator symbol +, - etc
 Punctuation symbol - parenthesis, comma, etc.
 The digits 0,1,2,………9.
 Bold face strings id, if etc.
2. These symbols are non terminals:
 Upper case letters – A, B, C,……
 Letter S – the start symbol
 Lower case italic – like expr , stmt etc
3. Upper case letters late in the alphabet, such as X,Y, Z represent grammar symbols, that are either non
terminals or terminals.
4. Lower case Greek letters, α, β, γ represent strings of grammar symbols.
Eg:
A→ α

Here A - Left hand side of productions


α - Right hand side of production
5. If A → α1 , A → α2, ….. A → αk are all productions with A on left, we call them A
production. We may write A → α1 | α2 | α3 | …... αk. We call α1, α2, α3 , …... αk the
alternative for A.
6. Unless otherwise stated, the left side of the first production is the start symbol.

Derivations:
Two basic requirements for a grammar are :
1 To generate a valid string ( Use derivation).
2 To recognize a valid string(Use reduction).
Derivation is a process that generates a valid string with the help of grammar by replacing the
non-terminals on the left with the string on the right side of the production.

Example 1: Consider the following grammar for arithmetic expressions :


E → E+E | E*E |(E) | -E | id

To generate a valid string - ( id+id ) from the grammar the steps are
1. E → - E
2. E → - ( E )
3. E → - ( E+E )
4. E → - ( id+E )
5. E → - ( id+id )

In the above derivation,


 E is the start symbol.
 -(id+id) is the required sentence(only terminals).
 Strings such as E, -E, -(E), . . . are called sentinel forms.

Example2: Consider the following grammar


<Sentence> → <Noun Phrase> <Verb Phrase>
<Noun Phrase>→<Article> <Noun>
<Verb Phrase> → <Verb> <Noun Phrase>
<Article> → a| an| the
<Noun> → boy | apple
<Verb>→ ate

After derivation:

S ⇒ <Sentence>
⇒ <Noun Phrase> <Verb phrase>
⇒<Article> <Noun> <Verb Phrase>
⇒ <Article> <Noun> <Verb> <Noun phrase>
⇒ the <Noun> <Verb> <Article> <Noun>
⇒ the boy <Verb> <Article> <Noun>
⇒ the boy ate <Article> <Noun>
⇒ the boy ate an <Noun>
⇒ the boy ate an apple

Types of derivations:
The two types of derivation are:
1 Left most derivation
2 Right most derivation.

 In leftmost derivations, the leftmost non-terminal in each sentinel is always


chosen first for replacement.
 In rightmost derivations, the rightmost non-terminal in each sentinel is always
chosen first for replacement.
Example:
Given grammar G : E → E+E | E*E | ( E ) | - E | id Sentence to be derived : - (id+id)

Left Most Derivation Right Most Derivation


E⇒ -E E⇒ -E
⇒ -(E) ⇒ -(E)
⇒ -(E+E) ⇒ -(E+E)
⇒ -(id+E) ⇒ -(E+id)
⇒ -(id+id) ⇒ -(id+id)

 String that appear in leftmost derivation are called left sentinel forms.
 String that appear in rightmost derivation are called right sentinel forms.

Sentinels:
Given a grammar G with start symbol S, if S → α , where α may contain non-terminals or
terminals, then α is called the sentinel form of G.

Yield or frontier of tree:


Each interior node of a parse tree is a non-terminal. The children of node can be a terminal
or non-terminal of the sentinel forms that are read from left to right. The sentinel form in the parse
tree is called yield or frontier of the tree.

Ambiguity:
A grammar that produces more than one parse for some sentence is said to be ambiguous
grammar.
Example : Given grammar G : E → E+E | E*E | ( E ) | - E | id
The sentence id+id*id has the following two distinct leftmost derivations:
E → E+ E E → E* E
E → id + E E→E+ E * E
E → id + E * E E → id + E * E
E → id + id * E E → id + id * E
E → id + id * id E → id + id * id

The two corresponding trees are,


Fig. 2.2 Two parse trees for id+id*id

Reduction
Reduction is a process that recognizes a valid string with the help of grammar by replacing the non-
terminals on the RHS with the non-terminals on the Left side of the production to reach the
distinguished symbol.

Eg: Determining the validity of a string

the boy ate an apple


→ <Article> boy ate an apple
→ <Article> <Noun> ate an apple
→ <Article> <Noun> <Verb> an apple
→ <Article> <Noun> <Verb> <Article> apple
→ <Article> <Noun> <Verb> <Article><Noun>
→ <Noun Phrase> <Verb> <Article> <Noun>
→ <Noun Phrase> <Verb> <Noun Phrase>
→ <Noun Phrase> <Verb Phrase>
→ <Sentence>
S
*
ie. the boy ate an apple → S

WRITING A GRAMMAR
A grammar consists of a number of productions. Each production has an abstract symbol
called a nonterminal as its left-hand side, and a sequence of one or more nonterminal and terminal
symbols as its right-hand side. For each grammar, the terminal symbols are drawn from a specified
alphabet.

Starting from a sentence consisting of a single distinguished nonterminal, called the goal
symbol, a given context-free grammar specifies a language, namely, the set of possible sequences of
terminal symbols that can result from repeatedly replacing any nonterminal in the sequence with a
right-hand side of a production for which the nonterminal is the left-hand side.

Each parsing method can handle grammar only of a certain form, the initial grammar may
have to be rewritten to make it parsable by the method chosen. Suitable grammar for expression can
be constructed using associativity and precedence information.
The following are the transformations that are useful for rewriting grammars so they become
suitable for top-down parsing.
1. Eliminating ambiguity
2. Elimination of left recursion
3. Left factoring.
Regular Expressions VS CFG

REGULAR EXPRESSION CONTEXT-FREE GRAMMAR

It is used to describe the tokens It consists of a quadruple where S → start


of programming languages. symbol, P → production, T → terminal, V →
variable or non- terminal.

It is used to check whether the given It is used to check whether the given input is
input is valid or not using transition valid or not using derivation.
diagram.

The transition diagram has set of The context-free grammar has set of
states and edges. productions.

It has no start symbol. It has start symbol.

It is useful for describing the structure It is useful in describing nested


of structures such as balance
lexical constructs such as
identifiers,
constants, keywords, and so forth. parentheses, matching begin-
end’s and so on.

Reasons for using the regular expression to define the lexical syntax of a language

 The lexical rules of a language are simple and Regular Expression is used to describe them.
 Regular expressions provide a more concise and easier to understand notation for tokens than
grammars.
 Efficient lexical analyzers can be constructed automatically from R e g u l a r E x p r e s s i o n
than from grammars.
 Separating the syntactic structure of a language into lexical and nonlexical parts provides a
convenient way of modularizing the front end into two manageable-sized components.

Eliminating ambiguity:
Ambiguity of the grammar that produces more than one parse tree for leftmost or
rightmost derivation can be eliminated by re-writing the grammar.
Consider this example, G: stmt → if expr then stmt | if expr then stmt else stmt | other This grammar is
ambiguous since the string if E1 then if E2 then S1 else S2 has the following two parse trees for
leftmost derivation (Fig. 2.5)

Fig. 2.3 Two parse trees for an ambiguous sentence

An ambiguous grammar can be rewritten to eliminate the ambiguity. e.g. Eliminate the
ambiguity from “dangling-else” grammar,

stmt → if expr then stmt


| if expr then stmt else stmt
| other
Match each else with the closest previous unmatched then. This disambiguity rule can be
incorporated into the grammar.

To eliminate ambiguity, the following grammar may be used:


stmt → matched_stmt | unmatched_stmt
matched_stmt → if expr then matched_stmt else matched_stmt
| other
unmatched_stmt → if expr then stmt | if expr then matched_stmt else unmatched_stmt

Eliminating Left Recursion:


A grammar is said to be left recursive if it has a non-terminal A such that there is a derivation
A=>Aα for some string α. Top-down parsing methods cannot handle left-recursive grammars. Hence,
left recursion can be eliminated as follows:

If there is a production A → Aα | β it can be replaced with


a sequence A →βA’
A’ → αA’ | ε
without changing the set of strings derivable from A.

Algorithm to eliminate left recursion:

Arrange the non-terminals in some order A1, A2 . . . An.


for i := 1 to n do begin
for j := 1 to i-1 do begin replace each production of the form Ai →
Aj γ
by the productions Ai → δ1 γ | δ2γ | . . . | δk γ
where Aj → δ1 | δ2 | . . . | δk are all the current Aj-productions;
end
eliminate the immediate left recursion among the Ai-productions
end

Left factoring:
Left factoring is a grammar transformation that is useful for producing a grammar suitable for
predictive parsing. When it is not clear which of two alternative productions to use to expand a non-
terminal A, we can rewrite the A- productions to defer the decision until we have seen enough of the
input to make the right choice.

If there is any production A → αβ1 | αβ2 , it can be


rewritten as A →αA’
A’ → β1 | β2
Consider the grammar , G : S → iEtS | iEtSeS | a
E→b
Left factored, this
grammar becomes S →
iEtSS’ | a
S’ → eS |ε
E→b
2.3 PARSING
It is the process of analyzing a continuous stream of input in order to determine its
grammatical structure with respect to a given formal grammar.

Parse tree:
Graphical representation of a derivation or deduction is called a parse tree. Each interior node
of the parse tree is a non-terminal; the children of the node can be terminals or non- terminals.

Types of parsing:
1 Top down parsing
2 Bottom up parsing
 Top-down parsing : A parser can start with the start symbol and try to
transform it to the input string. Example : LL Parsers.
 Bottom-up parsing : A parser can start with input and attempt to
rewrite it into the start symbol. Example : LR Parsers.

2.5 TOP-DOWN PARSING

It can be viewed as an attempt to find a left-most derivation for an input string or an attempt
to construct a parse tree for the input starting from the root to the leaves.

In top-down parsing, the parse tree is generated from top to bottom, i.e., from root to leaves &
expand till all leaves are generated. It generates the parse tree containing root as the starting symbol
of the Grammar. It starts derivation from the start symbol of Grammar & performs leftmost
derivation at each step.
Types of Top-Down Parsing
There are two types of top-down parsing which are as follows −
Example for backtracking :

Consider the grammar G : S → cAd


A→ab|a
and the input string w=cad.
The parse tree can be constructed using the following top-down approach :

Step1:
Initially create a tree with single node labeled S. An input pointer points to ‘c’, the first symbol of
w. Expand the tree with the production of S.
S

c A d
Step2:
The leftmost leaf ‘c’ matches the first symbol of w, so advance the input pointer to
the second symbol of w ‘a’ and consider the next leaf ‘A’. Expand A using the first
alternative.

c A d

a b

Step3:
The second symbol ‘a’ of w also matches with second leaf of tree. So advance the
input pointer to third symbol of w ‘d’. But the third leaf of tree is b which does not
match with the input symbol d. Hence discard the chosen production and reset the
pointer to second backtracking.

Step4:
S Now try the second alternative for

c A d

a
Now we can halt and announce the successful completion of parsing.

Algorithm (Naive Top – down Parsing)

For a valid source string α , a top down parse determines a derivation sequence
s => ……..=> …...=>α

1. Current Sentential Form (CSF) = ‘S’


2. Let CSF be of the form βA∏, such that β is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is T’s is a string of T’s ( note that β may be null), and A is ( is a string of T’s ( note that β may be null), and A is note is a string of T’s ( note that β may be null), and A is that is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is β is a string of T’s ( note that β may be null), and A is may is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is null), is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is A is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is
the is a string of T’s ( note that β may be null), and A is leftmost is a string of T’s ( note that β may be null), and A is NT is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is CSF. is a string of T’s ( note that β may be null), and A is Exit is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is success is a string of T’s ( note that β may be null), and A is if is a string of T’s ( note that β may be null), and A is CSF is a string of T’s ( note that β may be null), and A is = α.
3. Make derivation A => β1Bδ according to the production A –> δ is a string of T’s ( note that β may be null), and A is according is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is production is a string of T’s ( note that β may be null), and A is A is a string of T’s ( note that β may be null), and A is –> is a string of T’s ( note that β may be null), and A is β1Bδ according to the production A –> δ is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is G is a string of T’s ( note that β may be null), and A is such is a string of T’s ( note that β may be null), and A is that is a string of T’s ( note that β may be null), and A is β1 is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is
string is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is Ts is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is (again is a string of T’s ( note that β may be null), and A is β1 is a string of T’s ( note that β may be null), and A is may is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is null). is a string of T’s ( note that β may be null), and A is This is a string of T’s ( note that β may be null), and A is makes is a string of T’s ( note that β may be null), and A is CSF=β β1Bδ according to the production A –> δ∏.
4. is a string of T’s ( note that β may be null), and A is Go is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is step is a string of T’s ( note that β may be null), and A is 2.

Bottom-up Parsing
Bδ according to the production A –> ottom-up is a string of T’s ( note that β may be null), and A is parsing is a string of T’s ( note that β may be null), and A is starts is a string of T’s ( note that β may be null), and A is from is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is leaf is a string of T’s ( note that β may be null), and A is nodes is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is works is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is upward is a string of T’s ( note that β may be null), and A is direction is a string of T’s ( note that β may be null), and A is till is a string of T’s ( note that β may be null), and A is it
reaches is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is root is a string of T’s ( note that β may be null), and A is node. is a string of T’s ( note that β may be null), and A is Here, is a string of T’s ( note that β may be null), and A is we is a string of T’s ( note that β may be null), and A is start is a string of T’s ( note that β may be null), and A is from is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is sentence is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is then is a string of T’s ( note that β may be null), and A is apply is a string of T’s ( note that β may be null), and A is production is a string of T’s ( note that β may be null), and A is rules is a string of T’s ( note that β may be null), and A is in
reverse is a string of T’s ( note that β may be null), and A is manner is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is order is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is reach is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is start is a string of T’s ( note that β may be null), and A is symbol.

Bδ according to the production A –> ottom-up is a string of T’s ( note that β may be null), and A is parsing is a string of T’s ( note that β may be null), and A is can is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is represented is a string of T’s ( note that β may be null), and A is as is a string of T’s ( note that β may be null), and A is an is a string of T’s ( note that β may be null), and A is attempt is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is reduce is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is input is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is ω is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the
start is a string of T’s ( note that β may be null), and A is symbol is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is grammar is a string of T’s ( note that β may be null), and A is by is a string of T’s ( note that β may be null), and A is discovering is a string of T’s ( note that β may be null), and A is out is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is right-most is a string of T’s ( note that β may be null), and A is derivation is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is ω is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is reverse. is a string of T’s ( note that β may be null), and A is This
is is a string of T’s ( note that β may be null), and A is similar is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is generating is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is for is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is input is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is ω is a string of T’s ( note that β may be null), and A is by is a string of T’s ( note that β may be null), and A is starting is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is leaves is a string of T’s ( note that β may be null), and A is and
proceeding is a string of T’s ( note that β may be null), and A is toward is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is root is a string of T’s ( note that β may be null), and A is i.e., is a string of T’s ( note that β may be null), and A is attempting is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is generate is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is from is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is bottom-up.

This contains searching for the substring that connect the right side of any of the production of the
grammar. This substring is restored by the left-hand side nonterminal of the production if this
replacement leads to the generation of the sentential form that comes one step before the right-most
derivation.

This process of replacing the right-side of the production by the left side nonterminal is known as
reduction. Hence, the reduction is nothing more than performing derivations in reverse.

Types of Bottom-Up Parser

There are three types of the bottom-up parser which are as follows −

Shift Reduce Parser − Shift reduce parser is a type of bottom-up parser. It can require a stack to
influence grammar symbols. A parser goes on shifting the input symbols onto the stack until a handle
comes on the top of the stack. When a handle occurs on the top of the stack, it implements reduction.

Operator Precedence Parser − The shift-reduce parsers can be generated by hand for a small class of
grammars. These grammars have the property that no production on the right side is ϵ or has two
adjacent non-terminals. Grammar with the latter property is known as operator grammar.
LR Parsers − The LR Parser is a shift-reduce parser that creates use of deterministic finite automata,
identifying the set of all viable prefixes by reading the stack from bottom to top. It decides what handle,
if any, is available.

Bottom up parsing example:

Consider the grammar G : S → cAd


A→ab|a
and the input string w=cabd.

The is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is can is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is constructed is a string of T’s ( note that β may be null), and A is using is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is following is a string of T’s ( note that β may be null), and A is bottom is a string of T’s ( note that β may be null), and A is up is a string of T’s ( note that β may be null), and A is approach is a string of T’s ( note that β may be null), and A is :

Step 1:
We is a string of T’s ( note that β may be null), and A is start is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is input is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is cabd is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is build is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is root is a string of T’s ( note that β may be null), and A is =S, is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is top is a string of T’s ( note that β may be null), and A is level is a string of T’s ( note that β may be null), and A is Non is a string of T’s ( note that β may be null), and A is
Terminal. is a string of T’s ( note that β may be null), and A is First is a string of T’s ( note that β may be null), and A is build is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is leaf is a string of T’s ( note that β may be null), and A is nodes.

c a b d

Step 2:

Using is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is production is a string of T’s ( note that β may be null), and A is A→ab, is a string of T’s ( note that β may be null), and A is build is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is inner is a string of T’s ( note that β may be null), and A is node is a string of T’s ( note that β may be null), and A is A.

c a b d

is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is

Step is a string of T’s ( note that β may be null), and A is 3:

Using is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is production is a string of T’s ( note that β may be null), and A is S is a string of T’s ( note that β may be null), and A is → is a string of T’s ( note that β may be null), and A is cAd, is a string of T’s ( note that β may be null), and A is build is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is inner is a string of T’s ( note that β may be null), and A is node is a string of T’s ( note that β may be null), and A is S.

c a b d
Since is a string of T’s ( note that β may be null), and A is we is a string of T’s ( note that β may be null), and A is could is a string of T’s ( note that β may be null), and A is build is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree, is a string of T’s ( note that β may be null), and A is starting is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is input is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is up is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is starting is a string of T’s ( note that β may be null), and A is non
terminal is a string of T’s ( note that β may be null), and A is S, is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is input is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is ‘cabd’ is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is valid is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is based is a string of T’s ( note that β may be null), and A is on is a string of T’s ( note that β may be null), and A is that is a string of T’s ( note that β may be null), and A is grammar.

Algorithm
A is a string of T’s ( note that β may be null), and A is bottom-up is a string of T’s ( note that β may be null), and A is parser is a string of T’s ( note that β may be null), and A is constructs is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is parse is a string of T’s ( note that β may be null), and A is tree is a string of T’s ( note that β may be null), and A is for is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is source is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is through is a string of T’s ( note that β may be null), and A is a is a string of T’s ( note that β may be null), and A is sequence is a string of T’s ( note that β may be null), and A is of
reductions. is a string of T’s ( note that β may be null), and A is The is a string of T’s ( note that β may be null), and A is source is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is valid is a string of T’s ( note that β may be null), and A is if is a string of T’s ( note that β may be null), and A is it is a string of T’s ( note that β may be null), and A is can is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is reduced is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is S, is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is starting is a string of T’s ( note that β may be null), and A is nonterminal.
Bδ according to the production A –> ottom-up is a string of T’s ( note that β may be null), and A is parsing is a string of T’s ( note that β may be null), and A is proceeds is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is left-to-right is a string of T’s ( note that β may be null), and A is manner, is a string of T’s ( note that β may be null), and A is i.e. is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is reduction is a string of T’s ( note that β may be null), and A is starts is a string of T’s ( note that β may be null), and A is with is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is first
symbol is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is proceed is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is right. is a string of T’s ( note that β may be null), and A is Always is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is reduction is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is applied is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is left is a string of T’s ( note that β may be null), and A is of
the is a string of T’s ( note that β may be null), and A is pointer is a string of T’s ( note that β may be null), and A is SSM is a string of T’s ( note that β may be null), and A is (Source is a string of T’s ( note that β may be null), and A is String is a string of T’s ( note that β may be null), and A is Marker):

is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is

Since is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is reduction is a string of T’s ( note that β may be null), and A is has is a string of T’s ( note that β may be null), and A is been is a string of T’s ( note that β may be null), and A is applied is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is on is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is left is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is SSM, is a string of T’s ( note that β may be null), and A is this is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is is is a string of T’s ( note that β may be null), and A is composed
of is a string of T’s ( note that β may be null), and A is Nts(Non is a string of T’s ( note that β may be null), and A is Terminals) is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is Ts is a string of T’s ( note that β may be null), and A is (Terminals). is a string of T’s ( note that β may be null), and A is Remainder is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is string, is a string of T’s ( note that β may be null), and A is yet is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is processed is a string of T’s ( note that β may be null), and A is by
the is a string of T’s ( note that β may be null), and A is parser, is a string of T’s ( note that β may be null), and A is consists is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is Ts is a string of T’s ( note that β may be null), and A is alone.

Let is a string of T’s ( note that β may be null), and A is there is a string of T’s ( note that β may be null), and A is be is a string of T’s ( note that β may be null), and A is n is a string of T’s ( note that β may be null), and A is symbols is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is left is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is SSM is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is current is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is form. is a string of T’s ( note that β may be null), and A is We is a string of T’s ( note that β may be null), and A is try is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is reduce is a string of T’s ( note that β may be null), and A is some
part is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is string is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is immediate is a string of T’s ( note that β may be null), and A is left is a string of T’s ( note that β may be null), and A is of is a string of T’s ( note that β may be null), and A is SSM. is a string of T’s ( note that β may be null), and A is is a string of T’s ( note that β may be null), and A is Let is a string of T’s ( note that β may be null), and A is this is a string of T’s ( note that β may be null), and A is part is a string of T’s ( note that β may be null), and A is have is a string of T’s ( note that β may be null), and A is r is a string of T’s ( note that β may be null), and A is symbols is a string of T’s ( note that β may be null), and A is in is a string of T’s ( note that β may be null), and A is it, is a string of T’s ( note that β may be null), and A is and is a string of T’s ( note that β may be null), and A is let is a string of T’s ( note that β may be null), and A is it
be is a string of T’s ( note that β may be null), and A is reduced is a string of T’s ( note that β may be null), and A is to is a string of T’s ( note that β may be null), and A is the is a string of T’s ( note that β may be null), and A is NT, is a string of T’s ( note that β may be null), and A is A is a string of T’s ( note that β may be null), and A is as is a string of T’s ( note that β may be null), and A is follows:

Since we do not know the value of r, we try the values r =n, r=n-1, r=n-2, ……… r=1. The algorithm
is given below.
Operator Precedence
Operator Precedence Parsing is also a type of Bottom-Up Parsing that can be used to a class of
Grammars known as Operator Grammar.

A Grammar G is Operator Grammar if it has the following properties −

 Production should not contain ϵ on its right side.

 There should not be two adjacent non-terminals at the right side of production.

Example1 − Verify whether the following Grammar is operator Grammar or not.

E → E A E |(E)|id
A → +| − | ∗

Solution

No, it is not an operator Grammar as it does not satisfy property 2 of operator Grammar.

As it contains two adjacent Non-terminals on R.H.S of production E → E A E.

We can convert it into the operator Grammar by substituting the value of A in E → E A E.

E → E + E |E − E |E * E |(E) | id.

Operator Precedence Relations

Three precedence relations exist between the pair of terminals.

Three precedence relations exist between the pair of terminals.

Relation Meaning
a yields precedence to b

a has the same precedence as b

a takes precedence over b

Depending upon these precedence Relations, we can decide which operations will be executed or parsed
first.
Association and Precedence Rules

 If operators have different precedence

Since * has higher precedence than +

Example−

In a statement a + b * c

∴ + <. * + <. *

In statement a * b + c

∴ + <. * ∗ ⋗ +

 If operators have Equal precedence, then use Association rules.

(a) Example minus; In statement a + b + c here + operators are having equal precedence.
As '+' is left Associative in a + b + c

∴ + <. * (a + b) will be computed first, and then it will be added to c.

i.e., (a + b) + c

+⋗+

Similarly, '*' is left Associative in a * b * c

(b) Example − In a statement a ↑ b ↑ c here, ↑ is the Right Associative operator

∴ + <. * It will become a ↑ (b ↑ c)

∴ + <. * (b ↑ c) will be computed first.

∴ + <. * ↑<. ↑

Operator precedance table

For drawing the operator precedence table, we will take the terminals present in the grammar on the
left-hand side and right-hand side.
 If the symbol on the left-hand side has higher precedence than the right-hand side symbol, insert
the ⋗ symbol in the table. symbol in the table.
 If the symbol on the left-hand side has lower precedence than the right-hand side symbol, insert
the ⋖ symbol in the table. symbol in the table.
 If the symbol on the left-hand side has equal precedence to the right-hand side symbol, insert
nothing in the case of terminals, ⋗ symbol in the table. symbol in the operators, and A in the case of the $ symbol in
the table.

You might also like