SlideShare a Scribd company logo
COMPILER DESIGN
SYNTAX ANALYSIS
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 1
Ms. RICHA SHARMA
Assistant Professor
richa.18364@lpu.co.in
Lovely Professional
University
SYNTAX ANALYSIS INTRODUCTION
• LEXICAL PHASE IS IMPLEMENTED ON FINITE AUTOMATA & FINITE AUTOMATA CAN REALLY
ONLY EXPRESS THINGS WHERE YOU CAN COUNT MODULUS ON K.
• REGULAR LANGUAGES – THE WEAKEST FORMAL LANGUAGES WIDELY USED
– MANY APPLICATIONS
– CAN’T HANDLE ITERATION & NESTED LOOPS(NESTED IF ELSE ).
TO SUMMARIZE, THE LEXER TAKES A STRING OF CHARACTER AS INPUT AND PRODUCES A STRING
OF TOKENS AS OUTPUT.
THAT STRING OF TOKENS IS THE INPUT TO THE PARSER WHICH TAKES A STRING OF TOKENS AND
PRODUCES A PARSE TREE OF THE PROGRAM.
SOMETIMES THE PARSE TREE IS ONLY IMPLICIT. SO THE, A COMPILER MAY NEVER ACTUALLY
BUILD THE FULL PARSE
TREE.
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 2
Lexical
Analyzer
Parser
Source
program
token
getNext
Token
Symbol
table
Parse tree Rest of
Front End
Intermediate
representation
ROLE OF SYNTAX ANALYSIS/PARSER
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 3
CONTEXT FREE GRAMMARS
expression -> expression + term
expression -> expression – term
expression -> term
term -> term * factor
term -> term / factor
term -> factor
factor -> (expression)
factor -> id
S - IS A FINITE SET OF TERMINALS
N - IS A FINITE SET OF NON-TERMINALS
P - IS A FINITE SUBSET OF PRODUCTION
RULES
S - IS THE START SYMBOL
G=(S ,N,P,S)
• A GRAMMAR DERIVES STRINGS BY BEGINNING WITH START SYMBOL AND
REPEATEDLY REPLACING A NON TERMINAL BY THE RIGHT HAND SIDE OF A
PRODUCTION FOR THAT NON TERMINAL.
• FROM THE START SYMBOL OF A GRAMMAR G FORM THE LANGUAGE L(G)
DEFINED BY THE GRAMMAR THE STRINGS THAT CAN BE DERIVED .
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 4
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 5
• PROGRAMMING LANGUAGES HAVE RECURSIVE STRUCTURE
• CONTEXT-FREE GRAMMARS ARE A NATURAL NOTATION FOR THIS
RECURSIVE STRUCTURE .
NOT ALL STRINGS OF TOKENS ARE PROGRAMS . . .
. . . PARSER MUST DISTINGUISH BETWEEN VALID AND INVALID
STRINGS OF TOKENS
WE NEED :
– A LANGUAGE :FOR DESCRIBING VALID STRINGS OF TOKENS
– A METHOD: FOR DISTINGUISHING VALID FROM INVALID STRINGS OF
TOKENS
CONTEXT FREE GRAMMARS
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY)
E ::= T | E + T | E - T
T ::= F | T * F |T / F
F ::= id | (E)
• ARITHMETIC EXPRESSIONS
• STATEMENTS
If Statement ::= if E then Statement else Statement
CONTEXT FREE GRAMMAR EXAMPLES
Steps:
1. Begin with a string with only the start
symbol S
2. Replace any non-terminal X in the
string by the right-hand side of some
production
X -> Y1…Yn
3. Repeat (2) until there are no non-
terminals
6
DERIVATIONS
• DERIVATION IS A SEQUENCE OF PRODUCTIONS SO BEGINNING WITH THE START SYMBOL.
• WE CAN APPLY PRODUCTIONS ONE AT A TIME IN SEQUENCE & THAT WILL PRODUCES A
DERIVATION.
• A DERIVATION IS A SEQUENCE OF PRODUCTIONS
A -> … -> … ->… -> … -> …
• A DERIVATION CAN BE DRAWN AS A TREE
– START SYMBOL IS THE TREE’S ROOT
– FOR A PRODUCTION X -> Y1…Yn ADD CHILDREN Y1…Yn TO NODE X
• GRAMMAR
E -> E + E | E * E | (E) | ID
• STRING
ID *ID + ID
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 7
DERIVATIONS
DERIVATIONS ARE OF TWO TYPES:
• RIGHTMOST AND LEFTMOST DERIVATIONS
• LETS DISCUSS WITH EXAMPLE
GRAMMAR: E -> E + E | E * E | -E | (E) | ID
STRING :(ID+ID)
LEFT MOST DERIVATION RIGHT MOST DERIVATION
E E
= (E) = (E)
= (E+E) = (E+E)
= (ID+E) = (E+ID)
=(ID+ID) =(ID+ID)
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 8
DERIVATIONS
• NOW WE'RE GOING TO PARSE THIS STRING AND WE'RE GOING TO SHOW HOW TO
PRODUCE A DERIVATION FOR THE STRING AND ALSO AT THE SAME TIME BUILD
THE TREE.
• PARSE TREES HAVE TERMINALS AT THE LEAVES AND NONTERMINALS AT THE
INTERIOR NODES AND FURTHERMORE, IN-ORDER TRAVERSAL OF THE LEAVES IS
THE ORIGINAL INPUT.
• GRAMMAR
E -> E + E | E * E | (E) | ID
• STRING
ID * ID + ID
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 9
LEFT MOST DERIVATION AND PARSE TREE
E
E
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 10
LEFT MOST DERIVATION AND PARSE TREE
E
E+E E
E + E
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 11
LEFT MOST DERIVATION AND PARSE TREE
E
E+E E
E*E+E E + E
E * E
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 12
LEFT MOST DERIVATION AND PARSE TREE
E
E+E E
E*E+E E + E
id*E+E E * E
id
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 13
LEFT MOST DERIVATION AND PARSE TREE
E
E+E E
E*E+E E + E
id*E+E E * E
id*id+E id id
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 14
LEFT MOST DERIVATION AND PARSE TREE
E
E+E E
E*E+E E + E
id*E+E E * E id
id*id+id id id
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 15
DERIVATIONS
• A PARSE TREE HAS
– TERMINALS AT THE LEAVES
– NON-TERMINALS AT THE INTERIOR NODES
• AN IN-ORDER TRAVERSAL OF THE LEAVES IS THE ORIGINAL INPUT
• THE PARSE TREE SHOWS THE ASSOCIATION OF OPERATIONS, THE INPUT STRING
DOES NOT .
NOTE: THAT RIGHT-MOST AND LEFT-MOST DERIVATIONS HAVE THE SAME PARSE
TREE IF NOT THEN THE GRAMMAR IS AMBIGUOUS GRAMMAR.
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 16
AMBIGUITY
• IF STRING HAS TWO OR MORE RIGHT MOST DERIVATIONS OR TWO OR MORE
LEFT DERIVATIONS THEN THAT STRING WILL HAVE TWO DISTINCT PARSE TREES
AND HENCE GRAMMAR WILL BE AMBIGUOUS.
• AMBIGUITY IS BAD: LEAVES MEANING OF SOME PROGRAMS ILL-DEFINED
• MULTIPLE PARSE TREES FOR SOME PROGRAM THEN THAT ESSENTIALLY MEANS
THAT YOU'RE LEAVING IT UP TO THE COMPILER TO PICK WHICH OF THOSE TWO
POSSIBLE INTERPRETATIONS OF THE PROGRAM YOU WANT IT TO GENERATE
CODE FOR AND THAT'S NOT A GOOD IDEA.
• TO REMOVE AMBIGUITY WE NEED TO REWRITE THE RULES CHECKING OVER
PRECEDENCE AND ASSOCIATIVITY .
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 17
AMBIGUITY
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 18
Eg: The string id +id* id produces two parse tree hence the grammar is ambiguous.
One can remove the ambiguity by rewriting the grammar as introducing new non-terminal instead of r
non-terminal , but it can result in left or right recursion .Hence we have to remove left recursion.
AMBIGUITY
• IF WE HAVE AN AMBIGUOUS GRAMMAR:
E →E * E
E →NUM
• AS THIS DEPENDS ON THE ASSOCIATIVITY OF *,WE USE DIFFERENT REWRITE
RULES FOR DIFFERENT ASSOCIATIVITY .
• IF * IS LEFT-ASSOCIATIVE, WE MAKE THE GRAMMAR LEFT-RECURSIVE BY HAVING
A RECURSIVE REFERENCE TO THE LEFT ONLY OF THE OPERATOR SYMBOL.
UNAMBIGUOUS GRAMMAR: E →E * E’
E →E’
E’→NUM
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 19
LEFT RECURSION
• UNAMBIGUOUS GRAMMAR : E →E * E’
E →E’
E’→NUM
• THIS GRAMMAR IS NOW LEFT RECURSIVE. LEFT RECURSIVE GRAMMAR IS ANY
GRAMMAR THAT HAS A NON-TERMINAL WHERE IF YOU START WITH THAT NON-
TERMINAL AND YOU DO SOME NON-EMPTY SEQUENCE OF RE-WRITES.
• CONSIDER THE LEFT-RECURSIVE GRAMMAR S -> S a | b
• S GENERATES ALL STRINGS STARTING WITH “a” AND FOLLOWED BY ANY
NUMBER OF “b’S”
• CAN REWRITE USING RIGHT-RECURSION
• S ->bS’
S’ ->aS’ |€
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY)
20
EXAMPLES OF LEFT RECURSION
1. E -> E + T | T T -> ID | (E)
2. S ->(L)|X L ->L,S|S
3. S ->S0S1S|01
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY)
21
LEFT FACTORING
• LEFT FACTORING IS A GRAMMAR TRANSFORMATION THAT IS USEFUL
FOR PRODUCING A DETERMINISTIC GRAMMAR FROM NON-
DETERMINISTIC GRAMMAR SUITABLE FOR PREDICTIVE OR TOP-DOWN
PARSING.
• CONSIDER FOLLOWING GRAMMAR:
• STMT -> IF EXPR THEN STMT ELSE STMT
• | IF EXPR THEN STMT
• ON SEEING INPUT IF IT IS NOT CLEAR FOR THE PARSER WHICH
PRODUCTION TO USE
• WE CAN EASILY PERFORM LEFT FACTORING:
• IF WE HAVE A->ΑΒ1 | ΑΒ2 THEN WE REPLACE IT WITH
• A -> ΑA’
• A’ -> Β1 | Β2
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY)
22
EXAMPLES OF LEFT FACTORING
1. S -> iEtS|iEtSES|a E ->b
2. S-> aSSbS|aSaSb|abb|b
3. S-> bSSaaS|bSSaSb|bSb|a
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY)
23
RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 24
Ad

