module Examples where import List import AGParser import TypeAg {- Note that each sample grammar is needed to be copied in its own file before execution. Each file should include module FileName where import List import AGParser import TypeAg -} --------------- SAMPLE GRAMMAR 1 FOR {a^nb^nc^n|n>0} ---------------- start = memoize Start (parser (nt x S1) [] ) x = memoize Xs (parser (nt a S0 *> nt b S1 *> nt c S2 ) [rule_s Kill OF LHS EQ nomatch [synthesized VAL OF S0 , synthesized VAL OF S1, synthesized VAL OF S2]--, --rule_i MaxVal OF S2 EQ copy [synthesized VAL OF S0 ], --rule_i MaxVal OF S0 EQ copy [inherited MaxVal OF S2] ] ) a = memoize As (parser (nt a S3 *> nt term_a S7 ) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S3] --, -- rule_s Kill OF LHS EQ nomatch [synthesized VAL OF S3,synthesized VAL OF S7] ] <|> parser (nt term_a S6) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S6] ] ) b = memoize Bs (parser (nt b S4 *> nt term_b S1) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S4] ] <|> parser (nt term_b S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) c = memoize Cs (parser (nt c S5 *> nt term_c S1) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S5] ] <|> parser (nt term_c S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) term_a = memoize TA (terminal (term "a") [VAL 1] ) term_b = memoize TB (terminal (term "b") [VAL 1] ) term_c = memoize TC (terminal (term "c") [VAL 1] ) ---- semantic functions ---- assignVal [v] = \atts -> v incVal [v] = \atts -> VAL ((getAtts getAVAL atts v ) + 1) incMVal [v] = \atts -> MaxVal ((getAtts getAVAL atts v ) + 1) copyVal [v] = \atts -> VAL (getAtts getAVAL atts v) ---- TESTING main = aTest aTest = meaning_of start Start "a a b b c c" --------------- SAMPLE GRAMMAR1 FOR {a^mb^nc^md^n|n>0} ---------------- start = memoize Start (parser (nt x S1) [] ) x = memoize Xs (parser (nt a S0 *> nt b S1 *> nt c S2 *> nt d S3 ) [rule_s Kill OF LHS EQ nocrossmatch [synthesized VAL OF S0 , synthesized VAL OF S1, synthesized VAL OF S2, synthesized VAL OF S3] ] ) a = memoize As (parser (nt a S3 *> nt term_a S7 ) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S3] ] <|> parser (nt term_a S6) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S6] ] ) b = memoize Bs (parser (nt b S4 *> nt term_b S1) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S4] ] <|> parser (nt term_b S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) c = memoize Cs (parser (nt c S5 *> nt term_c S1) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S5] ] <|> parser (nt term_c S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) d = memoize Ds (parser (nt d S5 *> nt term_d S1) [rule_s VAL OF LHS EQ incVal [synthesized VAL OF S5] ] <|> parser (nt term_d S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) term_a = memoize TA (terminal (term "a") [VAL 1] ) term_b = memoize TB (terminal (term "b") [VAL 1] ) term_c = memoize TC (terminal (term "c") [VAL 1] ) term_d = memoize TD (terminal (term "d") [VAL 1] ) ---- semantic functions ---- assignVal [v] = \atts -> v incVal [v] = \atts -> VAL ((getAtts getAVAL atts v ) + 1) incMVal [v] = \atts -> MaxVal ((getAtts getAVAL atts v ) + 1) copyVal [v] = \atts -> VAL (getAtts getAVAL atts v) nocrossmatch [e1,e2,e3,e4] = \atts -> if ((getAtts getAVAL atts e1) == (getAtts getAVAL atts e3)) && ((getAtts getAVAL atts e2) == (getAtts getAVAL atts e4)) then (Kill False) else (Kill True) ---- TESTING main = aTest aTest = meaning_of start Start "a b b c d d" --------------- SAMPLE GRAMMAR1 FOR 2-place copy language ---------------- start = memoize Start (parser (nt x S1) [] ) x = memoize Xs (parser (nt w S0 *> nt w S1 ) [rule_s Kill OF LHS EQ nomatchList [synthesized List OF S0 , synthesized List OF S1] ] ) w = memoize As (parser (nt term_a S0 *> nt w S1 ) [rule_s List OF LHS EQ addToaList [synthesized VAL OF S0,synthesized List OF S1] ] <|> parser (nt term_b S2 *> nt w S3 ) [rule_s List OF LHS EQ addToaList [synthesized VAL OF S2,synthesized List OF S3] ] <|> parser (nt term_a S4) [rule_s List OF LHS EQ formaList [synthesized VAL OF S4] ] <|> parser (nt term_b S5) [rule_s List OF LHS EQ formaList [synthesized VAL OF S5] ] ) term_a = memoize TA (terminal (term "a") [VAL 0] ) term_b = memoize TB (terminal (term "b") [VAL 1] ) ---- semantic functions ---- addToaList [a,v] = \atts -> List ((getAtts getAVAL atts a):(getAtts getList atts v)) formaList [v] = \atts -> List ([(getAtts getAVAL atts v)]) assignVal [v] = \atts -> v incVal [v] = \atts -> VAL ((getAtts getAVAL atts v ) + 1) incMVal [v] = \atts -> MaxVal ((getAtts getAVAL atts v ) + 1) copyVal [v] = \atts -> VAL (getAtts getAVAL atts v) nomatchList [e1,e2] = \atts -> if ((getAtts getList atts e1) == (getAtts getList atts e2)) then (Kill False) else (Kill True) ---- TESTING main = aTest aTest = meaning_of start Start "a a a a a a" --------------- SAMPLE GRAMMAR1 FOR Natural Language Disambiguation with synthesized attributes ver 1 ---------------- s = memoize TA (parser (nt np S0) [] ) np = memoize TB (parser (nt np S0 *> nt pp S1 ) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S0] ,rule_s Kill OF LHS EQ nomatch [synthesized VAL OF S0, synthesized VAL OF S1] ] <|> parser (nt det S2 *> nt noun S3 ) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S3] ] <|> parser (nt noun S4) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S4] ] <|> parser (nt np S4 *> nt vp S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S4] ] ) vp = memoize TC (parser (nt verb S0 *> nt np S1 ) [] ) pp = -- memoize TD (parser (nt prep S0 *> nt np S1 ) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1]] ) noun = memoize As (terminal (term "bob") [VAL 0] <|> terminal (term "bird") [VAL 1] <|> terminal (term "telescope") [VAL 0]) det = memoize Bs (terminal (term "a") [VAL 9] ) verb = memoize Cs (terminal (term "saw") [VAL 9] ) prep = memoize Ds (terminal (term "with") [VAL 9] ) ---- semantic functions ---- addToaList [a,v] = \atts -> List ((getAtts getAVAL atts a):(getAtts getList atts v)) formaList [v] = \atts -> List ([(getAtts getAVAL atts v)]) assignVal [v] = \atts -> v incVal [v] = \atts -> VAL ((getAtts getAVAL atts v ) + 1) incMVal [v] = \atts -> MaxVal ((getAtts getAVAL atts v ) + 1) copyVal [v] = \atts -> VAL (getAtts getAVAL atts v) nomatchList [e1,e2] = \atts -> if ((getAtts getList atts e1) == (getAtts getList atts e2)) then (Kill False) else (Kill True) ---- TESTING main = aTest aTest = meaning_of s TA "bob saw a bird with a telescope" --------------- SAMPLE GRAMMAR1 FOR Natural Language Disambiguation with synthesized attributes ver 2 ---------------- s = memoize TA (parser (nt np S0) [] ) np = memoize TB (parser (nt np S0 *> nt pp S1 ) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ,rule_s Kill OF LHS EQ nomatch [synthesized VAL OF S0, synthesized VAL OF S1] ] <|> parser (nt det S2 *> nt noun S3 ) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S3] ] <|> parser (nt noun S4) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S4] ] <|> parser (nt np S4 *> nt rp S3 *> nt vp S1) [rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S4] ] ) vp = memoize TC (parser (nt verb S0 *> nt np S1 ) [] ) pp = memoize TD (parser (nt prep S0 *> nt np S1 ) [ rule_s VAL OF LHS EQ copyVal [synthesized VAL OF S1] ] ) rp = memoize Op (parser (nt relpro S0 *> nt vp S1 ) [-- rule_i MaxVal OF S0 EQ copyVal [inherited MaxVal OF LHS] -- ,rule_s VAL OF LHS EQ copyVal [inherited MaxVal OF S0{-,synthesized VAL OF S1-}] ] ) noun = memoize As (terminal (term "bob") [VAL 0] <|> terminal (term "bird") [VAL 1] <|> terminal (term "telescope") [VAL 0] <|> terminal (term "birding") [VAL 9]) det = memoize Bs (terminal (term "a") [VAL 9] ) verb = memoize Cs (terminal (term "saw") [VAL 9] <|> terminal (term "likes") [VAL 9] ) prep = memoize Ds (terminal (term "with") [VAL 9] ) relpro = memoize Fs (terminal (term "who") [VAL 9] ) ---- semantic functions ---- addToaList [a,v] = \atts -> List ((getAtts getAVAL atts a):(getAtts getList atts v)) formaList [v] = \atts -> List ([(getAtts getAVAL atts v)]) assignVal [v] = \atts -> v incVal [v] = \atts -> VAL ((getAtts getAVAL atts v ) + 1) incMVal [v] = \atts -> MaxVal ((getAtts getAVAL atts v ) + 1) copyVal [v] = \atts -> VAL (getAtts getAVAL atts v) nomatchList [e1,e2] = \atts -> if ((getAtts getList atts e1) == (getAtts getList atts e2)) then (Kill False) else (Kill True) ---- TESTING main = aTest aTest = meaning_of s TA "bob who likes birding saw a bird with a telescope"