Compiler Design CS - 5
Compiler Design CS - 5
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus
Contact Session - 5
M3 – Syntax Analysis
IMP Note to Self
IMP Note to Students
➢ It is important to know that just login to the session does not
guarantee the attendance.
➢ Once you join the session, continue till the end to consider
you as present in the class.
➢ IMPORTANTLY, you need to make the class more interactive
by responding to Professors queries in the session.
➢ Whenever Professor calls your number / name ,you need
to respond, otherwise it will be considered as ABSENT
Objectives
To Understand
▪ Top-down parsing
▪ LL(1) Grammars
▪ Implementing LL(1) Parser
▪ Examples on LL(1) Parser
▪ Parser Generator using YACC
2. Syntax analysis:
The task concerned with fitting a sequence of tokens
into a specified syntax.
3. Parsing:
To break a sentence down into its component parts of
speech with an explanation of the form, function, and
syntactical relationship of each part
Solution
◼ Build parse tree for the string ( of tokens) based on
grammar rules
◼ The process is known as P A R S I N G
BITS Pilani, Pilani Campus
PARSING
Constructs the syntax tree for a given sequence of tokens
using grammar rules and productions such that:
1. The top node is labeled with the start symbol of the
grammar
2. Leaf nodes are labeled with terminals and inner
nodes with nonterminals
3. the children of the inner node labeled N correspond
to the members of an alternative of N, in the same
order as they occur in that alternative
4. The terminals labeling the leaf nodes correspond to
the sequence of tokens, in the same order they occur
in the input
BITS Pilani, Pilani Campus
The Role of a Parser
Lexical Rest of the
PARSER
analysis front end
Symbol Table
• The parser continues the process of translation and
validation started by the Lexical Analyzer:
1. Analyzing the phrase structure of the program,
2. Adding hierarchical (tree) structure to the flat stream
of tokens produced by the lexical Analyzer,
3. Outputting a data structure conveying the program's
syntax to subsequent compile phases, AND
4. Reporting errors when the program does not match
the syntactic structure of the source language
BITS Pilani, Pilani Campus
Parsing Methods
• Three Types of parsing methods in Vogue
1. Universal Parsing method
– Too inefficient to use in any practical compilers ( hence
not discussed any further)
– EX: Coocke–Younger–Kasami (CYK) algorithm
2. Top – Down Parsing
– Can be generated automatically or written manually
– Ex: : Left-to-Right –Top –Down Parser ( A.k.a. LL parser)
3. Bottom – Up parsing
– Can only be generated automatically
– Ex : Left-to-Right –Bottom-Up Parser ( A.k.a. LR Parser)
BITS Pilani, Pilani Campus
Top – Down & Bottom – Up
Methods
• While they are efficient;
• They can work only on restricted classes of Grammars,
( such as LL and LR Grammars )
BUT
• They are expressive enough to describe most syntactic
constructs in Programming Languages.
SO
• Care must be taken while defining the grammar for the
language to make sure that the grammar is unambiguous
Predictive Parsing E
STACK: E
T
E’
$ Program T E’
$
INPUT: id + id id $ OUTPUT:
E
STACK: T E’
Predictive Parsing
TE Program
$
E’
$
NON- INPUT SYMBOL
NON- INPUT SYMBOL
TERMINAL
TERMINAL id id + +* (* ) ( $ ) $
PARSING E E
E → TE’ E → TE’ E → TE’ E → TE’
E’ E’ E’ → +TE’E’ → +TE’ E’ → E’ →E’ → E’ →
TABLE: T T → FT’ T → FT’
T T → FT’ T → FT’
T’ T’→ T’ → *FT’ T’ → T’ →
T’ T’→ T’ → *FT’ T’ → T’ →
F F → id F → (E)
F F → id F → (E)
BITS Pilani, Pilani Campus
A Predictive Parser
▪ The Working of the Table driven Predictive Parser
INPUT: id + id id $ OUTPUT:
E
STACK: T E’
Predictive Parsing
F
T Program F T’
T’
E’
E’
$
$ NON- INPUT SYMBOL
NON- INPUT SYMBOL
TERMINAL
TERMINAL id id + + * * ( ( ) )$ $
PARSING EE E→E→TE’TE’ E → TE’
E → TE’
E’ E’ E’ → → +TE’
E’+TE’ E’ → E’E’→→ E’ →
TABLE: TT T→T→FT’ FT’ T → FT’
T → FT’
T’ T’→ T’ → *FT’ T’ → T’ →
T’ T’→ T’ → *FT’ T’ → T’ →
F F → id F → (E)
F F → id F → (E)
BITS Pilani, Pilani Campus
A Predictive Parser
▪ The Working of the Table driven Predictive Parser
Action when Top(Stack) = input $ : Pop stack, advance input.
INPUT: id + id id $ OUTPUT:
E
STACK: T E’
Predictive Parsing
id
F Program F T’
T’
E’ id
$ NON- INPUT SYMBOL
NON- INPUT SYMBOL
TERMINAL
TERMINAL id id + + * * ( ( ) )$ $
PARSING EE E→E→TE’TE’ E → TE’
E → TE’
E’ E’ E’ → → +TE’
E’+TE’ E’ → E’E’→→ E’ →
TABLE: TT T→T→FT’ FT’ T → FT’
T → FT’
T’ T’→ T’ → *FT’ T’ → T’ →
T’ T’→ T’ → *FT’ T’ → T’ →
F F → id F → (E)
F F → id F → (E)
BITS Pilani, Pilani Campus
A Predictive Parser
▪ The Working of the Table driven Predictive Parser
Action when Top(Stack) = ε : Pop stack
INPUT: id + id id $ OUTPUT:
E
STACK: T E’
Predictive Parsing
ε
T’ Program F T’
E’
$ id ε
NON- INPUT SYMBOL
NON- INPUT SYMBOL
TERMINAL
TERMINAL id id + + * * ( ( ) )$ $
PARSING EE E→E→TE’TE’ E → TE’
E → TE’
E’ E’ E’ → → +TE’
E’+TE’ E’ → E’E’→→ E’ →
TABLE: TT T→T→FT’ FT’ T → FT’
T → FT’
T’ T’→ T’ → *FT’ T’ → T’ →
T’ T’→ T’ → *FT’ T’ → T’ →
F F → id F → (E)
F F → id F → (E)
BITS Pilani, Pilani Campus
A Predictive Parser
▪ The Working of the Table driven Predictive Parser
Action when Top(Stack) = input $ : Pop stack, advance input.
INPUT: id + id id $ OUTPUT:
E
STACK: T E’
Predictive Parsing
E’
+ F T’ + T E’
Program
T$
E’ id ε
$ NON- INPUT SYMBOL
NON- INPUT SYMBOL
TERMINAL
TERMINAL id id + + * * ( ( ) )$ $
PARSING EE E→E→TE’TE’ E → TE’
E → TE’
E’ E’ E’ → → +TE’
E’+TE’ E’ → E’E’→→ E’ →
TABLE: TT T→T→FT’ FT’ T → FT’
T → FT’
T’ T’→ T’ → *FT’ T’ → T’ →
T’ T’→ T’ → *FT’ T’ → T’ →
F F → id F → (E)
F F → id F → (E)
BITS Pilani, Pilani Campus
A Predictive Parser
When Top(Stack) = input = $ the parser halts & accepts the input
string – ( id + id * id ) BITS Pilani, Pilani Campus
Algorithm for Predictive parser
• The program execution is controlled by TWO inputs
– The input symbol a and the symbol on the top of the stack
X
• There are THREE possibilities for the parser
– If X = a = $ : halt & announce the successful completion of
parsing
– If X = a ≠ $ : Pop X off the stack and advance the input
pointer to next input symbol
– If X is a nonterminal : Consult entry M [ X, a ] in the Parsing
table M and replace X on top of the stack with the RHS of
the production
• If M [ X, a ] is error call error routine BITS Pilani, Pilani Campus
Algorithm for Predictive parser
• The program execution is controlled by TWO inputs
– The input symbol a and the symbol on the top of the stack X
• There are THREE possibilities for the parser
– If X = a = $ : halt & announce the successful completion of
parsing
– If X = a ≠ $ : Pop X off the stack and advance the input pointer
to next input symbol
– If X is a nonterminal : Consult entry M [ X, a ] in the Parsing
table M and replace X on top of the stack with the RHS of the
production
• If M [ X, a ] is error call error routine
Example2:
S->AaAb / BbBa First and Follow
A->ε Pdn First Follow
S->AaAb /BbBa a,b $
B-> ε A->ε ε a,b
B-> ε ε a,b
Example3: Example5:
S->A/a S->aABb
A->a A->c / ε
B->d / ε
Example4: Example6:
S->aB / ε S->aSa / ε
B->bC / ε A->c / ε
C->cS / ε
Write the First and Follow and also the parsing table for
the above examples to check the suitability of the
grammar for LL(1) BITS Pilani, Pilani Campus
Parser generators
C
y.tab.c a.out
compiler
}
[\t] ; [\n] return 0;
return yytext[0];
%%
int yywrap()
{ return 1; }
BITS Pilani, Pilani Campus
Sample program for Parser using
Yacc
Yacc Program: ( prog.y)
%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
Thank you