CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

Size: px
Start display at page:

Download "CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)"

Transcription

1 C 236 Lnguge nd Computtion Course Notes Prt I: Grmmrs for Defining yntx (II) Chpter I.2: yntx nd Grmmrs (10, 12.1) Anton etzer (Bsed on ook drft y J. V. Tucker nd K. tephenson) Dept. of Computer cience, wnse University csetzer/lectures/ lngugecomputtion/10/index.html Decemer 8, 2010 I.2.1. Forml Lnguges (10.1) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) C 236 ect. I.2.1 1/ 128 I.2.1. Forml Lnguges (10.1) I.2.1. Forml Lnguges (10.1) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) Alphet C 236 ect. I.2.1 2/ 128 I.2.1. Forml Lnguges (10.1) An lphet is finite non-empty set T. We shll consider the elements of T to e symols. Exmples The lphet of deciml digits is T Digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} The lower-cse lphet of the English lnguge is T English Lowercse Alphet = {,, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z} C 236 ect. I / 128 C 236 ect. I / 128

2 Alphet I.2.1. Forml Lnguges (10.1) tring I.2.1. Forml Lnguges (10.1) The lower-cse Welsh lphet is T Welsh Lowercse Alphet = {,, c, ch, d, dd, e, f, ff, g, ng, h, i, l, ll, m, n, o, p, ph, r, rh, s, t, th, u, w, y} (This lphet hs een checked crefully). Notice: j, k, q, v, x, z re not elements of this lphet. Therefore Txi written s Tcsi in Welsh. The Unicode lphet hs over 100,000 chrcters. The Ascii lphet hs 128 chrcters, of which 33 re non-printing control chrcters. A string or word over n lphet T is finite sequence elements of T. The set of ll strings orr words over the lphet T is T := {t 1 t 2 t n n 0, t 1, t 2,..., t n T } Note tht one exmple is the empty string, which is represented y ɛ. Mny lnguges, e.g. Hskell, identify trings with lists of chrcters. C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) tring in Hskell C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Exmples of trings :: tring = "Hello" :: tring = [ H, e, l, l, o ] csetzer@cspcnton:~> ghci... Prelude> :lod testtring.hs... *Min> "Hello" *Min> "Hello" 1984 nd 2000 re strings over the lphet T Digit of deciml digits. forwrds nd sdrwkc re words over the lower-cse English lphet T English Lowercse Alphet. C 236 ect. I / 128 C 236 ect. I / 128

3 Length of tring The length of string is given y the function _ : T N w is the numer of symols from the lphet it contins. If we identify strings with List(T ), it is the length of the corresponding list. Exmples 2000 = 4, forwrds = 8, ɛ = 0 T + is the set of non-empty strings: T + = T \ {ɛ} = {t 1 t 2 t n n 1, t 1,..., t n T } Conctention I.2.1. Forml Lnguges (10.1) 1. The conctention of two strings u = u 1 u m nd v = v 1 v n is the string uv = u 1,..., u m, v 1,..., v n 2. The conctention function is the function. : T T T For instnce, if u = 1984 nd v = 2000 then uv = We hve uv = u + v Conctention I.2.1. Forml Lnguges (10.1) C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Forml Lnguge We define w n := ww }{{ w}. n times For instnce forwrds 3 = forwrdsforwrdsforwrds A forml lnguge L over n lphet T is suset L of the set of strings over T, i.e. L T We usully sy lnguge for forml lnguge. C 236 ect. I / 128 C 236 ect. I / 128

4 Exmple 1 I.2.1. Forml Lnguges (10.1) Exmple 1 (Cont) I.2.1. Forml Lnguges (10.1) Let We cn enumerte T, T, T, = {, } in the following wy = {ɛ,,,,,,,,,,,,,,,...} o we write first strings of length 0, then of length 1, then of length 2 etc. There is only one string of length 0, nmely ɛ. The strings of length n + 1 re otined y dding in front of ech string of length n nd then do the sme with. Exercise Write progrm which enumertes the strings of length n for given n. Here re few exmples of lnguges over T, : 1. L =. 2. L = {, }. 3. L = {ɛ}. 4. L = {}. 5. L = {, }. 6. L = { n n is even}. 7. L = { n n n 0}. 8. L = { n n+1 n 0}. 9. L = {() n n 0}. C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Exmple 2: URLs C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Exmple 3: Infix Numers Let T = {,,..., z, A, B,..., Z, 0, 1,..., 9,,, /,.m, :} Let L = {w T w is n http ddress } L contins simple ddresses such s http : // or http : // ut complex exmples like re not in L, since for exmple? nd + re not in T. Let T Infix Arithmetic = {0, 1, +,., )} Expressions of type nturl numers re strings such s (0 + 1) + 1, (1 + 0).0, etc. C 236 ect. I / 128 C 236 ect. I / 128

5 I.2.1. Forml Lnguges (10.1) Exmple 4: Prefix Numers Tokens I.2.1. Forml Lnguges (10.1) Let T Prefix Arithmetic = {zero, succ, dd, mult,,, (, )} Expressions of type nturl numers re expressions such s succ(succ(zero)), dd(zero, succ(succ(zero))) Let L Prefix Arithmetic = {w TPrefix Arithmetic w is n rithmetic expression} Most prser genertors (or compiler-compiler) hve two phses: In phse one the incoming strem of chrcters is grouped into simple tokens. This cn e words like zero, succ ove. Or even longer words considered s one entity, such s <Identifier>. In phse two the text is prsed using grmmr which refers to the tokens generted in the first phse. Token genertion will mke use of regulr expressions, prsing in phse two will use restricted context free grmmrs. Notions regulr expression nd context free grmmr will e introduced lter. Tokens will ply the role of the input lphet for the context free grmmr. Token genertion is the process of generting text in the input lphet of the context free grmmr (tokens) from the input lphet of the lnguge (e.g. Ascii or Unicode chrcters). C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Desigining yntx Using Forml Lnguges C 236 ect. I / 128 I.2.1. Forml Lnguges (10.1) Recognition Prolem The syntx of lnguge is defined in two steps: 1. Choose n lphet T. 2. Define the lnguge L T. Let L T e forml lnguge over T. The recognition prolem for T is: Given ny w T decide whether or not w L. C 236 ect. I / 128 C 236 ect. I / 128

6 I.2.2. Grmmrs nd Derivtions (10.2) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I.2.1. Forml Lnguges (10.1) Exmple: Prse Tree for n English entence I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) NounPhrse entence VerPhrse I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) I g Ver o NounPhrse h o m e A simplified prse tree for the English entence I go home. Terminls C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Non-terminls entence entence NounPhrse VerPhrse NounPhrse VerPhrse I Ver NounPhrse I Ver NounPhrse g o h o m e g o h o m e I,, g, o etc. re the terminls of the grmmr. They re elements of the lphet of the lnguge. entence, NounPhrse, VerPhrse etc. re non-terminls. They re intermedite steps in the grmmr. Elements derivle from them form sulnguge, often grmmticl entity. C 236 ect. I / 128 C 236 ect. I c/ 128

7 trt ymol I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Productions I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) entence entence NounPhrse VerPhrse NounPhrse VerPhrse I Ver NounPhrse I Ver NounPhrse g o h o m e g o h o m e entence is the strt symol. The lnguge derived is the set of words formed from non-terminls derivle from them. We need s well rules clled which re used to crry out derivtions. E.g.: entence Nounphrse Verphrse Productions C 236 ect. I d/ 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) C 236 ect. I e/ 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Grmmrs ( ) NounPhrse I entence g Ver NounPhrse I VerPhrse Ver NounPhrse o VerPhrse NounPhrse h o m e A Grmmr G = (T, N,, P) consists of 1. finite set T clled the lphet or set of terminl symols, 2. finite set N of non-terminl symols or vrile symols such tht T N =, 3. specil non-terminl symol N clled the strt symol, 4. finite set P of sustitution or rewrite rules, clled, ech of which hs the form u v where 4.1 The left hnd string u (T N) + (esp. u is non-empty), 4.2 The right hnd string v (T N). C 236 ect. I f/ 128 C 236 ect. I / 128

