Friday, 15 August 2014

dcg - Prolog Roman Numerals (Attribute Grammars) -



dcg - Prolog Roman Numerals (Attribute Grammars) -

i working on assignment in prolog scans list of numerals , should homecoming whether list valid roman numeral , decimal value of numerals. ex)

1 ?- roman(n, ['i'], []). n = 1 true. 2 ?-

when run programme sense should work, decimal value right, i'm guessing got synthesized attributes part right, returns false numeral lists should homecoming true. i'd add together aborts when supposed if more 3 is, xs, or cs present.

1 ?- roman(n, ['i'], []). n = 1 ; false. 2 ?- roman(n, ['i','i','i','i'], []). error: many i's % execution aborted 3 ?-

when take out n , throw in {write('n = '), write(n)}, works fine , returns true.

1 ?- roman(['i'], []). n = 1 true.

when remove {n valh + valt + valu} returns true, however, no longer displays decimal value. here top line of code (because current assignment, prefer show little necessary answer):

roman(n) --> hundreds(valh), tens(valt), units(valu), {n valh + valt + valu}.

why returning false n, true without, , how prepare it?

assignment: the next bnf specification defines language of roman numerals less 1000:

<roman> ::= <hundreds> <tens> <units> <hundreds> ::= <low hundreds> | cd | d <low hundreds> | cm <low hundreds> ::= e | <low hundreds> c <tens> ::= <low tens> | xl | l <low tens> | xc <low tens> ::= e | <low tens> x <units> ::= <low units> | iv | v <low units> | ix <low units> ::= e | <low units>

define attributes grammar carry out 2 tasks:

a) restrict number of x’s in <low tens>, i’s in <low units>, , c’s in <low hundreds> no more three.

b) provide attribute <roman> gives decimal value of roman numeral beingness defined.

define other attributes needed these tasks, not alter bnf grammar.

did noticed grammar formed of same pattern (group//5) repeated 3 times, different symbols ? compactness...

roman(n) --> group('c','d','m',100, h), group('x','l','c',10, t), group('i','v','x',1, u), {n h+t+u}. group(a,b,c, scale, value) --> ( g3(a, t) ; [a, b], {t = 4} % daniel , catching bugs ; [b], g3(a, f), {t 5+f} ; [b], {t 5} ; [a, c], {t = 9} ; {t = 0} ), {value scale * t}. g3(c, 1) --> [c]. g3(c, 2) --> [c,c]. g3(c, 3) --> [c,c,c].

some test

?- atom_chars('cmxxx',l), phrase(roman(n),l). l = ['c', 'm', 'x', 'x', 'x'], n = 930 ; false. ?- atom_chars('cmxlviii',l), phrase(roman(n),l). l = ['c', 'm', 'x', 'l', 'v', 'i', 'i', 'i'], n = 943 ; false.

just curiousity, showing dcg @ work...

edit after daniel , comments...

?- atom_chars('viii',l), phrase(roman(n),l). l = ['v', 'i', 'i', 'i'], n = 8 . ?- phrase(roman(x), ['l','i','x']). x = 59 .

prolog dcg roman-numerals

1 comment:

  1. the next bnf specification defines language of roman numerals less 1000:

    ::= ::= | cd | d | cm ::= e | c ::= | xl | l | xc ::= e | x ::= | iv | v | ix ::= e |
    define attributes grammar carry out 2 tasks:

    a) restrict number of x’s in , i’s in , , c’s in no more three.

    b) provide attribute gives decimal value of roman numeral beingness defined.
    what is the solution of this problem.

    ReplyDelete