











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These are the Lecture Slides of Compiler Design which includes Representing Types, Type Equality, Type Coercions, Kinds of Type Systems, Primitive Types, Inherited Attributes, Computing Attribute Computations, Describing Type Systems etc. Key important points are: Data Structure for Grammars, Computing Nullable, Datatype for Grammars, Example Grammar, Operations on Grammars, Follow Compute, Basic Strategy, Record Changes, Main Loop, Regular Expression
Typology: Slides
1 / 19
This page cannot be seen from the preview
Don't miss anything!












fun inputc fname =
(fn n => TextIO.inputN(TextIO.openIn fname,n):string);
val lex = Mlex.makeLexer (inputc "test.english");
fun inputc h =
(fn n =>
TextIO.inputN(h,n):string);
val lex = let val h = TextIO.openIn “test.english”
in Mlex.makeLexer (inputc h) end;
val g1 = Gram(["Expr","Term","Factor","Expr'","Term'"] ,["ident","+","-","","div","(",")","num"] ,[("Expr",["Term","Expr'"]) ,("Expr'",["+","Term","Expr'"]) ,("Expr'",["-","Term","Expr'"]) ,("Expr'",[]) ,("Term",["Factor","Term'"]) ,("Term'",["","Factor","Term'"]) ,("Term'",["div","Factor","Term'"]) ,("Term'",[]) ,("Factor",["(","Expr",")"]) ,("Factor",["num"]) ,("Factor",["ident"]) ] ,"Expr");
fun termP (Gram(nts,ts,ps,s)) x =
List.exists (fn y => y=x) ts;
fun nontermP (Gram(nts,ts,ps,s)) x =
List.exists (fn y => y=x) nts;
fun rhssFor (Gram(nts,ts,ps,start))
symbol =
let fun test (lhs,rhs) = lhs=symbol
fun rhs (l,r) = r
in List.map rhs (List.filter test
ps) end;
we continue until no new changes are possible.
(* Update the slot at "s" with the function "updatef" *)
(* returns true if it makes a change, false otherwise *)
fun update s updatef pairs =
case List.find (fn (y,r) => s=y) pairs of
SOME(_,r) => let val old = !r
val new = updatef old
in if new=old
then false
else (r := new; true) end
| NONE => false
let val table = init … symbols
val changed = ref true
in while (!changed) do
(changed := false
; changed := onePass symbols);
table
end
pass.
and nullable table s =
case List.find (fn (y,r) => y=s)
table of
NONE => false
| SOME(_,r) => !r
and nullableRhs table [] = true
| nullableRhs table (x::xs) =
(nullable table x) andalso
(nullableRhs table xs);
val NullT = nullTable g1;
val it =
[("Expr",ref false), ("Term",ref false), ("Factor",ref false), ("Expr'",ref true), ("Term'",ref true), ("ident",ref false), ("+",ref false), ("-",ref false), ("*",ref false), ("div",ref false), ("(",ref false), (")",ref false), ("num",ref false)] : (string * bool ref) list
and first table s =
case List.find (fn (y,r) => y=s) table of
NONE => []
| SOME(_,r) => !r
and firstRhs gram table [] = [empty]
| firstRhs gram table [x] = first table x
| firstRhs gram table (x::xs) =
let val temp = first table x
in case List.find emptyP temp of
NONE => temp
| SOME _ => temp @ firstRhs gram table xs
end;
fun firstTable (gram as(Gram(nts,ts,ps,start))) = let val termTable = init ts (fn x => [x]) val nontermTable = init nts (fn x => []) val table = termTable @ nontermTable val changed = ref true fun onePass [] = false | onePass (x::xs) = let val rhss = rhssFor gram x fun first old = let val listOflists = map (firstRhs gram table) rhss val new = List.concat listOflists in norm(old @ new) end val b1 = update x first table val b2 = onePass xs in b1 orelse b2 end
in while (!changed) do (changed := false; changed := onePass nts); table end