8 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Remrk Presenttion of Grmmrs We present grmmr s 4-tuple G = (T, N,, P) nd lso use displyed version: Note tht oth left nd right hnd strings cn contin oth terminls nd non-terminls. Terminls is nother word for elements of the lphet of grmmr. grmmr terminls nonterminls G T N strt symol P C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Grmmr for English entence C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) Derivtions (Informl) grmmr terminls nonterminls strt symol impleenglishgrmmrexmple,,c,...,z,a,b,...,z, entence, Nounphrse, Verphrse,Ver entence A grmmr G defines forml lnguge L(G) T. The elements of L(G) re the set of strings we cn otin s follows: trt with strt symol. elect production such tht the left hnd string is sustring of wht you derived so fr. Replce this sustring y the right hnd string of the production. entence Nounphrse Verphrse Verphrse Ver Nounphrse Ver g o Nounphrse I Nounphrse h o m e Once you hve otined string consisting of non-terminls, possily stop. We will first give some exmples nd then forml definition of L(G). C 236 ect. I / 128 C 236 ect. I / 128

9 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Exmple 0: English Grmmr Exmple 1 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Consider the grmmr G [01] 1 = ({0, 1}, {},, { 1, 0, 1}) The following derives the sentence I go home : entence NounPhrse VerPhrse I VerPhrse I Ver NounPhrse I g o NounPhrse I g o h o m e displyed s follows grmmr G [01] 1 terminls 0, 1 nonterminls strt symol 1, 0, 1 C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Exmple 1 (Genertion of trings) We ssign numers to the production rules: Rule 1 1 Rule 2 0 Rule 3 1 A derivtion of 1001 L(G [01] 1 ) is s follows: 1 Rule 3 10 Rule Rule Rule 1 A derivtion of 011 L(G [01] 1 ) is s follows: 0 Rule 2 01 Rule Rule 1 C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Derivtions of trings in G {01} 1 The following shows ll strings derivle in this grmmr. Note tht this is not derivtion tree, its the tree of ll possile derivtions in this lnguge C 236 ect. I / 128 C 236 ect. I / 128

10 L(G {01} 1 ) I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Exmple 2 (Grmmrs) Consider the grmmr We cn see tht the elements of L(G {01} 1 ) re the strings in {0, 1} which end with 1: Remrk L(G {01} 1 ) = {w1 w {0, 1} } G {01} 1 is n exmple of regulr grmmr (this notion will e introduced lter). G n n = ({, }, {},, {, ) displyed s follows grmmr terminls nonterminls strt symol G n n,, C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Derivtions of strings in G n n L(G n n ) C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) We cn see tht the elements of L(G n n ) re the strings of the form n n : L(G n n ) = { n n n 1} Remrk is n exmple of context-free grmmr (this notion will e introduced lter). G n n Note gin tht this is not derivtion tree, ut tree determining ll posssile derivle strings in this lnguge. C 236 ect. I / 128 C 236 ect. I / 128

11 Exmple 3 (Grmmrs) Consider the grmmr G n n c n = ({,, c}, {, B, C},, { BC, BC, CB BC, B, B, C c, cc cc}) displyed s follows grmmr terminls nonterminls strt symol G n n c n,, c, B, C BC, BC, CB BC, B, B, C c, cc cc. I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) Derivtion of ccc Exmple Derivtion I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) A derivtion of cc in this grmmr is s follows: BC BCBC BBCC BCC CC cc cc C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) No Prse Trees for Context ensitive Grmmrs A derivtion of ccc will now e shown on the lck/whiteord. Note tht for context sensitive grmmrs nd unrestricted grmmrs we don t get prse trees. They exist in generl only for context free nd regulr grmmrs. C 236 ect. I / 128 C 236 ect. I / 128

12 L(G n n c n ) I.2.2. Grmmrs nd Derivtions (10.2) I Exmples of Grmmrs nd trings ( ) I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) Derivtions ( ) We cn see tht the elements of L(G n n c n ) re the strings of the form n n c n : L(G n n c n ) = { n n c n n 1} Remrk is often in ooks nd wepges presented s n exmple of context-sensitive grmmr (this notion will e introduced lter). G n n c n It is not, ut cn e trnsformed into context sensitve one, see ect. I.2.3. Exmple Let G = (T, N,, P) e grmmr, w (T N) + e non-empty word, w (T N) e possily empty word. We sy tht w is derived from w w G w in one step, with nottion iff there exists production u v P such tht u occurs in w, w is the result of replcing one occurrence of u in w y v. C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) (Cont.) C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) (Cont.) (Cont) 2. We sy s well w is immeditely generted from w or w is one-step generted from w, or for w G w. w w o w hs the form sut for some s, t (T N), nd w = svt where u v is production. Informlly w is the result of replcing in w sustring, which is equl to the right hnd string of production y the left hnd string of tht production. 3. Let G = (T, N,, P) e grmmr, w, w (T N) e possily empty words. We sy tht w derived from w, with nottion w G w iff is either w = w or there exists sequence of words w0,..., w n (T N) s.t. w 0 = w, w n = w for 1 i n 1 we hve w i G w i+1. C 236 ect. I / 128 C 236 ect. I / 128

13 I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) (Cont.) Exmple I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) In exmple 3 ove we hd the following derivtion: (Cont) 4. We sy s well w is generted from w or for w G w. w w BC BCBC BBCC BCC CC cc cc Therefore we hve for instnce cc BCC BCBC BCC BCBC BCBC Remrk C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Derivtions ( ) C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Lnguge Genertion ( ) Lnguge Genertion ( ) Remrk The ove definitions introduced reltions G (T N) + (T N) G (T N) (T N) G is the reflexive nd trnsitive closure of G Let G = (T, N,, P) e grmmr. The lnguge generted y the grmmr G, denoted y L(G) T is defined s the set of terminl strings generted from the strt symol, i.e. Remrk L(G) := {w T G w} If lnguge is generted y grmmr, then there re infinitely mny grmmrs which generte the sme lnguge. C 236 ect. I / 128 C 236 ect. I / 128

14 I.2.2. Grmmrs nd Derivtions (10.2) I Lnguge Genertion ( ) Equivlence of Grmmrs Exmple I.2.2. Grmmrs nd Derivtions (10.2) I Lnguge Genertion ( ) We sy tht two grmmrs G 1 nd G 2 re equivlent iff L(G 1 ) = L(G 2 ) We show tht L(G n n ) = { n n n 1} One cn esily show tht t n 0(t = n n t = n+1 n+1 ) follows y induction on length of the derivtion t. : show first the ssertion for t = n n y induction on n. Then the ssertion for t = n+1 n+1 follows s well. C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Designing yntx using Grmmrs ( ) Designing yntx using Grmmrs ( ) C 236 ect. I / 128 I.2.2. Grmmrs nd Derivtions (10.2) I Designing yntx using Grmmrs ( ) Designing yntx using Grmmrs For progrmming lnguges L it is usully difficult to find grmmr G s.t. L = L(G). One prolem is the fct tht vriles usully need to e defined efore eing used. We will lter see tht lnguges with such property cn usully not e defined y context-free grmmr. Without ny restrictions on the grmmr it is possile, ut lnguges with unrestricted grmmrs re difficult to prse. Insted one defines first grmmr which reflects the sic structure of the lnguge. Then one selects those strings which fulfil the dditionl criteri. In order to define lnguge L one usully proceeds s follows: tep 1 Choose n lphet s.t. L T. tep 2 Choose simple grmmr G with lphet T s.t. L L(G) T The grmmr should give good explntion of how the lnguge is formed. tep 3 Define n lgorithm which determines for t L(G) whether t L or not. C 236 ect. I / 128 C 236 ect. I / 128

