Previously on GLT. Basic technologies. Lexical analysis. Basic technologies ASF+SDF. Concepts of programming languages

Similar documents
Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Java II Finite Automata I

Lexical Analysis Part III

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

Lexical Analysis Finite Automate

Non-deterministic Finite Automata

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Fundamentals of Computer Science

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CMSC 330: Organization of Programming Languages

CHAPTER 1 Regular Languages. Contents

FABER Formal Languages, Automata and Models of Computation

Non-deterministic Finite Automata

Harvard University Computer Science 121 Midterm October 23, 2012

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

NFAs continued, Closure Properties of Regular Languages

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

Deterministic Finite Automata

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Minimal DFA. minimal DFA for L starting from any other

1.3 Regular Expressions

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Convert the NFA into DFA

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

1.4 Nonregular Languages

Homework 3 Solutions

Lecture 6 Regular Grammars

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

Lecture 08: Feb. 08, 2019

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

CS375: Logic and Theory of Computing

First Midterm Examination

Formal Language and Automata Theory (CS21004)

ɛ-closure, Kleene s Theorem,

First Midterm Examination

Worked out examples Finite Automata

Theory of Computation Regular Languages

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

CSC 311 Theory of Computation

Finite Automata-cont d

2. Lexical Analysis. Oscar Nierstrasz

Finite-State Automata: Recap

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

NFAs continued, Closure Properties of Regular Languages

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Let's start with an example:

CS 314 Principles of Programming Languages

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

Regular expressions, Finite Automata, transition graphs are all the same!!

CS 275 Automata and Formal Language Theory

Context-Free Grammars and Languages

CISC 4090 Theory of Computation

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

3 Regular expressions

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Nondeterminism and Nodeterministic Automata

Designing finite automata II

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

CS375: Logic and Theory of Computing

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Some Theory of Computation Exercises Week 1

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

Review for the Midterm

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Chapter 2 Finite Automata

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

1 From NFA to regular expression

Overview. Regular Languages. Finite Automata. A finite automaton. Startstate : q Acceptstate : q. Transitions

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

1 Nondeterministic Finite Automata

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Today s Topics Automata and Languages

Automata and Languages

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

GNFA GNFA GNFA GNFA GNFA

Formal languages, automata, and theory of computation

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Lecture 09: Myhill-Nerode Theorem

Closure Properties of Regular Languages

CS:4330 Theory of Computation Spring Regular Languages. Equivalences between Finite automata and REs. Haniel Barbosa

Closure Properties of Regular Languages

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Introduction to ω-autamata

Chapter 1, Part 1. Regular Languages. CSC527, Chapter 1, Part 1 c 2012 Mitsunori Ogihara 1

CS 573 Automata Theory and Formal Languages

Transcription:

Prevously on GLT Generc Lnguge Technology: Bsc technologes Pro.dr. Mrk vn den Brnd ASF+SDF syntx descrptons semntc descrptons: type checkng nlyss trnsormtons t so to s Concepts o progrmmng lnguges syntctc ssues semntc ssues Met modelng nd model trnsormtons / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 1 Bsc technologes Syntctcl nlyss Prser genertors Rewrte engnes Tsks nd orgnzton o lexcl nlyzer Speccton o lexcl tokens v regulr expressons Implementton o regulr expressons (non-)determnstc nte utomt trnslton o regulr expresson to utomton / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 2 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 3

progrm text (chrcters) Syntctcl nlyss prse tree Tsks o the lexcl nlyzer: redng the nput nd producton o tokens elmnton o lyout nd comments keepng trck o poston normton Lexcl nlyzer get next token token Prser Symol tle / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 4 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 5 A regulr expresson (r.e.) r over n lphet Σ corresponds to the lnguge L(r) 1. s r.e. nd corresponds to {} 2. Σ s r.e. nd corresponds to {} 3. Suppose r nd s re r.e. s correspondng to the lnguges L(r) nd L(s). lterntve (r) (s) s r.e. L(r) L(s). conctenton (r) (s) s r.e. L(r) L(s) c. Kleene closure (r)* s r.e. (L(r))* d. rckets (r) s r.e. L(r) Opertors re let-ssoctve nd prortes re * > conctenton > A regulr denton over lphet Σ hs the orm: re 1 -> d 1 re 2 -> d 2 re n -> d n where d re derent nmes nd ech re s r.e. over lphet Σ {d 1, d 2,, d -1 } Thus, n re occur only nmes whch re lredy dened / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 6 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 7

