SlideShare a Scribd company logo
Compiler Components & Generators
Traditional Parsing Algorithms

Guido Wachsmuth




       Delft
                                       Course IN4303
       University of
       Technology                Compiler Construction
       Challenge the future
Recap: Lexical Analysis
lessons learned

What are the formalisms to describe regular languages?
  •   regular grammars
  •   regular expressions
  •   finite state automata
Why are these formalisms equivalent?
  •   constructive proofs
How can we generate compiler tools from that?
  •   implement DFAs
  •   generate transition tables




                                            Traditional Parsing Algorithms   2
Overview
today’s lecture




                  Lexical Analysis   3
Overview
today’s lecture

the exam




                  Lexical Analysis   3
Overview
today’s lecture

the exam

assignment 3




                  Lexical Analysis   3
Overview
today’s lecture

the exam

assignment 3

efficient parsing algorithms

  •   predictive parsing
  •   LR parsing




                               Lexical Analysis   3
Overview
today’s lecture

the exam

assignment 3

efficient parsing algorithms

  •   predictive parsing
  •   LR parsing

grammar classes

  •   LL(k) grammars
  •   LR(k) grammars



                               Lexical Analysis   3
I
the exam




           Lexical Analysis   4
The exam

topics

   •     introduction
   •     declarative language definition (theory)
   •     compiling imperative & object-oriented languages
   •     compiler components & generators

style

   •     theory in praxis
   •     understanding over facts
   •     open questions
   •     closed book

                                                     Traditional Parsing Algorithms   5
II
assignment




             Lexical Analysis   6
MiniJava Compiler
schedule

Lab 1
   explore Java Bytecode & Jasmin
   first generator version

Lab 2 & 3
   generator for statements & expressions
   generator for classes, fields & methods

Lab 4
   liveness analysis

submission due Jan 9, 23:59

                                             Traditional Parsing Algorithms   7
MiniJava Compiler
code generation




class Main {

	   public static void main(String[] args) {
	   	 System.out.println(42) ;
	   }
}




                                               Traditional Parsing Algorithms   8
Recap: Code Generation
to-jbc:   Nil()   -> [ ACONST_NULL() ]
to-jbc:   NoVal() -> [ NOP() ]
to-jbc:   Seq(es) -> <mapconcat(to-jbc)> es
	
to-jbc:   Int(i)    -> [ LDC(Int(i))
to-jbc:   String(s) -> [ LDC(String(s))
	
to-jbc:   Bop(op, e1, e2) -> <concat> [ <to-jbc> e1, <to-jbc> e2, <to-jbc> op ]
	
to-jbc:   PLUS()    ->   [   IADD()   ]
to-jbc:   MINUS()   ->   [   ISUB()   ]
to-jbc:   MUL()     ->   [   IMUL()   ]
to-jbc:   DIV()     ->   [   IDIV()   ]

to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc>          lhs ]
	
to-jbc:     Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) =>           INT()
to-jbc:     Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) =>           STRING()
lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) =>          INT()
lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) =>          STRING()	




                                                     Traditional Parsing Algorithms   9
MiniJava Compiler
liveness analysis



