SlideShare una empresa de Scribd logo
1 de 63
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingGuido Wachsmuth
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolutionEelco 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 RegainedGuido Wachsmuth
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionEelco Visser
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesEelco Visser
 
Programming languages
Programming languagesProgramming languages
Programming languagesEelco Visser
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name ResolutionEelco Visser
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type CheckingEelco Visser
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil 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 RewritingEelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationEelco Visser
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)Eelco Visser
 

La actualidad más candente (20)

Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
LL Parsing
LL ParsingLL Parsing
LL Parsing
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
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
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Type analysis
Type analysisType analysis
Type analysis
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Ch04
Ch04Ch04
Ch04
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
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
 
Ch03
Ch03Ch03
Ch03
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Ch4a
Ch4aCh4a
Ch4a
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
 

Destacado

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 RuntimeOmar 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 ParsingGuido 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 coresssuhas
 
Evolution of Intel Processors
Evolution of Intel ProcessorsEvolution of Intel Processors
Evolution of Intel ProcessorsShad Ahmad Zaidi
 
Intel Processors
Intel ProcessorsIntel Processors
Intel Processorshome
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya 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 SortingStephen 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
 

Destacado (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
 
80386 Architecture
80386 Architecture80386 Architecture
80386 Architecture
 
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
 
Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project
 
Intel processors
Intel processorsIntel processors
Intel processors
 
Ch5b
Ch5bCh5b
Ch5b
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
01 intel processor architecture core
01 intel processor architecture core01 intel processor architecture core
01 intel processor architecture core
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Evolution of Intel Processors
Evolution of Intel ProcessorsEvolution of Intel Processors
Evolution of Intel Processors
 
Intel Processors
Intel ProcessorsIntel Processors
Intel Processors
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
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
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Ch5a
Ch5aCh5a
Ch5a
 
Code generation
Code generationCode generation
Code generation
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 

Similar a Compiler Components and their Generators - Traditional Parsing Algorithms

Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language DefinitionEelco 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 KliuchnikovVasil Remeniuk
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingFilip De Sutter
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
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 ParserYodalee
 
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 algorithmsPeshrowKareem1
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdfShivang70701
 
Stochastic Definite Clause Grammars
Stochastic Definite Clause GrammarsStochastic Definite Clause Grammars
Stochastic Definite Clause GrammarsChristian Have
 
Introduction to optimizxation
Introduction to optimizxationIntroduction to optimizxation
Introduction to optimizxationhelalmohammad2
 
DeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingDeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingThomas Winters
 

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

Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language Definition
 
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
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Ch2
Ch2Ch2
Ch2
 
Prolog & lisp
Prolog & lispProlog & lisp
Prolog & lisp
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
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
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction
IntroductionIntroduction
Introduction
 
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...
 
Flat
FlatFlat
Flat
 
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
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdf
 
Stochastic Definite Clause Grammars
Stochastic Definite Clause GrammarsStochastic Definite Clause Grammars
Stochastic Definite Clause Grammars
 
Introduction to optimizxation
Introduction to optimizxationIntroduction to optimizxation
Introduction to optimizxation
 
DeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingDeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic Programming
 

Más de Guido Wachsmuth

Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type SystemsGuido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingGuido 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 CollectionGuido 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 AllocationGuido 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 AnalysisGuido 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 RecordsGuido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Guido Wachsmuth
 

Más de Guido Wachsmuth (9)

Language
LanguageLanguage
Language
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
 
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
 
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
 
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
 
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
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
 

Último

DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfTechSoup
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 

Último (20)

DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 

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

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. linguistic theory\n\nNoam Chomsky\n
  18. linguistic theory\n\nNoam Chomsky\n
  19. linguistic theory\n\nNoam Chomsky\n
  20. restrictions on production rules =&gt; grammar classes\n
  21. restrictions on production rules =&gt; grammar classes\n
  22. restrictions on production rules =&gt; grammar classes\n
  23. restrictions on production rules =&gt; grammar classes\n
  24. restrictions on production rules =&gt; grammar classes\n
  25. restrictions on production rules =&gt; grammar classes\n
  26. restrictions on production rules =&gt; grammar classes\n
  27. restrictions on production rules =&gt; grammar classes\n
  28. \n
  29. \n
  30. \n
  31. \n
  32. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  33. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  34. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. restrictions on production rules =&gt; grammar classes\n
  43. restrictions on production rules =&gt; grammar classes\n
  44. restrictions on production rules =&gt; grammar classes\n
  45. restrictions on production rules =&gt; grammar classes\n
  46. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  47. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  48. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n