15 I.2.3. The Chomsky Hierrchy (12.1) I.2.3. The Chomsky Hierrchy (12.1) I.2.1. Forml Lnguges (10.1) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Unrestricted Grmmrs Chomsky Hierrchy The Chomsky hierrchy is the clssifiction of grmmrs y mens of 4 properties of its production rules: Unrestricted grmmrs. The limit of grmmrs. Context-sensitive grmmrs. It s usully n ccident if grmmr of lnguge is context-sensitive. C nd C++ hve some context-sensitive spects (delt with y selecting correct strings fter the prsing). Context-free grmmrs. Esy to understnd nd supported y prse genertors. In lnguge design one ims t lnguges hving n underlying context-free grmmr. Regulr grmmrs. imple to prse. Used for dividing the input strem of chrcters into tokens. C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Context-ensitive Grmmrs Let in the following four definitions G = (T, N,, P) e grmmr. Any grmmr G is of Type 0 or unrestricted, so ny production u v for u (T N) +, v (T N) re llowed. Any grmmr G is of Type 1 or context-sensitive, if ll its hve the form uav uwv where A N is nonterminl, which rewrites to non-empty string w (T N) +, ut only where A is in the context of strings u, v (T N). Furthermore production A ɛ is llowed, ut only if A does not occur in the right hnd side of ny production. C 236 ect. I / 128 C 236 ect. I / 128

16 I.2.3. The Chomsky Hierrchy (12.1) Context-Free Grmmrs Regulr Grmmrs I.2.3. The Chomsky Hierrchy (12.1) 1. A grmmr G is left-liner, iff ll its hve the form Any grmmr G is of Type 2 or context-free, if ll its hve the form A w where A N is nonterminl, which rewrites to string w (T N). C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) A Hierrchy of Lnguges A B or A or A ɛ 2. A grmmr G is right-liner, iff ll its hve the form A B or A or A ɛ 3. A grmmr G is of Type 3 or regulr, iff it is left-liner or right-liner In the ove we hve A, B N re nonterminl nd T. Note tht in regulr grmmr either ll must e left-liner or ll must e right-liner, so no mixing of the left-liner nd right-liner is llowed. C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Hierrchy of Lnguges A lnguge L T is unrestricted, context-sensitive, context-free, or regulr, iff there exists grmmr G of the relevnt type such tht L(G) = L. Remrk For ny L we hve L regulr L context-free L context-sensitive L unrestricted. We hve tht every regulr grmmr is context-free. every context-sensitive grmmr is n unrestricted grmmr. However not every context-free grmmr is context sensitive, since context-sensitive lnguges llow only A ɛ if A does not occur t the right hnd side of production. (Otherwise ll unrestricted lnguges would e context-sensitive). However one cn construct from context-free grmmr context-free grmmr of the sme lnguge, which hs only A ɛ, if A does not occur on the right hnd side of production. This grmmr is therefore context-sensitive s well. C 236 ect. I / 128 C 236 ect. I / 128

17 I.2.3. The Chomsky Hierrchy (12.1) I.2.3. The Chomsky Hierrchy (12.1) Hierrchy of Lnguges Exmples of Equivlent Grmmrs We give grmmrs of ech type for defining the lnguge regulr context-free context sensitive unrestricted L 2n := { i i is even } C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Unrestricted Grmmr for L 2n C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Context-ensitive Grmmr for L 2n grmmr G unrestricted,2n grmmr G context sensitive,2n terminls terminls nonterminls nonterminls, T strt symol strt symol ɛ ɛ T T T T C 236 ect. I / 128 C 236 ect. I / 128

18 I.2.3. The Chomsky Hierrchy (12.1) I.2.3. The Chomsky Hierrchy (12.1) Context-Free Grmmr for L 2n Regulr Grmmr for L 2n grmmr G context free,2n grmmr G regulr,2n terminls terminls nonterminls nonterminls, A strt symol strt symol ɛ ɛ A A C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) Exmple 1 (Grmmrs of the Levels of the Chomsky Hierrchy) Exmple 2 C 236 ect. I / 128 I.2.3. The Chomsky Hierrchy (12.1) grmmr G grmmr G terminls, terminls nonterminls nonterminls strt symol strt symol,, ɛ, L(G) =? G is of which type? L(G) =? G is of which type? C 236 ect. I / 128 C 236 ect. I / 128

19 Exmple 3 I.2.3. The Chomsky Hierrchy (12.1) Exmple 4 (Grmmrs) grmmr terminls nonterminls strt symol L(G) =? G is of which type? G,, Consider the grmmr grmmr terminls nonterminls strt symol G n n c n,, c, B, C BC, BC, CB BC, B, B, C c, cc cc. Exmple 4 is not context sensitive grmmr. Why? C 236 ect. I / 128 Exmple 5 (Grmmrs) Exmple Derivtion I.2.3. The Chomsky Hierrchy (12.1) Here is vrint which is context sensitive: grmmr terminls nonterminls strt symol G n n c n,, c, B, C, H BC, BC, CB HB, HB HC, HC BC, B, B, C c, cc cc. A derivtion of cc in Exmple 5 is s follows: BC BCBC BHBC BHCC BBCC BCC CC cc cc C 236 ect. I / 128

20 I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) Exmples I.2.1. Forml Lnguges (10.1) regulr { n n 1} context context-free sensitive { n n n 1} { n n c n n 1} unrestricted I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) pecifying yntx using Grmmrs: Modulrity nd BNF Nottion (10.3) A imple Modulr Grmmr for Progrmming Lnguge ( ) We introduce grmmr for simple while lnguge for computing nturl numers. It will hve the following components: identifiers, nturl numers, rithmetic expressions, Boolen expressions, progrms. C 236 ect. I / 128 Grmmr for Identifiers grmmr terminls nonterminls strt symol G Identifier,,..., z, A, B,..., Z Letter, Id Id Id Letter Id Letter Id Letter Letter Letter z Letter A Letter B Letter Z C 236 ect. I / 128

21 I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) Grmmr for Numers Import grmmr G Numer terminls 0, 1,..., 9 nonterminls strt symol Numer, Digit Numer Numer Digit Numer Digit Numer Digit 0 Digit 1 Digit 9 The grmmr for rithmetic expressions G Arithmetic Expression will import G Identifier G Numer. This mens the following: The terminls/nonterminls/ of these grmmrs re dded to the terminls/nonterminls/ of G Arithmetic Expression. C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) Grmmr for Arithmetic Expressions Amiguity C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) grmmr import G Arithmetic Expression G Identifier, G Numer terminls +,,, /, (, ) nonterminls strt symol AExp, AOp AExp AExp Id AExp Numer AExp ( AExp ) AExp AExp AOp AExp AOp + AOp AOp AOp / The grmmr G Arithmetic Expression is miguous, concept discussed in ection I.2.5. There we will discuss how to mke this grmmr unmiguous. The sme pplies to the grmmrs G Boolen Expression nd G while (ecuse of the sequencing opertion) introduced on the next two slides. C 236 ect. I / 128 C 236 ect. I / 128

22 Grmmr for Boolen Expressions grmmr import G Boolen Expression G Arithmetic Expression terminls true, flse, not, nd, or, =, < nonterminls strt symol BExp, BOp1, BOp2, RelOp BExp BExp BOp1 BExp BExp BExp BOp2 BExp BExp AExp RelOp AExp BExp true BExp flse BOp1 not BOp2 nd BOp2 or RelOp = RelOp < I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) User Friendly Grmmrs nd BNF Nottion (10.3.3) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) Grmmr for While Progrms grmmr import G while G Arithmetic Expression, G Boolen Expression terminls skip, if, then, else, fi, while, do, od, :=, ; nonterminls strt symol Exmple 1 Progrm Progrm Progrm skip Progrm Id := AExp Progrm Progrm ; Progrm Progrm if BExp then Progrm else Progrm fi Progrm while BExp do Progrm od C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) The Bckus-Nur-Form or BNF is the presenttion of grmmr using the following conventions: 1. The terminl symols re often written in old font. 2. The non-terminl symols re fmilir terms for syntctic components enclosed in ngle rckets, e.g. <sttement>, <expresssion>, <identifier>. 3. The strt symol is the non-terminl tht is presented first. 4. The symol ::= replces nd extends y listing the possile for non-terminl; lterntive possiilities for the right hnd sides of prticulr production re seprted y. For instnce the rules re condensed into one rule BExp ::= true BExp ::= flse BExp ::= BOp1 BExp BExp ::= BExp BOp2 BExp BExp ::= AExp RelOp AExp BExp ::= true flse BOp1 BExp BExp BOp2 BExp AExp RelOp AExp C 236 ect. I / 128 C 236 ect. I / 128

