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

Unit - 3 Syntax Analyzer

Syntax analyzer compiler design notes

Uploaded by

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

Unit - 3 Syntax Analyzer

Syntax analyzer compiler design notes

Uploaded by

DRASHTI LADVA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Syntax Analysis

Introduction to Syntax
Analyzer(Parser)
 By design, every programming language has precise rules that
prescribe the syntactic structure of well-formed programs.
 In C, for example, a program is made up of functions, a
function out of declarations and statements, a statement out of
expressions, and so on.
Role of 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 grammar for the source
language.
Role of Parser
 Conceptually, for well-formed programs, the parser constructs
a parse tree and passes it to the rest of the compiler for further
processing.
 There are three general types of parsers for grammars:
1. Top-down Parser
2. Bottom-up Parser
Grammer
 Grammer is nothing but collections of rules and it contains
terminal and non-terminal symbol.
 G=(N,⅀,S,P)
 For example,
Every sentence must be terminated with ;
S->S;
 Grammer is divided into 2 parts:
1. Restricted Grammer
2. Un-Restricted Grammer
Grammer
 Grammer is nothing but collections of rules and it contains
terminal and non-terminal symbol.
1. Terminal Symbols : Terminal symbols are those which are the
components of the sentences generated using a grammar and
are represented using small case letter like a-z ,digits etc.
2. Non-Terminal Symbols :Non-Terminal Symbols are those
symbols which take part in the generation of the sentence but
are not the component of the sentence. Non-Terminal Symbols
are also called Auxiliary Symbols and Variables. These
symbols are represented using a capital letter like A, B, C, etc.
Syntax Tree and Parse Tree
Syntax Tree and Parse Tree
Derivation
 The construction of a parse tree can be made by
1. Derivation
2. Reduction
Derivation
 Derivation means to replace a non terminal by the body of
its production.
 Derivation is used to find whether the string belongs to a
given grammar or not.
 Two types of Derivation
1. Left most (Left most non terminal is replaced)
2. Right most (Right most non terminal is
replaced)
Left most Derivation
Right most Derivation
Reduction
 Reduction means conversion of terminal to non terminal.
 Two types of Reduction
1. Left most (Left most terminal is replaced)
2. Right most (Right most terminal is replaced)
Ambiguity
 A grammar that produces more than one parse tree for
some sentence is said to be ambiguous.

 An ambiguous grammar is one that produces more than one


leftmost derivation or more than one rightmost derivation
for the same sentence.
Ambiguity
 For Example,
 For given grammer

leftmost derivations for the sentence id + id * id:


Ambiguity
 Two ways to solve ambiguity
1. Left Recursion
2. Left Factoring
Ambiguity : Left Recursion
 A grammar is said to be left recursive if it has a non
terminal A such that there is a derivation
A ->Aa for some string a.
 Algorithm to eliminate left recursion:
1. Arrange the non terminals in some order A1, ..., An
2. For i:= 1 to n do begin
for j:= 1 to i - 1 do begin
replace each production of the form
by the productions
Ambiguity : Left Recursion
Ambiguity : Left Factoring
Parsing
 Parsing is a technique that takes input string and produces
output either a parse tree if string is valid sentence of
grammar, or an error message indicating that string is not a
valid.
 Types of parsing are:
1. Top down parsing: In top down parsing parser build parse
tree from top to bottom.
2. Bottom up parsing: Bottom up parser starts from leaves and
work up to the root.
Parsing
LL(1) Parser(Predictive
Parser)
 The 1st L represents that the scanning of the Input will be
done from the Left to Right manner.

 The second L shows that in this parsing technique, we are


going to use the Left most Derivation Tree.

 The 1 represents the number of look-ahead, which means


how many symbols are you going to see when you want to
make a decision.
LL(1) Parser(Predictive
Parser)
 Step 1: Remove left recursion and left factorial (if any).
 Step 2: Construct predictive parser table.
2 operations : FIRST() and FOLLOW() of non terminals.
 Step 3: Parse input string using parse table.
FIRST() of Non Terminals
FIRST() of Non Terminals
FOLLOW() of Non Terminals
Recursive Decent Parser
 A top down parsing that executes a set of recursive
procedure to process the input without backtracking is
called recursive descent parser.
 There is a procedure for each non terminal in the grammar.
 Consider RHS of any production rule as definition of the
procedure.
 As it reads expected input symbol, it advances input pointer
to next position.
Recursive Decent Parser
Operator Precedence
Grammer
Precedence and Associativity
Steps of OPP
Rules to establish relation
Operator Precedence function
Operator Precedence function
SLR Parser
 LR parser is also called as SLR parser.

 it is weakest of the three methods but easier to implement.

 a grammar for which SLR parser can be constructed is


called SLR grammar.
Steps for constructing SLR
1. Writing augmentedParser
grammar.
2. LR(0) collection of items to be found.
3. Find FOLLOW of LHS of production.
4. Defining 2 functions: goto [list of terminals] and action[list
of non-terminals] in the parsing table.
SLR Parser
 Example,

S–>AA
A–>aA|b
SLR Parser
STEP1: Find augmented grammar
The augmented grammar of the given grammar is:-

S’–>.S [0th production]


S–>.AA [1st production]
A–>.aA [2nd production]
A–>.b [3rd production]
SLR Parser
STEP2: Find LR(0) collection of items.
SLR Parser
RULE –
If any non-terminal has ‘ . ‘ preceding it, we have to write all its production and
add ‘ . ‘ preceding each of its production.
RULE –
from each state to the next state, the ‘ . ‘ shifts to one place to the right.

• In the figure, I0 consists of augmented grammar.


• Io goes to I1 when ‘ . ‘ of 0th production is shifted towards the right of
S(S’->S.). this state is the accepted state. S is seen by the compiler.
• Io goes to I2 when ‘ . ‘ of 1st production is shifted towards right (S->A.A) .
A is seen by the compiler
• I0 goes to I3 when ‘ . ‘ of the 2nd production is shifted towards right (A-
>a.A) . a is seen by the compiler.
SLR Parser

STEP 3: Find FOLLOW of LHS of production

FOLLOW(S)=$
FOLLOW(A)=a,b,$
SLR Parser

STEP 4: Defining 2 functions : goto [list of non-terminals] and


action[list of terminals] in the parsing table. Below is the SLR
parsing table.

You might also like