A regulr expresson cn e compled nto nte utomton (FA = nte utomton) whch s recognzer or the correspondng regulr lnguge A nte utomton s non-determnstc severl derent trnstons re possle or one nput symol n stte (NFA) There re two possle wys o trnsormng r.e. nto determnstc nte utomton: 1. r.e. NFA DFA 2. re r.e. DFA The generted DFA hs to e optmzed Otherwse the nte utomton s determnstc (DFA) / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 8 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 9 Non-determnstc nte utomton conssts o: 1. Set o sttes S 2. Input lphet Σ 3. Trnston uncton whch tkes stte/symol pr nd yelds set o new sttes 4. The strt stte s 0 S 5. A set F o cceptng sttes Exmple: S = {0,1,2,3} Σ = {, } S 0 = 0 F = {3} Ths utomton ccepts: ( )* Trnston uncton: Stte 0 {0,1} {0} 1 {2} 2 {3} / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 10 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 11

Trnstons my lso e lelled wth 1 2 0 3 4 NFA ccepts strng x There exsts pth n the trnston dgrm rom strt to nl stte such tht the conctenton o the lels on the pth equl x For exmple s ccepted y the prevous NFA Pth: 0 1 2 2 2 Lels: = The regulr expresson s: * * / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 12 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 13 Regulr expresson NFA Input: regulr expresson r over lphet Σ Output: NFA N whch ccepts L(r) 1. r = 2. r = where Σ 1. Suppose N(s) nd N(t) re NFAs or the r.e. s nd t. r = s t. r=st N(s) N(s) N(t) N(t) Ths operton s only possle the nl stte o N(s) hs no outgong trnstons nd the strt stte o N(t) hs no ncomng trnstons / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 14 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 15

c. r = s* N(s) d. r =(s) thenn(r) =N(s) Converson NFA DFA Regulr expresson cn e trnsormed nto NFAs DFAs cn e smulted/mplemented ecently Trnsormton o NFA nto DFA: construct DFA where ech stte represents suset o the sttes o the sttes o the NFA ter redng the nput 1 2 n the NFA s n set o sttes T, whch corresponds to one stte o the DFA / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 16 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 17 Auxlry unctons: -closure(s) yelds set o NFA sttes rechle rom stte s n NFA v -trnstons only -closure(t) yelds set o NFA sttes rechle rom stte s n T v -trnstons only move(t, ) yelds set o NFA sttes rechle rom stte s n T v nput Intlly, -closure(s 0 ) s the only stte n Dsttes nd unmrked whle there s n unmrked stte T n Dsttes do mrk T; or ech nput symol do U := -closure(move(t, )) U s not n Dsttes then dd U s n unmrked stte to Dsttes; end Dtrns[T,] := U; end end / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 18 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 19