23 I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) Exmple 2 Extended BNF nf rules Letter Letter ::= LowerCse UpperCse LowerCse ::= c d e f g h i j k l m n o p q r s t u v w x y z UpperCse ::= A B C D E F G H I J K L M N O P Q R T U V W X Y Z Extended BNF or EBNF dds to BNF the following conventions: 1. An optionl occurrence of some portion of production rule choise is enclosed in squre rckets [ ]. [u] mens zero or one occurrence of u where u (T N) An ritrry numer of occurences of some portion of production rule choice is enclosed in rces { }. {u} mens zero or more occurrences of u where u (T N) +. Exmple C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) Exmple C 236 ect. I / 128 I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) In norml BNF, numers cn e defined s follows nf Numer rules Numer Digits Digit ::= Digit NonZeroDigit Digits ::= Digit Digit Digits ::= 0 NonZeroDigit NonZeroDigit ::= In EBNF, we cn define them s follows: enf Numer rules Numer Digit ::= 0 NonZeroDigit { Digit } ::= 0 NonZeroDigit NonZeroDigit ::= C 236 ect. I / 128 C 236 ect. I / 128

24 I.2.4. Modulrity nd BNF nottion (10.3) I BNF Nottion ( ) Theorem I Lemm Let L T e lnguge. L is definle in EBNF iff it is definle in BNF. Proof: Exercise. I.2.1. Forml Lnguges (10.1) I.2.2. Grmmrs nd Derivtions (10.2) I Grmmrs ( ) I Exmples of Grmmrs nd trings ( ) I Derivtions ( ) I Lnguge Genertion ( ) I Designing yntx using Grmmrs ( ) I.2.3. The Chomsky Hierrchy (12.1) I.2.4. Modulrity nd BNF nottion (10.3) I A simple modulr grmmr ( ) I The import construct ( ) I BNF Nottion ( ) C 236 ect. I / 128 Derivtion Trees or Prse Trees C 236 ect. I / 128 Exmple Consider the grmmr grmmr G Context free Grmmrs (revited s CFG in the following) llow to pply to non-terminl t position without needing the context. Therefore we cn expnd the non-terminls independently of ech other. This llows us to define derivtion trees (lso clled prse trees). terminls nonterminls strt symol, C 236 ect. I / 128 C 236 ect. I / 128

25 Exmple Derivtion Derivtion Tree We derive in it: C 236 ect. I / 128 Form of the Derivtion Tree C 236 ect. I / 128 Derivtion Tree Nodes re lelled with elements of N T {ɛ}. A node with lel A hs sutree A X 1 X 2... only if A is non-terminl nd there is production where X i T N. A X 1 X 2 X n All leves of the tree together red from left to right form the string derived, nmely. This is clled the frontier of the derivtion tree. We will s well consider derivtion trees not ending in string of terminls, so the frontier is n element of (T N). C 236 ect. I / 128 X n Let G = (T, N,, P) e CFG. A derivtion tree or prse tree for G is finite tree with nodes lelled y elements of N T {ɛ}, s.t. node A hs children with lels X 1,..., X n only if A N nd there is production A X 1 X 2 X n If the node of one of the children of A is ɛ, then this node is the only child of this tree. The frontier of the tree is the set of leves red from left to right in sequence, which is n element (T N). The root of the tree is the node t the to of the derivtion tree. C 236 ect. I / 128

26 Left-Most nd Right-Most Derivtions Exmple Derivtion Tree From derivtion tree we cn otin derivtion in vrious orders. Consider the grmmr grmmr G terminls, A B nonterminls, A, B strt symol AB, A A, A B B, B A B C 236 ect. I / 128 Different Derivtions of C 236 ect. I / 128 Left-Most Derivtion We cn derive in different wys: AB AB B B A left most derivtion AB AB A A A right most derivtion AB AB AB B AB AB AB A AB AB AB B Let G = (T, N,, P) e CFG. A single-step derivtion w w is left-most if rule ws pplied to the left-most non-terminl in w, i.e. w = sat for some A N, s T (consisting only of terminls), t ( T ), nd there exist production A v s.t. w = svt. AB AB AB A C 236 ect. I / 128 C 236 ect. I / 128

27 Left-Most Derivtion Left/Right-Most Derivtion equence Let G = (T, N,, P) e CFG. A single-step derivtion w w is right-most if rule ws pplied to the right-most non-terminl in w, i.e. w = sat for some A N, s ( T ), t T (consisting only of terminls), there exist production A v s.t. w = svt. Let G = (T, N,, P) e CFG 1. A derivtion sequence w 0 w 1 w 2 w n is left-most, if ech dervtion step w i w i+1 is left-most. 2. Right-most derivtion sequences re defined nlogously. C 236 ect. I / 128 Theorem I (Derivtion Trees nd Lnguge Genertion) C 236 ect. I / 128 Proof of Theorem I Theorem Let G = (T, N,, P) e CFG, A T, w, w (T N), Then the following re equivlent (1) There exist derivtion tree with root lelled y A nd frontier w. (2) A w. In cse w T, the derivtion sequence w w cn oth e chosen s left-most nd s right-most derivtion sequence A proof of this theorem cn e found in the dditionl mteril. We illustrte this theorem y n exmple. It will show oth: (red forwrds) how to otin from left-most derivtion derivtion tree, (red ckwrds) how to otin from derivtion tree left-most derivtion. C 236 ect. I / 128 C 236 ect. I / 128

28 Exmple (tep 1) Exmple (tep 2) AB A B A B A B A B C 236 ect. I / 128 Exmple (tep 3) C 236 ect. I / 128 Exmple (tep 4) AB AB AB AB B A B A B A B A B Derivtion tree for is trivil. C 236 ect. I / 128 C 236 ect. I / 128

29 Exmple (tep 5) Exmple (tep 6) AB AB B B AB AB B B A B A B A B A B C 236 ect. I / 128 Theorem I Uniqueness of Derivtion (Trees) Finl derivtion. C 236 ect. I / 128 Proof of Theorem I Theorem Let G = (T, N,, P) e CFG, A N, w T. (1) Assume there re two different derivtion trees with root lelled y A nd frontier w. Then there exist two different left-most nd two different right-most derivtions of A w. (2) Assume there re two different left-most derivtions or two different right-most-derivtions of A w. Then there exist two different derivtion trees of with root lelled y A nd frontier w. The exmple ove demonstrtes how derivtion trees re trnsformed into left-most derivtions in unique wy. A more forml proof cn e found in the dditionl mteril. C 236 ect. I / 128 C 236 ect. I / 128

30 Uniqueness of Derivtion (Trees) Amiguous Grmmrs A direct consequence of the theorem is the following: Theorem Let G = (T, N,, P) e CFG. The following re equivlent: (1) For every w T there exist t most one derivtion tree with lel nd frontier w. (2) For every w T there exist t most one left-most derivtion sequence w. (3) For every w T there exist t most one right-most derivtion sequence w. A CFG G = (T, N,, P) is miguous, if there is string w L(G) hving more thn one derivtion tree (or, equivlently, hving more thn one left-most or right-most derivtion). C 236 ect. I / 128 Exmple 1 C 236 ect. I / 128 Exmple 1 grmmr terminls nonterminls strt symol G, There re two left-most derivtions of : nd And two derivtion trees: C 236 ect. I / 128 C 236 ect. I / 128

31 Exmple 2: Dngling Else Exmple 2: Dngling Else Assume the following grmmr which is cut down version of the grmmr G while introduced in I.2.4 with if then else fi replced y if then nd if then else ): grmmr import G Dngling else G Arithmetic Expression, G Boolen Expression terminls if, then, else, :=, ; nonterminls strt symol Progrm Progrm Progrm Id := AExp Progrm if BExp then Progrm else Progrm Progrm if BExp then Progrm Assume strings 1, 2 deriving from BExp nd string s 1, s 2 deriving from Progrm. The string if 1 then if 2 then s 1 else s 2 hs two derivtion trees (we omit the derivtion trees for i, s i.) C 236 ect. I / 128 First Derivtion Tree C 236 ect. I / 128 econd Derivtion Tree Progrm Progrm if BExp then Progrm if BExp then Progrm else Progrm 1 if BExp then Progrm else Progrm 1 if BExp then Progrm s 2 2 s 1 s 2 2 s 1 C 236 ect. I / 128 C 236 ect. I / 128