More Related Content

What's hot (20)

Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Finite automata
Finite automataFinite automata
Finite automata
Bipul Roy Bpl
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
Zahid Parvez
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
Dattatray Gandhmal
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Mohammad Ilyas Malik
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
SomnathMore3
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Parsing
ParsingParsing
Parsing
ShrikantSharma86
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
Zahid Parvez
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Mohammad Ilyas Malik
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 

Viewers also liked (20)

Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
Richa Sharma
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Jessore University of Science & Technology, Jessore.
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
Iffat Anjum
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
Iffat Anjum
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Binsent Ribera
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
Syntactic Analysis
Syntactic AnalysisSyntactic Analysis
Syntactic Analysis
Aleli Lac
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
Dattatray Gandhmal
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Chetan Mahawar
 
Parsing
ParsingParsing
Parsing
jayashri kolekar
 
Compiler design
Compiler designCompiler design
Compiler design
Ashraf Hossain
 
Data Locality
Data LocalityData Locality
Data Locality
Syam Lal
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
codereplugd
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
Iffat Anjum
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
Gerwin Ocsena
 
Ad

Similar to Compiler design syntax analysis (18)

5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
movocode
 
Syntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdfSyntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
Mattupallipardhu
 
Fuzzy Matching with Apache Spark
Fuzzy Matching with Apache SparkFuzzy Matching with Apache Spark
Fuzzy Matching with Apache Spark
DataWorks Summit
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
ASHOK KUMAR REDDY
 