.method public static main([Ljava/lang/String;)V
	 	
   .limit locals 3
   .limit stack 4
	 	
   .var 1 is x I from startX to endX
   .var 2 is y Z from startY to endY

   ...
	 	
.end method




                                                   Traditional Parsing Algorithms 10
MiniJava Compiler
submission

procedure
   ensure compiler works
   add 5 positive test cases
   include your Net ID in project name
   export project as zip file
   ensure editor works when imported
   submit zip file via Blackboard

submission due Jan 9, 23:59
   delay costs you 25 points
   plus 1 point per hour or part thereof

                                           Traditional Parsing Algorithms 11
Assignment: MiniJava Editor
grades

code generation 60 points
   classes 10 points

   fields & methods 15 points

   statements 15 points

   expressions 20 points

liveness analysis 30+5 points
   maximum stack size 5 bonus points

test cases 10 points


                                       Traditional Parsing Algorithms 12
III
predictive parsing




                     Lexical Analysis 13
Recap: A Theory of Language
formal languages




                        Traditional Parsing Algorithms 14
Recap: A Theory of Language
formal languages

vocabulary Σ
   finite, nonempty set of elements (words, letters)
   alphabet




                                                       Traditional Parsing Algorithms 14
Recap: A Theory of Language
formal languages

vocabulary Σ
   finite, nonempty set of elements (words, letters)
   alphabet

string over Σ
   finite sequence of elements chosen from Σ
   word, sentence, utterance




                                                       Traditional Parsing Algorithms 14
Recap: A Theory of Language
formal languages

vocabulary Σ
   finite, nonempty set of elements (words, letters)
   alphabet

string over Σ
   finite sequence of elements chosen from Σ
   word, sentence, utterance

formal language λ
   set of strings over a vocabulary Σ
   λ ⊆ Σ*


                                                       Traditional Parsing Algorithms 14
Recap: A Theory of Language
formal grammars




                        Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal grammars

formal grammar G = (N, Σ, P, S)
   nonterminal symbols N
   terminal symbols Σ
   production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*
   start symbol S∈N




                                                   Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal grammars

formal grammar G = (N, Σ, P, S)
   nonterminal symbols N      nonterminal symbol
   terminal symbols Σ
   production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*
   start symbol S∈N




                                                   Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal grammars

formal grammar G = (N, Σ, P, S)
   nonterminal symbols N
   terminal symbols Σ
   production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*
   start symbol S∈N
                        context




                                                   Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal grammars

formal grammar G = (N, Σ, P, S)
   nonterminal symbols N
   terminal symbols Σ
   production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*
   start symbol S∈N
                                replacement




                                                   Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal grammars

formal grammar G = (N, Σ, P, S)
   nonterminal symbols N
   terminal symbols Σ
   production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*
   start symbol S∈N

grammar classes
   type-0, unrestricted
   type-1, context-sensitive: (a A c, a b c)
   type-2, context-free: P ⊆ N × (N∪Σ)*
   type-3, regular: (A, x) or (A, xB)

                                                   Traditional Parsing Algorithms 15
Recap: A Theory of Language
formal languages




                        Traditional Parsing Algorithms 16
Recap: A Theory of Language
formal languages

formal grammar G = (N, Σ, P, S)




                                  Traditional Parsing Algorithms 16
Recap: A Theory of Language
formal languages

formal grammar G = (N, Σ, P, S)

derivation relation     G   ⊆ (N∪Σ)* × (N∪Σ)*

    w   G   w’

            ∃(p, q)∈P: ∃u,v∈(N∪Σ)*:
            w=u p v ∧ w’=u q v




                                                Traditional Parsing Algorithms 16
Recap: A Theory of Language
formal languages

formal grammar G = (N, Σ, P, S)

derivation relation     G    ⊆ (N∪Σ)* × (N∪Σ)*

    w   G   w’

            ∃(p, q)∈P: ∃u,v∈(N∪Σ)*:
            w=u p v ∧ w’=u q v

formal language L(G) ⊆ Σ*
    L(G) = {w∈Σ* | S     G
                             *
                                 w}




                                                 Traditional Parsing Algorithms 16
Recap: A Theory of Language
formal languages

formal grammar G = (N, Σ, P, S)

derivation relation     G    ⊆ (N∪Σ)* × (N∪Σ)*

    w   G   w’

            ∃(p, q)∈P: ∃u,v∈(N∪Σ)*:
            w=u p v ∧ w’=u q v

formal language L(G) ⊆ Σ*
    L(G) = {w∈Σ* | S     G
                             *
                                 w}


classes of formal languages

                                                 Traditional Parsing Algorithms 16
Predictive parsing
recursive descent



Exp → “while” Exp “do” Exp




public void parseExp() {
   consume(WHILE);
   parseExp();
   consume(DO);
   parseExp();
}



                             Traditional Parsing Algorithms 17
Predictive parsing
look ahead


Exp → “while” Exp “do” Exp
Exp → “if” Exp “then” Exp “else” Exp




public void parseExp() {

    switch current() {
       case WHILE: consume(WHILE); parseExp(); ...; break;
       case IF   : consume(IF); parseExp(); ...; break;
       default   : error();
    }
}


                                                     Traditional Parsing Algorithms 18
Predictive parsing
parse table

rows                                    T1            T2              T3            ...

  •    nonterminal symbols N    N1    N1 →...                     N1 →...

  •    symbol to parse          N2                N2 →...

columns                         N3                N3 →... N3 →...
                                N4    N4 →...
  •    terminal symbols Σ   k

                                N5                N5 →...
  •    look ahead k
                                N6    N6 →... N6 →...
entries
                                N7                                N7 →...
  •    production rules P
                                N8    N8 →... N8 →... N8 →...
  •    possible conflicts
                                ...

                                                Traditional Parsing Algorithms 19
Predictive parsing
automaton


                     input


                t1 … tn $




                             parse table
            S
 stack
            $


                              Traditional Parsing Algorithms 20
Predictive parsing
automaton




                     x   …   $




            x
            …
            $


                                 Traditional Parsing Algorithms 21
Predictive parsing
automaton




                     …   $




            …
            $


                             Traditional Parsing Algorithms 21
Predictive parsing
automaton




                           x   …      $



                           x


            Xi   Xi
                      Xi → Y1... Yk
            …
            $


                                          Traditional Parsing Algorithms 22
Predictive parsing
automaton




                           x   …      $



            Y1             x
            …
            Yk   Xi
                      Xi → Y1... Yk
            …
            $


                                          Traditional Parsing Algorithms 22
Predictive parsing
filling the table




entry (X, w)∈P at row X and column T

   T∈FIRST(w)
   nullable(w) ∧ T∈FOLLOW(X)




                                       Traditional Parsing Algorithms 23
Predictive parsing
nullable


nullable(X)
   (X, ε) ∈ P    nullable(X)

   (X0, X1 … Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk)        nullable(X0)




nullable(w)
   nullable(ε)
   nullable(X1 … Xk) = nullable(X1) ∧ … ∧ nullable(Xk)


                                                       Traditional Parsing Algorithms 24
Predictive parsing
first sets


FIRST(X)
   X∈Σ : FIRST(X) = {X}
   (X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi)   FIRST(X0) ⊇ FIRST(Xi+1)




FIRST(w)
   FIRST(ε) = {}
   ¬nullable(X)     FIRST(Xw) = FIRST(X)

   nullable(X)     FIRST(Xw) = FIRST(X) ∪ FIRST(w)


                                                   Traditional Parsing Algorithms 25
Predictive parsing
follow sets




FOLLOW(X)
   (X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk)   FOLLOW(Xi) ⊇ FOLLOW(X0)

   (X0, X1 … Xi … Xk)∈P   FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk)




                                                   Traditional Parsing Algorithms 26
