SlideShare une entreprise Scribd logo
1  sur  110
[object Object],[object Object],[object Object],[object Object]
MODULE  II ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
THE ROLE OF THE PARSER Lexical  Analyzer Parser source  program token get next token parse tree Symbol table
The Role of the Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Role of the Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax error handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Error recovery strategies   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Error recovery strategies ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Error recovery strategies ,[object Object],[object Object],[object Object],[object Object]
Context-Free Grammars ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Context-Free Grammars ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Notational Conventions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],E and A are non terminals E is start symbol Others are terminals
Derivations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],* +
CFG - Terminology ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],+ *
Derivation Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Left-Most and Right-Most Derivations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],lm lm lm lm lm rm rm rm rm rm
Parse Tree ,[object Object],[object Object],[object Object],E    -E  E E - E E E E E + - ( ) E E E - ( ) E E id E E E + - ( ) id E E E E E + - ( ) id    -(E)    -(E+E)    -(id+E)    -(id+id)
Ambiguity ,[object Object],[object Object],E    E+E    id+E    id+E*E    id+id*E    id+id*id E    E*E    E+E*E    id+E*E    id+id*E    id+id*id E id E + id id E E * E E E + id E E * E id id
Ambiguity  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ambiguity – “dangling else ” stmt     if   expr  then   stmt  | if   expr  then   stmt  else   stmt  |  other stmts if   E 1   then  if   E 2   then   S 1   else  S 2 stmt if   expr  then   stmt  else   stmt E 1   if   expr  then   stmt  S 2   E 2    S 1 stmt if   expr  then   stmt E 1   if   expr  then   stmt  else   stmt   E 2    S 1    S 2 1 2
Ambiguity  ,[object Object],[object Object],[object Object],[object Object],stmt     matchedstmt  |  unmatchedstmt matchedstmt     if   expr  then   matchedstmt  else   matchedstmt  |  otherstmts unmatchedstmt     if   expr  then   stmt  | if   expr  then   matchedstmt  else   unmatchedstmt
Left Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],+ +
Immediate Left-Recursion A    A    |     where    does not start with A  eliminate immediate left recursion A       A ’ A ’        A ’   |     an equivalent grammar A    A   1  | ... | A   m  |   1  | ... |   n   where   1  ...   n  do not start with A  eliminate immediate left recursion A      1  A ’  | ... |   n  A ’ A ’       1  A ’   | ... |   m  A ’   |     an equivalent grammar In general,
Immediate Left-Recursion -- Example E    E+T  |  T T    T*F  |  F F    id  |  (E) E    T E ’ E ’    +T E ’  |   T    F T ’ T ’     *F T ’   |   F    id  |  (E)    eliminate immediate left recursion
Left-Factoring  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Left-Factoring -- Algorithm  ,[object Object],[object Object],[object Object],[object Object],[object Object]
Left-Factoring – Example1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Top-Down Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive-Descent Parsing (uses Backtracking) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],fails, backtrack
Predictive Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Predictive Parser (example) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Predictive Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Predictive Parsing  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Predictive Parsing  ,[object Object],[object Object],[object Object],[object Object]
Recursive Predictive Parsing (Example) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],follow set of B  first set of C
Non-Recursive Predictive Parsing -- LL(1) Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Non-Recursive Predictive Parsing  Predictive Parsing  Program Parsing Table M a  +  b  $ X Y Z $ INPUT OUTPUT STACK
LL(1) Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LL(1) Parser – Parser Actions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Non Recursive Predictive Parsing program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LL(1) Parser – Example1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Input : abba B    bB B      B S    aBa S $ b a
LL(1) Parser – Example1  Outputs: S    aBa  B    bB  B    bB    B        Derivation(left-most):  S   aBa   abBa   abbBa   abba S B a a B B b b  parse tree
LL(1) Parser – Example2 E    TE ’ E ’     +TE ’  |   T    FT ’ T ’     *FT ’  |   F    (E)  |  id E    E+T  |  T T    T*F  |  F F    id  |  (E) Input : id +id F    (E) F    id F T ’       T ’       T ’     *FT ’ T ’       T ’ T    FT ’ T    FT ’ T E ’       E ’       E ’     +TE ’ E ’ E    TE ’ E    TE ’ E $ ) ( * + id
LL(1) Parser – Example2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Constructing LL(1) Parsing Tables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],* *
Compute FIRST for Any String X ,[object Object],[object Object],[object Object],[object Object]
FIRST Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Compute FOLLOW (for non-terminals) ,[object Object],[object Object],[object Object],[object Object]
FOLLOW Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Constructing LL(1) Parsing Table -- Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object]
Constructing LL(1) Parsing Table -- Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LL(1) PARSING TABLE F    (E) F    id F T ’       T ’       T ’     *FT ’ T ’       T ’ T    FT ’ T    FT ’ T E ’       E ’       E ’     +TE ’ E ’ E    TE ’ E    TE ’ E $ ) ( * + id
LL(1) Grammars ,[object Object],[object Object],[object Object],[object Object],[object Object]
A Grammar which is not LL(1) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],C    b C E      E    e S E      E S    iCtSE S    a S $ t i e b a
A Grammar which is not LL(1)  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Properties of LL(1) Grammars ,[object Object],[object Object],[object Object],[object Object]
Error Recovery in Predictive Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Panic-Mode Error Recovery in LL(1) Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Panic-Mode Error Recovery in LL(1) Parsing example synch synch F    (E) synch synch F    id F T ’       T ’       T ’     *FT ’ T ’       T ’ synch synch T    FT ’ synch T    FT ’ T E ’       E ’       E ’     +TE ’ E ’ synch synch E    TE ’ E    TE ’ E $ ) ( * + id
Panic-Mode Error Recovery in LL(1) Parsing example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Phrase-Level Error Recovery ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Bottom-Up Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Shift-Reduce Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],* rm rm rm
Shift-Reduce Parsing -- Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],rm rm rm rm
Handle ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],rm rm *
Handle Pruning ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],rm rm rm rm rm
A Shift-Reduce Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Stack Implementation of A Shift-Reduce Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Stack Implementation of A Shift-Reduce Parser  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conflicts During Shift-Reduce Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Shift-Reduce Parsers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],SLR CFG CLR LALR
Operator-Precedence Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Not operator grammar
Operator-Precedence Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Operator precedence relations
Operator-Precedence Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operator-Precedence Parsing Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Precedence Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LR Parsers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LR Parsing Algorithm stack input output S 0 X 1 S 1 . . X m-1 S m-1 X m S m $ a n ... a i ... a 1 Goto Table non-terminal s t  each item is a  a state number t e s Action Table terminals and $ s t  four different  a  actions t e s LR Parsing Algorithm
A Configuration of LR Parsing Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Actions of A LR-Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
(SLR) Parsing Tables for Expression Grammar Action Table Goto Table 1)  E    E+T 2)  E    T 3)  T    T*F 4)  T    F 5)  F    (E) 6)  F    id r3 r3 r3 r3 10 r5 r5 r5 r5 11 r1 r1 s7 r1 9 s11 s6 8 10 s4 s5 7 3 9 s4 s5 6 r6 r6 r6 r6 5 3 2 8 s4 s5 4 r4 r4 r4 r4 3 r2 r2 s7 r2 2 acc s6 1 3 2 1 s4 s5 0 F T  E $ ) ( * + id state
Actions of A (S)LR-Parser -- Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Constructing SLR Parsing Tables – LR(0) Item ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Closure Operation ,[object Object],[object Object],[object Object],[object Object]
The Closure Operation  -- Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Goto Operation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Construction of The Canonical LR(0) Collection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Canonical LR(0) Collection -- Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transition Diagram (DFA) of Goto Function I 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 to I 4 to I 5 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id ( F * E E + T T T ) F F F ( id id ( * ( id +
Constructing SLR Parsing Table  (of an augumented grammar G’) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Parsing Tables of Expression Grammar Action Table Goto Table r3 r3 r3 r3 10 r5 r5 r5 r5 11 r1 r1 s7 r1 9 s11 s6 8 10 s4 s5 7 3 9 s4 s5 6 r6 r6 r6 r6 5 3 2 8 s4 s5 4 r4 r4 r4 r4 3 r2 r2 s7 r2 2 acc s6 1 3 2 1 s4 s5 0 F T  E $ ) ( * + id state
SLR(1) Grammar ,[object Object],[object Object],[object Object]
shift/reduce and reduce/reduce conflicts ,[object Object],[object Object],[object Object]
CANONICAL LR PARSER(CLR) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Canonical Collection of Sets of LR(1) Items ,[object Object],[object Object],[object Object],[object Object]
goto operation ,[object Object],[object Object]
Construction of The Canonical LR(1) Collection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Short Notation for The Sets of LR(1) Items ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Construction of LR(1) Parsing Tables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
CLR Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Canonical collections( LR(1) ITEMS)
d S->.S,$ S->.CC,$ C->.cC,c/d C->.d,c/d I 0 C->d.,c/d I 4 S->C.C,$ C->.Cc,$ C->.d,$ I 2 C->c.C,$ C->.cC,$ C->.d,$ I 6 S->CC.,$ I 5 C->.cC.,$ I 9 C->d.,$ I 7 C->cC.,c/d I 8 C->c.C,c/d C->.cC,c/d C->.d,c/d I 3 S’->S.,$ I 1 S C C C c d C c d c c
CLR PARSING TABLE r2 9 r2 r2 8 r3 7 9 s7 s6 6 r1 5 r3 r3 4 8 s4 s3 3 5 s7 s6 2 acc 1 2 1 s4 s3 0 C S $ d c goto action STATE
LALR Parsing Tables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating LALR Parsing Tables ,[object Object],[object Object],[object Object],[object Object]
The Core of A Set of LR(1) Items ,[object Object],[object Object],[object Object],[object Object]
Creation of LALR Parsing Tables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LALR Items ,[object Object],[object Object],[object Object],[object Object],[object Object]
LALR PARSING TABLE r2 r2 r2 89 r1 5 r3 r3 r3 47 89 s47 s36 36 5 s47 s36 2 acc 1 2 1 s47 s36 0 C S $ d c goto action STATE

Contenu connexe

Tendances

System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
Manoj Patil
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
Manoj Patil
 

Tendances (20)

Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)
 
Regular expressions and languages pdf
Regular expressions and languages pdfRegular expressions and languages pdf
Regular expressions and languages pdf
 
Parse Tree
Parse TreeParse Tree
Parse Tree
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Stack and its usage in assembly language
Stack and its usage in assembly language Stack and its usage in assembly language
Stack and its usage in assembly language
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Computer architecture addressing modes and formats
Computer architecture addressing modes and formatsComputer architecture addressing modes and formats
Computer architecture addressing modes and formats
 
ARIES Recovery Algorithms
ARIES Recovery AlgorithmsARIES Recovery Algorithms
ARIES Recovery Algorithms
 
1D Array in Assembly Language
1D Array in Assembly Language1D Array in Assembly Language
1D Array in Assembly Language
 

En vedette

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
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
venkatam
 
Direct linking loader
Direct linking loaderDirect linking loader
Direct linking loader
babyparul
 

En vedette (20)

Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Ch5a
Ch5aCh5a
Ch5a
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Software tools
Software toolsSoftware tools
Software tools
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.
 
Ch4b
Ch4bCh4b
Ch4b
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
 
Direct linking loader
Direct linking loaderDirect linking loader
Direct linking loader
 
Different types of Editors in Linux
Different types of Editors in LinuxDifferent types of Editors in Linux
Different types of Editors in Linux
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 

Similaire à Module 11

Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
venkatapranaykumarGa
 
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
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
bolovv
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
vip_du
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
bolovv
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
riddhi viradiya
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
wigewej294
 

Similaire à Module 11 (20)

Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptx
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
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
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Syntax part1
Syntax part1Syntax part1
Syntax part1
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 

Module 11

  • 1.
  • 2.
  • 3. THE ROLE OF THE PARSER Lexical Analyzer Parser source program token get next token parse tree Symbol table
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20. Ambiguity – “dangling else ” stmt  if expr then stmt | if expr then stmt else stmt | other stmts if E 1 then if E 2 then S 1 else S 2 stmt if expr then stmt else stmt E 1 if expr then stmt S 2 E 2 S 1 stmt if expr then stmt E 1 if expr then stmt else stmt E 2 S 1 S 2 1 2
  • 21.
  • 22.
  • 23. Immediate Left-Recursion A  A  |  where  does not start with A  eliminate immediate left recursion A   A ’ A ’   A ’ |  an equivalent grammar A  A  1 | ... | A  m |  1 | ... |  n where  1 ...  n do not start with A  eliminate immediate left recursion A   1 A ’ | ... |  n A ’ A ’   1 A ’ | ... |  m A ’ |  an equivalent grammar In general,
  • 24. Immediate Left-Recursion -- Example E  E+T | T T  T*F | F F  id | (E) E  T E ’ E ’  +T E ’ |  T  F T ’ T ’  *F T ’ |  F  id | (E)  eliminate immediate left recursion
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37. Non-Recursive Predictive Parsing Predictive Parsing Program Parsing Table M a + b $ X Y Z $ INPUT OUTPUT STACK
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43. LL(1) Parser – Example1 Outputs: S  aBa B  bB B  bB B   Derivation(left-most): S  aBa  abBa  abbBa  abba S B a a B B b b  parse tree
  • 44. LL(1) Parser – Example2 E  TE ’ E ’  +TE ’ |  T  FT ’ T ’  *FT ’ |  F  (E) | id E  E+T | T T  T*F | F F  id | (E) Input : id +id F  (E) F  id F T ’   T ’   T ’  *FT ’ T ’   T ’ T  FT ’ T  FT ’ T E ’   E ’   E ’  +TE ’ E ’ E  TE ’ E  TE ’ E $ ) ( * + id
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53. LL(1) PARSING TABLE F  (E) F  id F T ’   T ’   T ’  *FT ’ T ’   T ’ T  FT ’ T  FT ’ T E ’   E ’   E ’  +TE ’ E ’ E  TE ’ E  TE ’ E $ ) ( * + id
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60. Panic-Mode Error Recovery in LL(1) Parsing example synch synch F  (E) synch synch F  id F T ’   T ’   T ’  *FT ’ T ’   T ’ synch synch T  FT ’ synch T  FT ’ T E ’   E ’   E ’  +TE ’ E ’ synch synch E  TE ’ E  TE ’ E $ ) ( * + id
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79. LR Parsing Algorithm stack input output S 0 X 1 S 1 . . X m-1 S m-1 X m S m $ a n ... a i ... a 1 Goto Table non-terminal s t each item is a a state number t e s Action Table terminals and $ s t four different a actions t e s LR Parsing Algorithm
  • 80.
  • 81.
  • 82. (SLR) Parsing Tables for Expression Grammar Action Table Goto Table 1) E  E+T 2) E  T 3) T  T*F 4) T  F 5) F  (E) 6) F  id r3 r3 r3 r3 10 r5 r5 r5 r5 11 r1 r1 s7 r1 9 s11 s6 8 10 s4 s5 7 3 9 s4 s5 6 r6 r6 r6 r6 5 3 2 8 s4 s5 4 r4 r4 r4 r4 3 r2 r2 s7 r2 2 acc s6 1 3 2 1 s4 s5 0 F T E $ ) ( * + id state
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90. Transition Diagram (DFA) of Goto Function I 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 to I 4 to I 5 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id ( F * E E + T T T ) F F F ( id id ( * ( id +
  • 91.
  • 92. Parsing Tables of Expression Grammar Action Table Goto Table r3 r3 r3 r3 10 r5 r5 r5 r5 11 r1 r1 s7 r1 9 s11 s6 8 10 s4 s5 7 3 9 s4 s5 6 r6 r6 r6 r6 5 3 2 8 s4 s5 4 r4 r4 r4 r4 3 r2 r2 s7 r2 2 acc s6 1 3 2 1 s4 s5 0 F T E $ ) ( * + id state
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103. d S->.S,$ S->.CC,$ C->.cC,c/d C->.d,c/d I 0 C->d.,c/d I 4 S->C.C,$ C->.Cc,$ C->.d,$ I 2 C->c.C,$ C->.cC,$ C->.d,$ I 6 S->CC.,$ I 5 C->.cC.,$ I 9 C->d.,$ I 7 C->cC.,c/d I 8 C->c.C,c/d C->.cC,c/d C->.d,c/d I 3 S’->S.,$ I 1 S C C C c d C c d c c
  • 104. CLR PARSING TABLE r2 9 r2 r2 8 r3 7 9 s7 s6 6 r1 5 r3 r3 4 8 s4 s3 3 5 s7 s6 2 acc 1 2 1 s4 s3 0 C S $ d c goto action STATE
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110. LALR PARSING TABLE r2 r2 r2 89 r1 5 r3 r3 r3 47 89 s47 s36 36 5 s47 s36 2 acc 1 2 1 s47 s36 0 C S $ d c goto action STATE