32 Different Interprettions of the Progrm Execution following the Derivtion Tree 1 The two different derivtion trees of the progrm In the first the else cse elongs to the second if. It is executed if 1 is true nd 2 is flse. The progrm cn e using intendtion e written s follows: if 1 then if 2 then s 1 else s 2 correspond to two different wys of executing the progrm: if 1 then if 2 then s 1 else s 2 C 236 ect. I / 128 Execution following the Derivtion Tree 2 C 236 ect. I / olutions for olving the Prolem In the second derivtion tree, the else cse elongs to the first if. It is executed if 1 is flse. The progrm cn e using intendtion e written s follows: if 1 then if 2 else s 2 then s 1 There re 2 solutions for solving this prolem. The first solution is to dd to if then nd if then else symol fi (or some other keyword such s end if). lelling the end of the sttement. grmmr import G Unmiguous if G Arithmetic Expression, G Boolen Expression terminls if, then, else, :=, ; nonterminls strt symol Progrm Progrm Progrm Id := AExp Progrm if BExp then Progrm else Progrm fi Progrm if BExp then Progrm fi C 236 ect. I / 128 C 236 ect. I / 128

33 olution 1 olution 2 Now the two interprettions of the originl string would e written in s two different strings: Else elonging to the second if is written s if 1 then if 2 then s 1 else s 2 fi fi Else elong to the first if is written s if 1 then if 2 then s 1 fi else s 2 fi This solution hs een tken for instnce in Algol, in the sh shell (Linux), nd in Ad (where fi is replced y end if ). The 2nd solution is to modify the grmmr so tht the derivtion tree will e possile only for one of the two choices. For this one we modify the grmmr, tht the sttement s 1 in is not mtched y ut only y if 1 then s 1 else s 2 if 1 then s 1 if 1 then s 1 else s 2 This solution hs een tken in most other progrmming lnguges. C 236 ect. I / 128 olution 2 olution 2 C 236 ect. I / 128 For this we split Progrms into two ctegories: Those derived from MtchedIf. In progrm deriving from MtchedIf, ech if is mtched y n else cluse. Those derived from UnmtchedIf. These hve t lest one if with no mtching else cluse. The grmmr will mke sure tht else will lwys e ssocited with the first if to the left, which hs no unmtched else yet. o if then mthf else expression will e prsed s in the first derivtion tree. C 236 ect. I / 128

34 olution 2 Here is the grmmr. We omit sequencing (comining progrms using ; ), since it would result in n miguous grmmr, which needs to e resolved. grmmr import G Dngling Else G Arithmetic Expression, G Boolen Expression terminls if, then, else, :=, ; nonterminls strt symol Progrm Progrm Progrm UnmtchedIf Progrm MtchedIf MtchedIf Id := AExp MtchedIf if BExp then MtchedIf else MtchedIf UnmtchedIf if BExp then Progrm UnmtchedIf if BExp then MtchedIf else UnmtchedIf Exmple: Grmmr for Arithmetic Expressions Rememer the grmmr for rithmetic expressions (using elements of BNF nottion) Unique Derivtion Tree 2nd olution Progrm UnmtchedIf if BExp then Progrm 1 MtchedIf if BExp then MtchedIf else 2 s 1 MtchedIf C 236 ect. I / 128 First Prse tree for AExp s 2 grmmr import G Arithmetic Expression G Identifier, G Numer terminls +,,, /, (, ) nonterminls strt symol AExp, AOp AExp AExp Id Numer AExp ( AExp ) AExp AExp AOp AExp AOp + / AExp AExp AOp AOp AExp Numer + Numer 2 3 AExp Numer 4 C 236 ect. I / 128 C 236 ect. I / 128

35 econd Prse tree for Difference in Evlution AExp AExp Numer AOp + AExp AExp AOp AExp The first prse tree correponds to prsing it s if it were (2 + 3) 4 Evlution will return 20. The second prse tree correponds to prsing it s if it were 2 + (3 4) Evlution will return Numer Numer 3 4 C 236 ect. I / 128 Unmiguous Version C 236 ect. I / 128 Arithmetic Expression Unique Prse Tree for in Gunmiguous AExp grmmr import Arithmetic Expression Gunmiguous G Identifier, G Numer terminls +,,, /, (, ) nonterminls strt symol AExp, Term, Fctor AExp AExp AExp + Term AExp Term Term Term Term Fctor Term/Fctor Fctor Fctor Id Numer ( AExp ) AExp Term Fctor Numer + Term Fctor Numer Term Fctor Numer C 236 ect. I / 128 C 236 ect. I / 128

36 Mking Context Free Grmmrs Unmiguous The following is known out Context Free Grmmrs: There re lnguges defined y context free grmmrs which cnnot e defined y n unmiguous grmmr. Context free grmmrs, for which there exist no equivlent unmigous grmmr, re clled inherently miguous grmmrs. ee Hopcroft/Motwni/Ullmn, 5.4.4, p It is undecdile whether grmmr is miguous. (me ook, 7.4.5, p. 307.) It is undecdile whether grmmr is inherently miguous grmmrs. (me ook, nd 9.5.2, p. 413). C 236 ect. I / 128

Dr. D.M. Akbar Hussain

Dr. D.M. Akbar Hussain Dr. D.M. Akr Hussin Lexicl Anlysis. Bsic Ide: Red the source code nd generte tokens, it is similr wht humns will do to red in; just tking on the input nd reking it down in pieces. Ech token is sequence

More information

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22) Homework Context Free Lnguges III Prse Trees nd Homework #5 (due 10/22) From textbook 6.4,b 6.5b 6.9b,c 6.13 6.22 Pln for tody Context Free Lnguges Next clss of lnguges in our quest! Lnguges Recll. Wht

More information

COMP 423 lecture 11 Jan. 28, 2008

COMP 423 lecture 11 Jan. 28, 2008 COMP 423 lecture 11 Jn. 28, 2008 Up to now, we hve looked t how some symols in n lphet occur more frequently thn others nd how we cn sve its y using code such tht the codewords for more frequently occuring

More information

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table TDDD55 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing, Prt 2 Constructing Prse Tles Prse tle construction Grmmr conflict hndling Ctegories of LR Grmmrs nd Prsers Peter Fritzson, Christoph

More information

CMPSC 470: Compiler Construction