Predictive parsing
encoding precedence

Exp   →   Num
Exp   →   “(” Exp “)”
Exp   →   Exp “*” Exp
Exp   →   Exp “+” Exp


Fact → Num
Fact → “(” Exp “)”
Term → Term “*” Fact
Term → Fact
Exp → Exp “+” Term
Exp → Term
                        Traditional Parsing Algorithms 27
Predictive parsing
eliminating left recursion


Term → Term “*” Fact
Term → Fact
Exp → Exp “+” Term
Exp → Term

Term’ → “*” Fact Term’
Term’ →
Term → Fact Term’
Exp’ → “+” Term Exp’
Exp’ →
Exp → Term Exp’
                             Traditional Parsing Algorithms 28
Predictive parsing
left factoring



Exp → “if” Exp “then” Exp “else” Exp
Exp → “if” Exp “then” Exp




Exp → “if” Exp “then” Exp Else
Else → “else” Exp
Else →


                                       Traditional Parsing Algorithms 29
Grammar classes
           context-free grammars




                                   Traditional Parsing Algorithms 30
Grammar classes
           context-free grammars




                 LL(0)




                                   Traditional Parsing Algorithms 30
Grammar classes
           context-free grammars




                 LL(1)




                 LL(0)




                                   Traditional Parsing Algorithms 30
Grammar classes
           context-free grammars


                 LL(k)




                 LL(1)




                 LL(0)




                                   Traditional Parsing Algorithms 30
coffee break




               Traditional Parsing Algorithms 31
IV
LR parsing




             Lexical Analysis 32
LR parsing
idea

problems with LL parsing

  •    predicting right rule
  •    left recursion

LR parsing

  •    see whole left-hand side of a rule
  •    look ahead
  •    shift or reduce




                                            Traditional Parsing Algorithms 33
LR parsing
example
 stack                                 input


$            7   *   3   +     7     *      3     $




                     Traditional Parsing Algorithms 34
LR parsing
example


$            7   *   3   +     7     *      3     $




                     Traditional Parsing Algorithms 34
LR parsing
example


$   7        *   3   +     7     *      3     $




                 Traditional Parsing Algorithms 34
LR parsing
example


$   7        *   3   +     7     *      3     $
$   E        *   3   +     7     *     3      $




                 Traditional Parsing Algorithms 34