05SyntaxAnalysis in compiler design notespdf
05SyntaxAnalysis in compiler design notespdf05SyntaxAnalysis in compiler design notespdf
05SyntaxAnalysis in compiler design notespdf
Padamata Rameshbabu
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
BOTTOM_UP_Parsing techniques_compiler design5.pptx
BOTTOM_UP_Parsing techniques_compiler design5.pptxBOTTOM_UP_Parsing techniques_compiler design5.pptx
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
ROOP SAGAR
 
Module 11
Module 11Module 11
Module 11
bittudavis
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
Sandy Smith
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
riddhi viradiya
 
Witchcraft
WitchcraftWitchcraft
Witchcraft
Brooklyn Zelenka
 
compiler design syntax analysis top down parsing
compiler design syntax analysis top down parsingcompiler design syntax analysis top down parsing
compiler design syntax analysis top down parsing
babar532588
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
movocode
 
Syntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdfSyntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Fuzzy Matching with Apache Spark
Fuzzy Matching with Apache SparkFuzzy Matching with Apache Spark
Fuzzy Matching with Apache Spark
DataWorks Summit
 
05SyntaxAnalysis in compiler design notespdf
05SyntaxAnalysis in compiler design notespdf05SyntaxAnalysis in compiler design notespdf
05SyntaxAnalysis in compiler design notespdf
Padamata Rameshbabu
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
BOTTOM_UP_Parsing techniques_compiler design5.pptx
BOTTOM_UP_Parsing techniques_compiler design5.pptxBOTTOM_UP_Parsing techniques_compiler design5.pptx
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
ROOP SAGAR
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
Sandy Smith
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
riddhi viradiya
 