Suset constructon (Rn & Scott) NFA = (Q, V, γ, q 0, F) NFA N or ( )* Equvlent DFA = (P(Q), V, δ, {q 0 }, F ) ) δ P(Q) V P(Q) δ(qq, ) ) = ( q:q qq : γ(q, ( )) F = {qq P(Q) qq F } δ({q 0 }, w) = set o ll sttes n whch the orgnl NFA cn e ter processng strng w 2 3 0 1 6 7 8 9 10 4 5 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 20 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 21 A = {0, 1, 4, 5, 7} (= -closure(0)) B = {1, 2, 3, 4, 6, 7, 8} (= move({0, 1, 4, 5, 7}, )) C = {1, 2, 4, 5, 6, 7} (= move({0, 1, 4, 5, 7}, )) D = {1,2,4,5,6,7,9}(= move({1, 2, 3, 4, 6, 7, 8}, )) E = {1, 2, 4, 5, 6, 7, 10} (= move({1, 2, 3, 4, 6, 7, 9}, )) Resultng DFA A B D E Trnston tle stte A B C B B D C B C D B E E B C C / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 22 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 23

LEX s scnner genertor whch trnsorms regulr expressons nto nte utomton: r.e. NFA re 0 {cton 0 } re 1 {cton 1 } 0 0 re k {cton k } strt F = { 0,, k } NFA DFA cceptng sttes hve the orm {,,,,, c, } wth correspondng cton: cton mn(,,c) k k Resoluton o mgutes Longest mtch s preerred I two lterntves recognze the sme sequence o chrcters, the lterntve occurrng rst n the speccton s chosen BEGIN [sym := egnsym] IF [sym := sym] letter.(letter dgt)* [sym := dsym] dgt.(dgt)* [sym := ntrepsym] := [sym := ecomessym] / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 24 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 25 Syntctcl nlyss Syntctcl nlyss Context-ree grmmrs Dervtons Prse Trees Let-recursve grmmrs Top-down prsng non-recursve predctve prsers constructon o prse tles Bottom-up prsng sht/reduce prsers LR prsers GLR prsers SGLR prsers A context-ree grmmr s 4-tuple G = (N, Σ, P, S) 1. N s set o non termnls 2. Σ s set o termnls (dsjont rom N) 3. P s suset o (N Σ)* N An element (α, A) P s clled producton A ::= α or α A 4. S N s the strt symol The sets N, Σ, P re nte / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 26 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 27

Syntctcl nlyss Syntctcl nlyss A context-ree grmmr cn e consder s smple rewrte system: A A P (,, (N Σ)*, A N) Exmple N = {E}, Σ = {+,*,(,),-, }, S = E, P = { E + E E E * E E ( E ) E - E E E} } Dervton: E -E -(E) -(E+E) -(+E) -(+) The lnguge L(G) generted y the context-ree grmmr G = (N, Σ, P, S) s: L(G) = {w Σ* S + w} A sentence w L(G) contns only termnls A sententl orm s strng o termnls nd nontermnls whch cn e derved rom S: S * wth (N Σ) * A sentence n L(G) s sententl orm n whch no non-termnls occur / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 28 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 29 Syntctcl nlyss Syntctcl nlyss Let/rght dervtons There re choces to e mde or ech dervton step: whch non-termnl must e replced? whch lterntve o the selected non-termnl must e ppled? Alwys selectng the letmost non-termnl n the sententl orm gves letmost dervton: lm There exsts lso rghtmost dervton: rm Consder the context-ree grmmr or expressons: Letmost dervton or -(+) E -E -(E) -(E+E) -(+E) -(+) Rghtmost t dervton or -(+) E -E -(E) -(E+E) -(E+) -(+) A prse tree or context-ree grmmr s G = (N, Σ, P, S) tree: 1. The root s leled wth S (the strt non-termnl) 2. Ech le s leled wth termnl ( Σ)or 3. All other nodes re leled wth non-termnl I A s the lel o node nd X 1,,X n re the lels o the chldren (rom let to rght) then X 1,,X n A must e producton rule n G (wth X s ether termnl or non-termnl) Specl cse: A wth lel A whch hs exctly one chld wth lel / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 30 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 31

Syntctcl nlyss Syntctcl nlyss Exmple: E -E -(E) -(E+E) -(+E) -(+) E E E E E E E - E - E - E - E - E - E ( E ) ( E ) ( E ) ( E ) ( E ) E + E E + E E + E The prse tree strcts rom the dervton order Acceptor nd prser For ech grmmr G there exsts decson procedure (cceptor) AG or L(G): AG: STRING {true, lse} such tht AG(w) =true w L(G) A prser s n cceptor whch constructs prse tree s well. A top-down prser constructs the tree strtng rom the root A ottom-up prser constructs the tree strtng rom the les / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 32 / Fcultet Wskunde en Inormtc 9-12-2009 PAGE 33