LR parsing
example


$   7        *   3   +     7     *      3     $
$   E   *        3   +     7     *     3      $




                 Traditional Parsing Algorithms 34
LR parsing
example


$   7           *   3   +     7     *      3     $
$   E   *   3           +     7     *     3      $




                    Traditional Parsing Algorithms 34
LR parsing
example


$   7           *   3   +     7     *      3     $
$   E   *   3           +     7     *     3      $
$   E   *   E           +     7     *     3      $




                    Traditional Parsing Algorithms 34
LR parsing
example


$   7           *   3   +     7     *      3     $
$   E   *   3           +     7     *     3      $
$   E   *   E           +     7     *     3      $
$   E                   +     7     *     3      $




                    Traditional Parsing Algorithms 34
LR parsing
example


$   7           *   3   +     7     *      3     $
$   E   *   3           +     7     *     3      $
$   E   *   E           +     7     *     3      $
$   E   +                     7     *     3      $




                    Traditional Parsing Algorithms 34
Ad

More Related Content

What's hot (20)

Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
Eelco Visser
 
LL Parsing
LL ParsingLL Parsing
LL Parsing
Eelco Visser
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
Eelco Visser
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
Eelco Visser
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
Guido Wachsmuth
 
Type analysis
Type analysisType analysis
Type analysis
Eelco Visser
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
Eelco Visser
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
Eelco Visser
 
Programming languages
Programming languagesProgramming languages
Programming languages
Eelco Visser
 
Ch04
Ch04Ch04
Ch04
Hankyo
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
Eelco Visser
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
Ch03
Ch03Ch03
Ch03
Hankyo
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
 
Ch4a
Ch4aCh4a
Ch4a
kinnarshah8888
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Eelco Visser
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
Eelco Visser
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
Eelco Visser
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
Eelco Visser
 
Programming languages
Programming languagesProgramming languages
Programming languages
Eelco Visser
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
Eelco Visser
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Eelco Visser
 

Viewers also liked (20)

An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
Omar Bashir
 
80386 Architecture
80386 Architecture80386 Architecture
80386 Architecture
Rohit Choudhury
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
Guido Wachsmuth
 
Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project
Bhuvnesh Pratap
 
Intel processors
Intel processorsIntel processors
Intel processors
Kapil Raghuwanshi
 
Ch5b
Ch5bCh5b
Ch5b
kinnarshah8888
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
01 intel processor architecture core
01 intel processor architecture core01 intel processor architecture core
01 intel processor architecture core
sssuhas
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
Evolution of Intel Processors
Evolution of Intel ProcessorsEvolution of Intel Processors
Evolution of Intel Processors
Shad Ahmad Zaidi
 
Intel Processors
Intel ProcessorsIntel Processors
Intel Processors
home
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
Sorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingSorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card Sorting
Stephen Anderson
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Ch5a
Ch5aCh5a
Ch5a
kinnarshah8888
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Sorting
SortingSorting
Sorting
Ghaffar Khan
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
Omar Bashir
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
Guido Wachsmuth
 
Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project
Bhuvnesh Pratap
 
01 intel processor architecture core
01 intel processor architecture core01 intel processor architecture core
01 intel processor architecture core
sssuhas
 
Evolution of Intel Processors
Evolution of Intel ProcessorsEvolution of Intel Processors
Evolution of Intel Processors
Shad Ahmad Zaidi
 
Intel Processors
Intel ProcessorsIntel Processors
Intel Processors
home
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
Sorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingSorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card Sorting
Stephen Anderson
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Ad

Similar to Compiler Components and their Generators - Traditional Parsing Algorithms (20)

Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
daniloalbay1
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language Definition
Eelco Visser
 
Parsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Parsers Combinators in Scala, Ilya @lambdamix KliuchnikovParsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Parsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Vasil Remeniuk
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
Mahesh Kumar Chelimilla
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
eliasabdi2024
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
Pierre de Lacaze
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
Syntactic Analysis in Compiler Construction
Syntactic Analysis in Compiler ConstructionSyntactic Analysis in Compiler Construction
Syntactic Analysis in Compiler Construction
voyoc79528
 
Ch2
Ch2Ch2
Ch2
kinnarshah8888
 
Prolog & lisp
Prolog & lispProlog & lisp
Prolog & lisp
Ismail El Gayar
 
A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query Languages
Kim Mens
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
Anshul Sharma
 
Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language Parser
Yodalee
 