compiler design syntax analysis top down parsing
compiler design syntax analysis top down parsingcompiler design syntax analysis top down parsing
compiler design syntax analysis top down parsing
babar532588
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Ad

Recently uploaded (20)

Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 

Compiler design syntax analysis

  • 1. COMPILER DESIGN SYNTAX ANALYSIS RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 1 Ms. RICHA SHARMA Assistant Professor [email protected] Lovely Professional University
  • 2. SYNTAX ANALYSIS INTRODUCTION • LEXICAL PHASE IS IMPLEMENTED ON FINITE AUTOMATA & FINITE AUTOMATA CAN REALLY ONLY EXPRESS THINGS WHERE YOU CAN COUNT MODULUS ON K. • REGULAR LANGUAGES – THE WEAKEST FORMAL LANGUAGES WIDELY USED – MANY APPLICATIONS – CAN’T HANDLE ITERATION & NESTED LOOPS(NESTED IF ELSE ). TO SUMMARIZE, THE LEXER TAKES A STRING OF CHARACTER AS INPUT AND PRODUCES A STRING OF TOKENS AS OUTPUT. THAT STRING OF TOKENS IS THE INPUT TO THE PARSER WHICH TAKES A STRING OF TOKENS AND PRODUCES A PARSE TREE OF THE PROGRAM. SOMETIMES THE PARSE TREE IS ONLY IMPLICIT. SO THE, A COMPILER MAY NEVER ACTUALLY BUILD THE FULL PARSE TREE. RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 2
  • 3. Lexical Analyzer Parser Source program token getNext Token Symbol table Parse tree Rest of Front End Intermediate representation ROLE OF SYNTAX ANALYSIS/PARSER RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 3
  • 4. CONTEXT FREE GRAMMARS expression -> expression + term expression -> expression – term expression -> term term -> term * factor term -> term / factor term -> factor factor -> (expression) factor -> id S - IS A FINITE SET OF TERMINALS N - IS A FINITE SET OF NON-TERMINALS P - IS A FINITE SUBSET OF PRODUCTION RULES S - IS THE START SYMBOL G=(S ,N,P,S) • A GRAMMAR DERIVES STRINGS BY BEGINNING WITH START SYMBOL AND REPEATEDLY REPLACING A NON TERMINAL BY THE RIGHT HAND SIDE OF A PRODUCTION FOR THAT NON TERMINAL. • FROM THE START SYMBOL OF A GRAMMAR G FORM THE LANGUAGE L(G) DEFINED BY THE GRAMMAR THE STRINGS THAT CAN BE DERIVED . RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 4
  • 5. RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 5 • PROGRAMMING LANGUAGES HAVE RECURSIVE STRUCTURE • CONTEXT-FREE GRAMMARS ARE A NATURAL NOTATION FOR THIS RECURSIVE STRUCTURE . NOT ALL STRINGS OF TOKENS ARE PROGRAMS . . . . . . PARSER MUST DISTINGUISH BETWEEN VALID AND INVALID STRINGS OF TOKENS WE NEED : – A LANGUAGE :FOR DESCRIBING VALID STRINGS OF TOKENS – A METHOD: FOR DISTINGUISHING VALID FROM INVALID STRINGS OF TOKENS CONTEXT FREE GRAMMARS
  • 6. RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) E ::= T | E + T | E - T T ::= F | T * F |T / F F ::= id | (E) • ARITHMETIC EXPRESSIONS • STATEMENTS If Statement ::= if E then Statement else Statement CONTEXT FREE GRAMMAR EXAMPLES Steps: 1. Begin with a string with only the start symbol S 2. Replace any non-terminal X in the string by the right-hand side of some production X -> Y1…Yn 3. Repeat (2) until there are no non- terminals 6
  • 7. DERIVATIONS • DERIVATION IS A SEQUENCE OF PRODUCTIONS SO BEGINNING WITH THE START SYMBOL. • WE CAN APPLY PRODUCTIONS ONE AT A TIME IN SEQUENCE & THAT WILL PRODUCES A DERIVATION. • A DERIVATION IS A SEQUENCE OF PRODUCTIONS A -> … -> … ->… -> … -> … • A DERIVATION CAN BE DRAWN AS A TREE – START SYMBOL IS THE TREE’S ROOT – FOR A PRODUCTION X -> Y1…Yn ADD CHILDREN Y1…Yn TO NODE X • GRAMMAR E -> E + E | E * E | (E) | ID • STRING ID *ID + ID RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 7
  • 8. DERIVATIONS DERIVATIONS ARE OF TWO TYPES: • RIGHTMOST AND LEFTMOST DERIVATIONS • LETS DISCUSS WITH EXAMPLE GRAMMAR: E -> E + E | E * E | -E | (E) | ID STRING :(ID+ID) LEFT MOST DERIVATION RIGHT MOST DERIVATION E E = (E) = (E) = (E+E) = (E+E) = (ID+E) = (E+ID) =(ID+ID) =(ID+ID) RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 8
  • 9. DERIVATIONS • NOW WE'RE GOING TO PARSE THIS STRING AND WE'RE GOING TO SHOW HOW TO PRODUCE A DERIVATION FOR THE STRING AND ALSO AT THE SAME TIME BUILD THE TREE. • PARSE TREES HAVE TERMINALS AT THE LEAVES AND NONTERMINALS AT THE INTERIOR NODES AND FURTHERMORE, IN-ORDER TRAVERSAL OF THE LEAVES IS THE ORIGINAL INPUT. • GRAMMAR E -> E + E | E * E | (E) | ID • STRING ID * ID + ID RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 9
  • 10. LEFT MOST DERIVATION AND PARSE TREE E E RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 10
  • 11. LEFT MOST DERIVATION AND PARSE TREE E E+E E E + E RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 11
  • 12. LEFT MOST DERIVATION AND PARSE TREE E E+E E E*E+E E + E E * E RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 12
  • 13. LEFT MOST DERIVATION AND PARSE TREE E E+E E E*E+E E + E id*E+E E * E id RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 13
  • 14. LEFT MOST DERIVATION AND PARSE TREE E E+E E E*E+E E + E id*E+E E * E id*id+E id id RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 14
  • 15. LEFT MOST DERIVATION AND PARSE TREE E E+E E E*E+E E + E id*E+E E * E id id*id+id id id RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 15
  • 16. DERIVATIONS • A PARSE TREE HAS – TERMINALS AT THE LEAVES – NON-TERMINALS AT THE INTERIOR NODES • AN IN-ORDER TRAVERSAL OF THE LEAVES IS THE ORIGINAL INPUT • THE PARSE TREE SHOWS THE ASSOCIATION OF OPERATIONS, THE INPUT STRING DOES NOT . NOTE: THAT RIGHT-MOST AND LEFT-MOST DERIVATIONS HAVE THE SAME PARSE TREE IF NOT THEN THE GRAMMAR IS AMBIGUOUS GRAMMAR. RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 16
  • 17. AMBIGUITY • IF STRING HAS TWO OR MORE RIGHT MOST DERIVATIONS OR TWO OR MORE LEFT DERIVATIONS THEN THAT STRING WILL HAVE TWO DISTINCT PARSE TREES AND HENCE GRAMMAR WILL BE AMBIGUOUS. • AMBIGUITY IS BAD: LEAVES MEANING OF SOME PROGRAMS ILL-DEFINED • MULTIPLE PARSE TREES FOR SOME PROGRAM THEN THAT ESSENTIALLY MEANS THAT YOU'RE LEAVING IT UP TO THE COMPILER TO PICK WHICH OF THOSE TWO POSSIBLE INTERPRETATIONS OF THE PROGRAM YOU WANT IT TO GENERATE CODE FOR AND THAT'S NOT A GOOD IDEA. • TO REMOVE AMBIGUITY WE NEED TO REWRITE THE RULES CHECKING OVER PRECEDENCE AND ASSOCIATIVITY . RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 17
  • 18. AMBIGUITY RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 18 Eg: The string id +id* id produces two parse tree hence the grammar is ambiguous. One can remove the ambiguity by rewriting the grammar as introducing new non-terminal instead of r non-terminal , but it can result in left or right recursion .Hence we have to remove left recursion.
  • 19. AMBIGUITY • IF WE HAVE AN AMBIGUOUS GRAMMAR: E →E * E E →NUM • AS THIS DEPENDS ON THE ASSOCIATIVITY OF *,WE USE DIFFERENT REWRITE RULES FOR DIFFERENT ASSOCIATIVITY . • IF * IS LEFT-ASSOCIATIVE, WE MAKE THE GRAMMAR LEFT-RECURSIVE BY HAVING A RECURSIVE REFERENCE TO THE LEFT ONLY OF THE OPERATOR SYMBOL. UNAMBIGUOUS GRAMMAR: E →E * E’ E →E’ E’→NUM RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 19
  • 20. LEFT RECURSION • UNAMBIGUOUS GRAMMAR : E →E * E’ E →E’ E’→NUM • THIS GRAMMAR IS NOW LEFT RECURSIVE. LEFT RECURSIVE GRAMMAR IS ANY GRAMMAR THAT HAS A NON-TERMINAL WHERE IF YOU START WITH THAT NON- TERMINAL AND YOU DO SOME NON-EMPTY SEQUENCE OF RE-WRITES. • CONSIDER THE LEFT-RECURSIVE GRAMMAR S -> S a | b • S GENERATES ALL STRINGS STARTING WITH “a” AND FOLLOWED BY ANY NUMBER OF “b’S” • CAN REWRITE USING RIGHT-RECURSION • S ->bS’ S’ ->aS’ |€ RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 20
  • 21. EXAMPLES OF LEFT RECURSION 1. E -> E + T | T T -> ID | (E) 2. S ->(L)|X L ->L,S|S 3. S ->S0S1S|01 RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 21
  • 22. LEFT FACTORING • LEFT FACTORING IS A GRAMMAR TRANSFORMATION THAT IS USEFUL FOR PRODUCING A DETERMINISTIC GRAMMAR FROM NON- DETERMINISTIC GRAMMAR SUITABLE FOR PREDICTIVE OR TOP-DOWN PARSING. • CONSIDER FOLLOWING GRAMMAR: • STMT -> IF EXPR THEN STMT ELSE STMT • | IF EXPR THEN STMT • ON SEEING INPUT IF IT IS NOT CLEAR FOR THE PARSER WHICH PRODUCTION TO USE • WE CAN EASILY PERFORM LEFT FACTORING: • IF WE HAVE A->ΑΒ1 | ΑΒ2 THEN WE REPLACE IT WITH • A -> ΑA’ • A’ -> Β1 | Β2 RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 22
  • 23. EXAMPLES OF LEFT FACTORING 1. S -> iEtS|iEtSES|a E ->b 2. S-> aSSbS|aSaSb|abb|b 3. S-> bSSaaS|bSSaSb|bSb|a RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 23
  • 24. RICHA SHARMA (LOVELY PROFESSIONAL UNIVERSITY) 24