CMPSC 470: Compiler Construction CMPSC 47: Compiler Construction Plese complete the following: Midterm (Type A) Nme Instruction: Mke sure you hve ll pges including this cover nd lnk pge t the end. Answer ech question in the spce provided.

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5 CS321 Lnguges nd Compiler Design I Winter 2012 Lecture 5 1 FINITE AUTOMATA A non-deterministic finite utomton (NFA) consists of: An input lphet Σ, e.g. Σ =,. A set of sttes S, e.g. S = {1, 3, 5, 7, 11,

More information

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

In the last lecture, we discussed how valid tokens may be specified by regular expressions. LECTURE 5 Scnning SYNTAX ANALYSIS We know from our previous lectures tht the process of verifying the syntx of the progrm is performed in two stges: Scnning: Identifying nd verifying tokens in progrm.

More information

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011 CSCI 3130: Forml Lnguges nd utomt Theory Lecture 12 The Chinese University of Hong Kong, Fll 2011 ndrej Bogdnov In progrmming lnguges, uilding prse trees is significnt tsk ecuse prse trees tell us the

More information

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the LR() nlysis Drwcks of LR(). Look-hed symols s eplined efore, concerning LR(), it is possile to consult the net set to determine, in the reduction sttes, for which symols it would e possile to perform reductions.

More information

Definition of Regular Expression

Definition of Regular Expression Definition of Regulr Expression After the definition of the string nd lnguges, we re redy to descrie regulr expressions, the nottion we shll use to define the clss of lnguges known s regulr sets. Recll

More information

Fig.25: the Role of LEX

Fig.25: the Role of LEX The Lnguge for Specifying Lexicl Anlyzer We shll now study how to uild lexicl nlyzer from specifiction of tokens in the form of list of regulr expressions The discussion centers round the design of n existing

More information

CS 430 Spring Mike Lam, Professor. Parsing

CS 430 Spring Mike Lam, Professor. Parsing CS 430 Spring 2015 Mike Lm, Professor Prsing Syntx Anlysis We cn now formlly descrie lnguge's syntx Using regulr expressions nd BNF grmmrs How does tht help us? Syntx Anlysis We cn now formlly descrie

More information

What are suffix trees?

What are suffix trees? Suffix Trees 1 Wht re suffix trees? Allow lgorithm designers to store very lrge mount of informtion out strings while still keeping within liner spce Allow users to serch for new strings in the originl

More information

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing Motivting Exmple Lexicl nd yntx Anlysis (2) In Text: Chpter 4 Consider the grmmr -> cad A -> b Input string: w = cd How to build prse tree top-down? 2 Initilly crete tree contining single node (the strt

More information

10.5 Graphing Quadratic Functions

10.5 Graphing Quadratic Functions 0.5 Grphing Qudrtic Functions Now tht we cn solve qudrtic equtions, we wnt to lern how to grph the function ssocited with the qudrtic eqution. We cll this the qudrtic function. Grphs of Qudrtic Functions

More information

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata CS 432 Fll 2017 Mike Lm, Professor (c)* Regulr Expressions nd Finite Automt Compiltion Current focus "Bck end" Source code Tokens Syntx tree Mchine code chr dt[20]; int min() { flot x = 42.0; return 7;

More information

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards A Tutology Checker loosely relted to Stålmrck s Algorithm y Mrtin Richrds mr@cl.cm.c.uk http://www.cl.cm.c.uk/users/mr/ University Computer Lortory New Museum Site Pemroke Street Cmridge, CB2 3QG Mrtin

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

ASTs, Regex, Parsing, and Pretty Printing

ASTs, Regex, Parsing, and Pretty Printing ASTs, Regex, Prsing, nd Pretty Printing CS 2112 Fll 2016 1 Algeric Expressions To strt, consider integer rithmetic. Suppose we hve the following 1. The lphet we will use is the digits {0, 1, 2, 3, 4, 5,

More information

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs. Lecture 5 Wlks, Trils, Pths nd Connectedness Reding: Some of the mteril in this lecture comes from Section 1.2 of Dieter Jungnickel (2008), Grphs, Networks nd Algorithms, 3rd edition, which is ville online

More information

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08 CS412/413 Introduction to Compilers Tim Teitelum Lecture 4: Lexicl Anlyzers 28 Jn 08 Outline DFA stte minimiztion Lexicl nlyzers Automting lexicl nlysis Jlex lexicl nlyzer genertor CS 412/413 Spring 2008

More information

CSE 401 Midterm Exam 11/5/10 Sample Solution

CSE 401 Midterm Exam 11/5/10 Sample Solution Question 1. egulr expressions (20 points) In the Ad Progrmming lnguge n integer constnt contins one or more digits, but it my lso contin embedded underscores. Any underscores must be preceded nd followed

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy Recognition of Tokens if expressions nd reltionl opertors if è if then è then else è else relop

More information

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong LING/C SC/PSYC 438/538 Lecture 21 Sndiwy Fong Tody's Topics Homework 8 Review Optionl Homework 9 (mke up on Homework 7) Homework 8 Review Question1: write Prolog regulr grmmr for the following lnguge:

More information

CSCE 531, Spring 2017, Midterm Exam Answer Key

CSCE 531, Spring 2017, Midterm Exam Answer Key CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: (

More information

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2014 Dec 11 th /13 th Finl Exm Nme: Note: in ll questions, the specil symol ɛ (epsilon) is used to indicte the empty string. Question 1. [5 points] Consider the following regulr expression;

More information

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries Tries Yufei To KAIST April 9, 2013 Y. To, April 9, 2013 Tries In this lecture, we will discuss the following exct mtching prolem on strings. Prolem Let S e set of strings, ech of which hs unique integer

More information

Topic 2: Lexing and Flexing

Topic 2: Lexing and Flexing Topic 2: Lexing nd Flexing COS 320 Compiling Techniques Princeton University Spring 2016 Lennrt Beringer 1 2 The Compiler Lexicl Anlysis Gol: rek strem of ASCII chrcters (source/input) into sequence of

More information

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis CS143 Hndout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexicl Anlysis In this first written ssignment, you'll get the chnce to ply round with the vrious constructions tht come up when doing lexicl

More information

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number> DDD16 Compilers nd Interpreters DDB44 Compiler Construction R Prsing Prt 1 R prsing concept Using prser genertor Prse ree Genertion Wht is R-prsing? eft-to-right scnning R Rigthmost derivtion in reverse

More information

CS 241 Week 4 Tutorial Solutions

CS 241 Week 4 Tutorial Solutions CS 4 Week 4 Tutoril Solutions Writing n Assemler, Prt & Regulr Lnguges Prt Winter 8 Assemling instrutions utomtilly. slt $d, $s, $t. Solution: $d, $s, nd $t ll fit in -it signed integers sine they re 5-it

More information

CMSC 331 First Midterm Exam

CMSC 331 First Midterm Exam 0 00/ 1 20/ 2 05/ 3 15/ 4 15/ 5 15/ 6 20/ 7 30/ 8 30/ 150/ 331 First Midterm Exm 7 October 2003 CMC 331 First Midterm Exm Nme: mple Answers tudent ID#: You will hve seventy-five (75) minutes to complete

More information

Compilers Spring 2013 PRACTICE Midterm Exam

Compilers Spring 2013 PRACTICE Midterm Exam Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs

More information

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) * Pln for Tody nd Beginning Next week Interpreter nd Compiler Structure, or Softwre Architecture Overview of Progrmming Assignments The MeggyJv compiler we will e uilding. Regulr Expressions Finite Stte

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming Lecture 10 Evolutionry Computtion: Evolution strtegies nd genetic progrmming Evolution strtegies Genetic progrmming Summry Negnevitsky, Person Eduction, 2011 1 Evolution Strtegies Another pproch to simulting

More information

Theory of Computation CSE 105

Theory of Computation CSE 105 $ $ $ Theory of Computtion CSE 105 Regulr Lnguges Study Guide nd Homework I Homework I: Solutions to the following problems should be turned in clss on July 1, 1999. Instructions: Write your nswers clerly

More information

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Lexicl Anlysis Amith Snyl (www.cse.iit.c.in/ s) Deprtment of Computer Science nd Engineering, Indin Institute of Technology, Bomy Septemer 27 College of Engineering, Pune Lexicl Anlysis: 2/6 Recp The input

More information

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009 Deprtment of Computer cience Columbi University mple Midterm olutions COM W4115 Progrmming Lnguges nd Trnsltors Mondy, October 12, 2009 Closed book, no ids. ch question is worth 20 points. Question 5(c)

More information

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2016 Sep 29th Exm 1 Nme: Note: in ll questions, the speil symol ɛ (epsilon) is used to indite the empty string. Question 1. [10 points] Speify regulr expression tht genertes the lnguge over

More information

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing Undergrdute Compilers Review nd Intro to MJC Announcements Miling list is in full swing Tody Some thoughts on grd school Finish prsing Semntic nlysis Visitor pttern for bstrct syntx trees Some Thoughts

More information

Lexical analysis, scanners. Construction of a scanner

Lexical analysis, scanners. Construction of a scanner Lexicl nlysis scnners (NB. Pges 4-5 re for those who need to refresh their knowledge of DFAs nd NFAs. These re not presented during the lectures) Construction of scnner Tools: stte utomt nd trnsition digrms.

More information

Reducing a DFA to a Minimal DFA

Reducing a DFA to a Minimal DFA Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. Hrry H. Porter,

More information

Lexical Analysis: Constructing a Scanner from Regular Expressions

Lexical Analysis: Constructing a Scanner from Regular Expressions Lexicl Anlysis: Constructing Scnner from Regulr Expressions Gol Show how to construct FA to recognize ny RE This Lecture Convert RE to n nondeterministic finite utomton (NFA) Use Thompson s construction

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 3: Lexer genertors Viktor Leijon Slides lrgely y John Nordlnder with mteril generously provided y Mrk P. Jones. 1 Recp: Hndwritten Lexers: Don t require sophisticted

More information

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers? 1.1 TEXAS ESSENTIAL KNOWLEDGE AND SKILLS Prepring for 2A.6.K, 2A.7.I Intervl Nottion nd Set Nottion Essentil Question When is it convenient to use set-uilder nottion to represent set of numers? A collection

More information

ECE 468/573 Midterm 1 September 28, 2012

ECE 468/573 Midterm 1 September 28, 2012 ECE 468/573 Midterm 1 September 28, 2012 Nme:! Purdue emil:! Plese sign the following: I ffirm tht the nswers given on this test re mine nd mine lone. I did not receive help from ny person or mteril (other

More information

2014 Haskell January Test Regular Expressions and Finite Automata

2014 Haskell January Test Regular Expressions and Finite Automata 0 Hskell Jnury Test Regulr Expressions nd Finite Automt This test comprises four prts nd the mximum mrk is 5. Prts I, II nd III re worth 3 of the 5 mrks vilble. The 0 Hskell Progrmming Prize will be wrded

More information

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CS 321 Programming Languages and Compilers. Bottom Up Parsing CS 321 Progrmming nguges nd Compilers Bottom Up Prsing Bottom-up Prsing: Shift-reduce prsing Grmmr H: fi ; fi b Input: ;;b hs prse tree ; ; b 2 Dt for Shift-reduce Prser Input string: sequence of tokens

More information

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or Opertor Precedence Most progrmming lnguges hve opertor precedence rules tht stte the order in which opertors re pplied (in the sence of explicit prentheses). Thus in C nd Jv nd CSX, +*c mens compute *c,

More information

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig CS311H: Discrete Mthemtics Grph Theory IV Instructor: Işıl Dillig Instructor: Işıl Dillig, CS311H: Discrete Mthemtics Grph Theory IV 1/25 A Non-plnr Grph Regions of Plnr Grph The plnr representtion of

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy RecogniNon of Tokens if expressions nd relnonl opertors if è if then è then else è else relop è

More information

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona CSc 453 Compilers nd Systems Softwre 4 : Lexicl Anlysis II Deprtment of Computer Science University of Arizon collerg@gmil.com Copyright c 2009 Christin Collerg Implementing Automt NFAs nd DFAs cn e hrd-coded

More information

Functor (1A) Young Won Lim 10/5/17

Functor (1A) Young Won Lim 10/5/17 Copyright (c) 2016-2017 Young W. Lim. Permission is grnted to copy, distribute nd/or modify this document under the terms of the GNU Free Documenttion License, Version 1.2 or ny lter version published

More information

The Fundamental Theorem of Calculus

The Fundamental Theorem of Calculus MATH 6 The Fundmentl Theorem of Clculus The Fundmentl Theorem of Clculus (FTC) gives method of finding the signed re etween the grph of f nd the x-xis on the intervl [, ]. The theorem is: FTC: If f is

More information

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex Long Quiz2 45mins Nme: Personl Numer: Prolem. (20pts) Here is n Tle of Perl Regulr Ex Chrcter Description. single chrcter \s whitespce chrcter (spce, t, newline) \S non-whitespce chrcter \d digit (0-9)

More information

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples COMPUTER SCIENCE 123 Foundtions of Computer Science 6. Tuples Summry: This lecture introduces tuples in Hskell. Reference: Thompson Sections 5.1 2 R.L. While, 2000 3 Tuples Most dt comes with structure

More information

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona Implementing utomt Sc 5 ompilers nd Systems Softwre : Lexicl nlysis II Deprtment of omputer Science University of rizon collerg@gmil.com opyright c 009 hristin ollerg NFs nd DFs cn e hrd-coded using this

More information

Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example

Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example Bottom up prsing Generl e LR0) LR LR1) LLR o est exploit JvCUP, should understnd the theoreticl sis LR prsing); op-down vs Bottom-up Bottom-up more powerful thn top-down; Cn process more powerful grmmr

More information

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins. Problem Set 2 Fll 16 Due: Wednesdy, September 21th, in clss, before clss begins. 1. LL Prsing For the following sub-problems, consider the following context-free grmmr: S T$ (1) T A (2) T bbb (3) A T (4)

More information

Typing with Weird Keyboards Notes

Typing with Weird Keyboards Notes Typing with Weird Keyords Notes Ykov Berchenko-Kogn August 25, 2012 Astrct Consider lnguge with n lphet consisting of just four letters,,,, nd. There is spelling rule tht sys tht whenever you see n next

More information

Lexical Analysis and Lexical Analyzer Generators

Lexical Analysis and Lexical Analyzer Generators 1 Lexicl Anlysis nd Lexicl Anlyzer Genertors Chpter 3 COP5621 Compiler Construction Copyright Roert vn Engelen, Florid Stte University, 2007-2009 2 The Reson Why Lexicl Anlysis is Seprte Phse Simplifies

More information

Suffix trees, suffix arrays, BWT

Suffix trees, suffix arrays, BWT ALGORITHMES POUR LA BIO-INFORMATIQUE ET LA VISUALISATION COURS 3 Rluc Uricru Suffix trees, suffix rrys, BWT Bsed on: Suffix trees nd suffix rrys presenttion y Him Kpln Suffix trees course y Pco Gomez Liner-Time

More information

Algorithm Design (5) Text Search

Algorithm Design (5) Text Search Algorithm Design (5) Text Serch Tkshi Chikym School of Engineering The University of Tokyo Text Serch Find sustring tht mtches the given key string in text dt of lrge mount Key string: chr x[m] Text Dt:

More information

12-B FRACTIONS AND DECIMALS

12-B FRACTIONS AND DECIMALS -B Frctions nd Decimls. () If ll four integers were negtive, their product would be positive, nd so could not equl one of them. If ll four integers were positive, their product would be much greter thn

More information

Section 3.1: Sequences and Series

Section 3.1: Sequences and Series Section.: Sequences d Series Sequences Let s strt out with the definition of sequence: sequence: ordered list of numbers, often with definite pttern Recll tht in set, order doesn t mtter so this is one

More information

Functor (1A) Young Won Lim 8/2/17

Functor (1A) Young Won Lim 8/2/17 Copyright (c) 2016-2017 Young W. Lim. Permission is grnted to copy, distribute nd/or modify this document under the terms of the GNU Free Documenttion License, Version 1.2 or ny lter version published

More information

COMBINATORIAL PATTERN MATCHING

COMBINATORIAL PATTERN MATCHING COMBINATORIAL PATTERN MATCHING Genomic Repets Exmple of repets: ATGGTCTAGGTCCTAGTGGTC Motivtion to find them: Genomic rerrngements re often ssocited with repets Trce evolutionry secrets Mny tumors re chrcterized

More information

Midterm I Solutions CS164, Spring 2006

Midterm I Solutions CS164, Spring 2006 Midterm I Solutions CS164, Spring 2006 Februry 23, 2006 Plese red ll instructions (including these) crefully. Write your nme, login, SID, nd circle the section time. There re 8 pges in this exm nd 4 questions,

More information

Simplifying Algebra. Simplifying Algebra. Curriculum Ready.

Simplifying Algebra. Simplifying Algebra. Curriculum Ready. Simplifying Alger Curriculum Redy www.mthletics.com This ooklet is ll out turning complex prolems into something simple. You will e le to do something like this! ( 9- # + 4 ' ) ' ( 9- + 7-) ' ' Give this

More information

A dual of the rectangle-segmentation problem for binary matrices

A dual of the rectangle-segmentation problem for binary matrices A dul of the rectngle-segmenttion prolem for inry mtrices Thoms Klinowski Astrct We consider the prolem to decompose inry mtrix into smll numer of inry mtrices whose -entries form rectngle. We show tht

More information

Assignment 4. Due 09/18/17

Assignment 4. Due 09/18/17 Assignment 4. ue 09/18/17 1. ). Write regulr expressions tht define the strings recognized by the following finite utomt: b d b b b c c b) Write FA tht recognizes the tokens defined by the following regulr

More information

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables George Boole IT 3123 Hrdwre nd Softwre Concepts My 28 Digitl Logic The Little Mn Computer 1815 1864 British mthemticin nd philosopher Mny contriutions to mthemtics. Boolen lger: n lger over finite sets

More information

MATH 25 CLASS 5 NOTES, SEP

MATH 25 CLASS 5 NOTES, SEP MATH 25 CLASS 5 NOTES, SEP 30 2011 Contents 1. A brief diversion: reltively prime numbers 1 2. Lest common multiples 3 3. Finding ll solutions to x + by = c 4 Quick links to definitions/theorems Euclid

More information

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1 Deterministic Finite Automt And Regulr Lnguges Fll 2018 Costs Busch - RPI 1 Deterministic Finite Automton (DFA) Input Tpe String Finite Automton Output Accept or Reject Fll 2018 Costs Busch - RPI 2 Trnsition

More information

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code

More information

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2.

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2. TDDB9 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing Updted/New slide mteril 007: Pushdown Automton for LR-Prsing Finite-stte pushdown utomton contins lterntingly sttes nd symols in NUΣ

More information

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have Rndom Numers nd Monte Crlo Methods Rndom Numer Methods The integrtion methods discussed so fr ll re sed upon mking polynomil pproximtions to the integrnd. Another clss of numericl methods relies upon using

More information

Improper Integrals. October 4, 2017

Improper Integrals. October 4, 2017 Improper Integrls October 4, 7 Introduction We hve seen how to clculte definite integrl when the it is rel number. However, there re times when we re interested to compute the integrl sy for emple 3. Here

More information

Slides for Data Mining by I. H. Witten and E. Frank

Slides for Data Mining by I. H. Witten and E. Frank Slides for Dt Mining y I. H. Witten nd E. Frnk Simplicity first Simple lgorithms often work very well! There re mny kinds of simple structure, eg: One ttriute does ll the work All ttriutes contriute eqully

More information

Presentation Martin Randers

Presentation Martin Randers Presenttion Mrtin Rnders Outline Introduction Algorithms Implementtion nd experiments Memory consumption Summry Introduction Introduction Evolution of species cn e modelled in trees Trees consist of nodes

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Progrmming Lnguges h"p://www.di.unipi.it/~ndre/did2c/plp- 14/ Prof. Andre Corrdini Deprtment of Computer Science, Pis Lesson 5! Gener;on of Lexicl Anlyzers Creting Lexicl Anlyzer with Lex

More information

2 Computing all Intersections of a Set of Segments Line Segment Intersection

2 Computing all Intersections of a Set of Segments Line Segment Intersection 15-451/651: Design & Anlysis of Algorithms Novemer 14, 2016 Lecture #21 Sweep-Line nd Segment Intersection lst chnged: Novemer 8, 2017 1 Preliminries The sweep-line prdigm is very powerful lgorithmic design

More information

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus Unit #9 : Definite Integrl Properties, Fundmentl Theorem of Clculus Gols: Identify properties of definite integrls Define odd nd even functions, nd reltionship to integrl vlues Introduce the Fundmentl

More information

Lecture 7: Integration Techniques

Lecture 7: Integration Techniques Lecture 7: Integrtion Techniques Antiderivtives nd Indefinite Integrls. In differentil clculus, we were interested in the derivtive of given rel-vlued function, whether it ws lgeric, eponentil or logrithmic.

More information

Suffix Tries. Slides adapted from the course by Ben Langmead

Suffix Tries. Slides adapted from the course by Ben Langmead Suffix Tries Slides dpted from the course y Ben Lngmed en.lngmed@gmil.com Indexing with suffixes Until now, our indexes hve een sed on extrcting sustrings from T A very different pproch is to extrct suffixes

More information

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016 Applied Dtses Lecture 13 Online Pttern Mtching on Strings Sestin Mneth University of Edinurgh - Ferury 29th, 2016 2 Outline 1. Nive Method 2. Automton Method 3. Knuth-Morris-Prtt Algorithm 4. Boyer-Moore

More information

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev Fll 2016-2017 Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University of the Negev Agend Understnd role of lexicl nlysis in compiler Regulr lnguges reminder Lexicl nlysis lgorithms

More information

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I C 45 Compilers n ystems oftwre 6 : op-down Prsing I Christin Collberg Deprtment of Computer iene University of rizon ollberg@gmil.om Copyright 2009 Christin Collberg eptember 14, 2009 1 Overview 2 Compiler

More information

MTH 146 Conics Supplement

MTH 146 Conics Supplement 105- Review of Conics MTH 146 Conics Supplement In this section we review conics If ou ne more detils thn re present in the notes, r through section 105 of the ook Definition: A prol is the set of points

More information

Misrepresentation of Preferences

Misrepresentation of Preferences Misrepresenttion of Preferences Gicomo Bonnno Deprtment of Economics, University of Cliforni, Dvis, USA gfbonnno@ucdvis.edu Socil choice functions Arrow s theorem sys tht it is not possible to extrct from

More information

9 4. CISC - Curriculum & Instruction Steering Committee. California County Superintendents Educational Services Association

9 4. CISC - Curriculum & Instruction Steering Committee. California County Superintendents Educational Services Association 9. CISC - Curriculum & Instruction Steering Committee The Winning EQUATION A HIGH QUALITY MATHEMATICS PROFESSIONAL DEVELOPMENT PROGRAM FOR TEACHERS IN GRADES THROUGH ALGEBRA II STRAND: NUMBER SENSE: Rtionl

More information

CS481: Bioinformatics Algorithms

CS481: Bioinformatics Algorithms CS481: Bioinformtics Algorithms Cn Alkn EA509 clkn@cs.ilkent.edu.tr http://www.cs.ilkent.edu.tr/~clkn/teching/cs481/ EXACT STRING MATCHING Fingerprint ide Assume: We cn compute fingerprint f(p) of P in

More information

Eliminating left recursion grammar transformation. The transformed expression grammar

Eliminating left recursion grammar transformation. The transformed expression grammar Eliminting left recursion grmmr trnsformtion Originl! rnsformed! 0 0! 0 α β α α α α α α α α β he two grmmrs generte the sme lnguge, but the one on the right genertes the rst, nd then string of s, using

More information

Regular Expressions and Automata using Miranda

Regular Expressions and Automata using Miranda Regulr Expressions nd Automt using Mirnd Simon Thompson Computing Lortory Univerisity of Kent t Cnterury My 1995 Contents 1 Introduction ::::::::::::::::::::::::::::::::: 1 2 Regulr Expressions :::::::::::::::::::::::::::::

More information

Information Retrieval and Organisation

Information Retrieval and Organisation Informtion Retrievl nd Orgnistion Suffix Trees dpted from http://www.mth.tu.c.il/~himk/seminr02/suffixtrees.ppt Dell Zhng Birkeck, University of London Trie A tree representing set of strings { } eef d

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp nd Scheme Lists in Lisp nd Scheme Lists re Lisp s fundmentl dt structures, ut there re others Arrys, chrcters, strings, etc. Common Lisp hs moved on from eing merely LISt Processor However,

More information

Lecture 12 : Topological Spaces

Lecture 12 : Topological Spaces Leture 12 : Topologil Spes 1 Topologil Spes Topology generlizes notion of distne nd loseness et. Definition 1.1. A topology on set X is olletion T of susets of X hving the following properties. 1. nd X

More information

From Dependencies to Evaluation Strategies

From Dependencies to Evaluation Strategies From Dependencies to Evlution Strtegies Possile strtegies: 1 let the user define the evlution order 2 utomtic strtegy sed on the dependencies: use locl dependencies to determine which ttriutes to compute

More information

1.5 Extrema and the Mean Value Theorem

1.5 Extrema and the Mean Value Theorem .5 Extrem nd the Men Vlue Theorem.5. Mximum nd Minimum Vlues Definition.5. (Glol Mximum). Let f : D! R e function with domin D. Then f hs n glol mximum vlue t point c, iff(c) f(x) for ll x D. The vlue

More information