Introduction
IntroductionIntroduction
Introduction
Royalzig Luxury Furniture
 
Introduction
IntroductionIntroduction
Introduction
Royalzig Luxury Furniture
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...
Vissarion Fisikopoulos
 
Flat
FlatFlat
Flat
lavishka_anuj
 
Compiler components and their generators traditional parsing algorithms
Compiler components and their generators  traditional parsing algorithmsCompiler components and their generators  traditional parsing algorithms
Compiler components and their generators traditional parsing algorithms
PeshrowKareem1
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdf
Shivang70701
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language Definition
Eelco Visser
 
Parsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Parsers Combinators in Scala, Ilya @lambdamix KliuchnikovParsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Parsers Combinators in Scala, Ilya @lambdamix Kliuchnikov
Vasil Remeniuk
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
eliasabdi2024
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
Syntactic Analysis in Compiler Construction
Syntactic Analysis in Compiler ConstructionSyntactic Analysis in Compiler Construction
Syntactic Analysis in Compiler Construction
voyoc79528
 
A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query Languages
Kim Mens
 
Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language Parser
Yodalee
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...
Vissarion Fisikopoulos
 
Compiler components and their generators traditional parsing algorithms
Compiler components and their generators  traditional parsing algorithmsCompiler components and their generators  traditional parsing algorithms
Compiler components and their generators traditional parsing algorithms
PeshrowKareem1
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdf
Shivang70701
 
Ad

More from Guido Wachsmuth (9)

Language
LanguageLanguage
Language
Guido Wachsmuth
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
Guido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
Guido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
Guido Wachsmuth
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
Guido Wachsmuth
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
Guido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
Guido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
Guido Wachsmuth
 

Recently uploaded (20)

Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 

Compiler Components and their Generators - Traditional Parsing Algorithms

  • 1. Compiler Components & Generators Traditional Parsing Algorithms Guido Wachsmuth Delft Course IN4303 University of Technology Compiler Construction Challenge the future
  • 2. Recap: Lexical Analysis lessons learned What are the formalisms to describe regular languages? • regular grammars • regular expressions • finite state automata Why are these formalisms equivalent? • constructive proofs How can we generate compiler tools from that? • implement DFAs • generate transition tables Traditional Parsing Algorithms 2
  • 3. Overview today’s lecture Lexical Analysis 3
  • 6. Overview today’s lecture the exam assignment 3 efficient parsing algorithms • predictive parsing • LR parsing Lexical Analysis 3
  • 7. Overview today’s lecture the exam assignment 3 efficient parsing algorithms • predictive parsing • LR parsing grammar classes • LL(k) grammars • LR(k) grammars Lexical Analysis 3
  • 8. I the exam Lexical Analysis 4
  • 9. The exam topics • introduction • declarative language definition (theory) • compiling imperative & object-oriented languages • compiler components & generators style • theory in praxis • understanding over facts • open questions • closed book Traditional Parsing Algorithms 5
  • 10. II assignment Lexical Analysis 6
  • 11. MiniJava Compiler schedule Lab 1 explore Java Bytecode & Jasmin first generator version Lab 2 & 3 generator for statements & expressions generator for classes, fields & methods Lab 4 liveness analysis submission due Jan 9, 23:59 Traditional Parsing Algorithms 7
  • 12. MiniJava Compiler code generation class Main { public static void main(String[] args) { System.out.println(42) ; } } Traditional Parsing Algorithms 8
  • 13. Recap: Code Generation to-jbc: Nil() -> [ ACONST_NULL() ] to-jbc: NoVal() -> [ NOP() ] to-jbc: Seq(es) -> <mapconcat(to-jbc)> es to-jbc: Int(i) -> [ LDC(Int(i)) to-jbc: String(s) -> [ LDC(String(s)) to-jbc: Bop(op, e1, e2) -> <concat> [ <to-jbc> e1, <to-jbc> e2, <to-jbc> op ] to-jbc: PLUS() -> [ IADD() ] to-jbc: MINUS() -> [ ISUB() ] to-jbc: MUL() -> [ IMUL() ] to-jbc: DIV() -> [ IDIV() ] to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc> lhs ] to-jbc: Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) => INT() to-jbc: Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) => STRING() lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) => INT() lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) => STRING() Traditional Parsing Algorithms 9
  • 14. MiniJava Compiler liveness analysis .method public static main([Ljava/lang/String;)V .limit locals 3 .limit stack 4 .var 1 is x I from startX to endX .var 2 is y Z from startY to endY ... .end method Traditional Parsing Algorithms 10
  • 15. MiniJava Compiler submission procedure ensure compiler works add 5 positive test cases include your Net ID in project name export project as zip file ensure editor works when imported submit zip file via Blackboard submission due Jan 9, 23:59 delay costs you 25 points plus 1 point per hour or part thereof Traditional Parsing Algorithms 11
  • 16. Assignment: MiniJava Editor grades code generation 60 points classes 10 points fields & methods 15 points statements 15 points expressions 20 points liveness analysis 30+5 points maximum stack size 5 bonus points test cases 10 points Traditional Parsing Algorithms 12
  • 17. III predictive parsing Lexical Analysis 13
  • 18. Recap: A Theory of Language formal languages Traditional Parsing Algorithms 14
  • 19. Recap: A Theory of Language formal languages vocabulary Σ finite, nonempty set of elements (words, letters) alphabet Traditional Parsing Algorithms 14
  • 20. Recap: A Theory of Language formal languages vocabulary Σ finite, nonempty set of elements (words, letters) alphabet string over Σ finite sequence of elements chosen from Σ word, sentence, utterance Traditional Parsing Algorithms 14
  • 21. Recap: A Theory of Language formal languages vocabulary Σ finite, nonempty set of elements (words, letters) alphabet string over Σ finite sequence of elements chosen from Σ word, sentence, utterance formal language λ set of strings over a vocabulary Σ λ ⊆ Σ* Traditional Parsing Algorithms 14
  • 22. Recap: A Theory of Language formal grammars Traditional Parsing Algorithms 15
  • 23. Recap: A Theory of Language formal grammars formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N Traditional Parsing Algorithms 15
  • 24. Recap: A Theory of Language formal grammars formal grammar G = (N, Σ, P, S) nonterminal symbols N nonterminal symbol terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N Traditional Parsing Algorithms 15
  • 25. Recap: A Theory of Language formal grammars formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N context Traditional Parsing Algorithms 15
  • 26. Recap: A Theory of Language formal grammars formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N replacement Traditional Parsing Algorithms 15
  • 27. Recap: A Theory of Language formal grammars formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N grammar classes type-0, unrestricted type-1, context-sensitive: (a A c, a b c) type-2, context-free: P ⊆ N × (N∪Σ)* type-3, regular: (A, x) or (A, xB) Traditional Parsing Algorithms 15
  • 28. Recap: A Theory of Language formal languages Traditional Parsing Algorithms 16
  • 29. Recap: A Theory of Language formal languages formal grammar G = (N, Σ, P, S) Traditional Parsing Algorithms 16
  • 30. Recap: A Theory of Language formal languages formal grammar G = (N, Σ, P, S) derivation relation G ⊆ (N∪Σ)* × (N∪Σ)* w G w’ ∃(p, q)∈P: ∃u,v∈(N∪Σ)*: w=u p v ∧ w’=u q v Traditional Parsing Algorithms 16
  • 31. Recap: A Theory of Language formal languages formal grammar G = (N, Σ, P, S) derivation relation G ⊆ (N∪Σ)* × (N∪Σ)* w G w’ ∃(p, q)∈P: ∃u,v∈(N∪Σ)*: w=u p v ∧ w’=u q v formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S G * w} Traditional Parsing Algorithms 16
  • 32. Recap: A Theory of Language formal languages formal grammar G = (N, Σ, P, S) derivation relation G ⊆ (N∪Σ)* × (N∪Σ)* w G w’ ∃(p, q)∈P: ∃u,v∈(N∪Σ)*: w=u p v ∧ w’=u q v formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S G * w} classes of formal languages Traditional Parsing Algorithms 16
  • 33. Predictive parsing recursive descent Exp → “while” Exp “do” Exp public void parseExp() { consume(WHILE); parseExp(); consume(DO); parseExp(); } Traditional Parsing Algorithms 17
  • 34. Predictive parsing look ahead Exp → “while” Exp “do” Exp Exp → “if” Exp “then” Exp “else” Exp public void parseExp() { switch current() { case WHILE: consume(WHILE); parseExp(); ...; break; case IF : consume(IF); parseExp(); ...; break; default : error(); } } Traditional Parsing Algorithms 18
  • 35. Predictive parsing parse table rows T1 T2 T3 ... • nonterminal symbols N N1 N1 →... N1 →... • symbol to parse N2 N2 →... columns N3 N3 →... N3 →... N4 N4 →... • terminal symbols Σ k N5 N5 →... • look ahead k N6 N6 →... N6 →... entries N7 N7 →... • production rules P N8 N8 →... N8 →... N8 →... • possible conflicts ... Traditional Parsing Algorithms 19
  • 36. Predictive parsing automaton input t1 … tn $ parse table S stack $ Traditional Parsing Algorithms 20
  • 37. Predictive parsing automaton x … $ x … $ Traditional Parsing Algorithms 21
  • 38. Predictive parsing automaton … $ … $ Traditional Parsing Algorithms 21
  • 39. Predictive parsing automaton x … $ x Xi Xi Xi → Y1... Yk … $ Traditional Parsing Algorithms 22
  • 40. Predictive parsing automaton x … $ Y1 x … Yk Xi Xi → Y1... Yk … $ Traditional Parsing Algorithms 22
  • 41. Predictive parsing filling the table entry (X, w)∈P at row X and column T T∈FIRST(w) nullable(w) ∧ T∈FOLLOW(X) Traditional Parsing Algorithms 23
  • 42. Predictive parsing nullable nullable(X) (X, ε) ∈ P nullable(X) (X0, X1 … Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk) nullable(X0) nullable(w) nullable(ε) nullable(X1 … Xk) = nullable(X1) ∧ … ∧ nullable(Xk) Traditional Parsing Algorithms 24
  • 43. Predictive parsing first sets FIRST(X) X∈Σ : FIRST(X) = {X} (X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi) FIRST(X0) ⊇ FIRST(Xi+1) FIRST(w) FIRST(ε) = {} ¬nullable(X) FIRST(Xw) = FIRST(X) nullable(X) FIRST(Xw) = FIRST(X) ∪ FIRST(w) Traditional Parsing Algorithms 25
  • 44. Predictive parsing follow sets FOLLOW(X) (X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk) FOLLOW(Xi) ⊇ FOLLOW(X0) (X0, X1 … Xi … Xk)∈P FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk) Traditional Parsing Algorithms 26
  • 45. Predictive parsing encoding precedence Exp → Num Exp → “(” Exp “)” Exp → Exp “*” Exp Exp → Exp “+” Exp Fact → Num Fact → “(” Exp “)” Term → Term “*” Fact Term → Fact Exp → Exp “+” Term Exp → Term Traditional Parsing Algorithms 27
  • 46. Predictive parsing eliminating left recursion Term → Term “*” Fact Term → Fact Exp → Exp “+” Term Exp → Term Term’ → “*” Fact Term’ Term’ → Term → Fact Term’ Exp’ → “+” Term Exp’ Exp’ → Exp → Term Exp’ Traditional Parsing Algorithms 28
  • 47. Predictive parsing left factoring Exp → “if” Exp “then” Exp “else” Exp Exp → “if” Exp “then” Exp Exp → “if” Exp “then” Exp Else Else → “else” Exp Else → Traditional Parsing Algorithms 29
  • 48. Grammar classes context-free grammars Traditional Parsing Algorithms 30
  • 49. Grammar classes context-free grammars LL(0) Traditional Parsing Algorithms 30
  • 50. Grammar classes context-free grammars LL(1) LL(0) Traditional Parsing Algorithms 30
  • 51. Grammar classes context-free grammars LL(k) LL(1) LL(0) Traditional Parsing Algorithms 30
  • 52. coffee break Traditional Parsing Algorithms 31
  • 53. IV LR parsing Lexical Analysis 32
  • 54. LR parsing idea problems with LL parsing • predicting right rule • left recursion LR parsing • see whole left-hand side of a rule • look ahead • shift or reduce Traditional Parsing Algorithms 33
  • 55. LR parsing example stack input $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 56. LR parsing example $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 57. LR parsing example $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 58. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 59. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 60. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 34
  • 61. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ Traditional Parsing Algorithms 34
  • 62. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 34
  • 63. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 34
  • 64. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 34
  • 65. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 34
  • 66. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 34
  • 67. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 34
  • 68. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ Traditional Parsing Algorithms 34
  • 69. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ Traditional Parsing Algorithms 34
  • 70. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ Traditional Parsing Algorithms 34
  • 71. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ Traditional Parsing Algorithms 34
  • 72. LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ $ S Traditional Parsing Algorithms 34
  • 73. LR parsing parse table rows T1 ... N1 ... • states of a DFA 1 s3 2 g5 columns 3 r1 • topmost stack symbol 4 r2 • Σ, N 5 entries 6 g1 • reduce, goto state 7 s1 • shift, goto state 8 • goto state ... Traditional Parsing Algorithms 35
  • 74. Grammar classes context-free grammars LL(k) LL(1) LL(0) Traditional Parsing Algorithms 36
  • 75. Grammar classes context-free grammars LL(k) LL(1) LL(0) LR(0) Traditional Parsing Algorithms 36
  • 76. Grammar classes context-free grammars LL(k) LL(1) LR(1) LL(0) LR(0) Traditional Parsing Algorithms 36
  • 77. Grammar classes context-free grammars LL(k) LR(k) LL(1) LR(1) LL(0) LR(0) Traditional Parsing Algorithms 36
  • 78. Grammar classes context-free grammars LL(k) LR(k) LL(1) LR(1) SLR LL(0) LR(0) Traditional Parsing Algorithms 36
  • 79. Grammar classes context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0) Traditional Parsing Algorithms 36
  • 80. V summary Lexical Analysis 37
  • 81. Summary lessons learned Traditional Parsing Algorithms 38
  • 82. Summary lessons learned How can we parse context-free languages effectively? • predictive parsing algorithms • LR parsing algorithms Traditional Parsing Algorithms 38
  • 83. Summary lessons learned How can we parse context-free languages effectively? • predictive parsing algorithms • LR parsing algorithms Which grammar classes are supported by these algorithms? • LL(k) grammars, LL(k) languages • LR(k) grammars, LR(k) languages Traditional Parsing Algorithms 38
  • 84. Summary lessons learned How can we parse context-free languages effectively? • predictive parsing algorithms • LR parsing algorithms Which grammar classes are supported by these algorithms? • LL(k) grammars, LL(k) languages • LR(k) grammars, LR(k) languages How can we generate compiler tools from that? • implement automata • generate parse tables Traditional Parsing Algorithms 38
  • 85. Summary lessons learned How can we parse context-free languages effectively? • predictive parsing algorithms • LR parsing algorithms Which grammar classes are supported by these algorithms? • LL(k) grammars, LL(k) languages • LR(k) grammars, LR(k) languages How can we generate compiler tools from that? • implement automata • generate parse tables Traditional Parsing Algorithms 38
  • 86. Literature learn more Traditional Parsing Algorithms 39
  • 87. Literature learn more formal languages Noam Chomsky: Three models for the description of language. 1956 J. E. Hopcroft, R. Motwani, J. D. Ullman: Introduction to Automata Theory, Languages, and Computation. 2006 Traditional Parsing Algorithms 39
  • 88. Literature learn more formal languages Noam Chomsky: Three models for the description of language. 1956 J. E. Hopcroft, R. Motwani, J. D. Ullman: Introduction to Automata Theory, Languages, and Computation. 2006 syntactical analysis Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers: Principles, Techniques, and Tools, 2nd edition. 2006 Traditional Parsing Algorithms 39
  • 89. Outlook coming next lectures • Lecture 13: SDF inside • Lecture 14: Static analysis • Lecture 15: Beyond grammarware Question & Answer Jan 5 • 10 questions, submit & vote Lab Dec 3 • explore Java Bytecode • explore Jasmin • start generator Traditional Parsing Algorithms 40
  • 90. questions Lexical Analysis 41
  • 91. credits Lexical Analysis 42
  • 92. Pictures copyrights Slide 1: Book Scanner by Ben Woosley, some rights reserved Slide 5: Report card by Carosaurus, some rights reserved Slides 7, 12: Students, TU Delft, Media Solutions, all rights reserved Slides 8, 10: Italian Java book cover by unknown artist, some rights reserved Slide 11: Envelopes by benchilada, some rights reserved Slides 14-16: Noam Chomsky by Fellowsisters, some rights reserved Slide 17, 18, 27-29: Tiger by Bernard Landgraf, some rights reserved Slide 31: West Cornwall Pasty Co. by Dominica Williamson, some rights reserved Slide 40: Ostsee by Mario Thiel, some rights reserved Slide 41: Questions by Oberazzi, some rights reserved Slide 42: Too Much Credit by Andres Rueda, some rights reserved Traditional Parsing Algorithms 43

Editor's Notes