| 1 | % (c) 2014-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(smtlib2_parser,[parse_smtlib2/2, get_smtlib_lines_read/1]). | |
| 6 | ||
| 7 | :- use_module(probsrc(module_information),[module_info/2]). | |
| 8 | :- module_info(group,smtlib). | |
| 9 | :- module_info(description,'The SMT-LIB 2 Interpreter - Parser'). | |
| 10 | ||
| 11 | :- use_module(library(lists),[append/2]). | |
| 12 | :- use_module(probsrc(debug),[debug_println/2]). | |
| 13 | :- use_module(probsrc(error_manager),[add_error/3]). | |
| 14 | ||
| 15 | :- set_prolog_flag(double_quotes, codes). | |
| 16 | ||
| 17 | parse_smtlib2(Source, ListOfCommands) :- | |
| 18 | debug_println(5,parsing_smtlib2(Source)), | |
| 19 | reset_parser, | |
| 20 | (phrase(commands(ListOfCommands),Source) | |
| 21 | -> debug_println(5,finished_parsing) | |
| 22 | ; line_counter(Line), | |
| 23 | add_error(smtlib2_parser,'Parsing failed at line:',Line), | |
| 24 | fail | |
| 25 | ). | |
| 26 | ||
| 27 | % meta magic | |
| 28 | % at_least_once consumes rule A at least once and returns a list | |
| 29 | % multiple_times consumes rule A zero or more times | |
| 30 | ||
| 31 | :- meta_predicate multiple_times(3,-,-,-). | |
| 32 | :- meta_predicate at_least_once(3,-,-,-). | |
| 33 | at_least_once(A,[Partial|List]) --> | |
| 34 | call(A,Partial), ws, | |
| 35 | !, | |
| 36 | multiple_times(A,List). | |
| 37 | at_least_once(A,_) --> err_message(A). | |
| 38 | ||
| 39 | err_message(Expected) --> err_message('Unrecognized',Expected). | |
| 40 | err_message(Kind,Expected) --> {line_counter(N), format_with_colour_nl(user_error,[red],"! ~w ~w on line ~w.",[Kind,Expected,N]),fail}. | |
| 41 | err_message(_,_) --> any_chars_but_newline(S), !, {format_with_colour_nl(user_error,[red],"! '~s'",[S]),fail}. | |
| 42 | ||
| 43 | multiple_times(A,[Partial|List]) --> | |
| 44 | call(A,Partial), ws, | |
| 45 | multiple_times(A,List). | |
| 46 | multiple_times(_A,[],In,In). | |
| 47 | ||
| 48 | ||
| 49 | :- use_module(probsrc(tools_printing),[format_with_colour_nl/4]). | |
| 50 | ||
| 51 | % grammar rules | |
| 52 | commands(Cmds) --> real_ws, !, commands(Cmds). | |
| 53 | commands(Commands) --> ";", any_chars_but_newline(_), newline, | |
| 54 | {debug_println(19,comment_line)},!, | |
| 55 | commands(Commands). | |
| 56 | commands([Command|Commands]) --> | |
| 57 | command(Command), {debug_println(19,command(Command))}, !, | |
| 58 | commands(Commands). | |
| 59 | commands([]) --> !. | |
| 60 | commands(_) --> err_message(command). | |
| 61 | ||
| 62 | reset_parser :- retractall(line_counter(_)), assertz(line_counter(1)). | |
| 63 | :- dynamic line_counter/1. | |
| 64 | line_counter(1). | |
| 65 | get_smtlib_lines_read(Nr) :- (line_counter(N) -> Nr is N-1 ; Nr = 0). | |
| 66 | ||
| 67 | inc_line_counter :- | |
| 68 | retract(line_counter(N)), debug_println(5,processed_line(N)), | |
| 69 | N1 is N+1, assertz(line_counter(N1)). | |
| 70 | ||
| 71 | newline --> [10],!, {inc_line_counter}. | |
| 72 | newline --> [13],!, {inc_line_counter}. | |
| 73 | ||
| 74 | command(Command) --> "(", ws, !, command1(Command). | |
| 75 | command(exit) --> ":x",!. % for ProB REPL compatibility | |
| 76 | ||
| 77 | ||
| 78 | command1(set_logic(Logic)) --> "set-logic", !, ws, symbol(Logic), close_paren(set_logic). | |
| 79 | command1(set_option(Option)) --> "set-option", !, ws, attribute(Option), close_paren(set_option). | |
| 80 | command1(set_info(Info)) --> "set-info", !, ws, attribute(Info), close_paren(set_info(Info)). | |
| 81 | command1(set_info(Info)) --> "meta-info", !, ws, attribute(Info), close_paren(meta_info(Info)). | |
| 82 | command1(reset) --> "reset", !, close_paren(reset). | |
| 83 | command1(declare_sort(Name,Arity)) --> "declare-sort", !, ws, | |
| 84 | symbol(Name), ws, numeral(Arity), close_paren(declare_sort). | |
| 85 | command1(define_sort(Name,Parameters,Sort)) --> "define-sort", !, ws, | |
| 86 | symbol(Name), | |
| 87 | open_paren(define_sort_params(Name)), | |
| 88 | multiple_times(symbol,(Parameters)),close_paren(define_sort_params(Name)), ws, | |
| 89 | must_be_smt_sort(Sort), close_paren(define_sort). | |
| 90 | command1(declare_fun(Name,Parameters,Result)) --> "declare-fun", ws, !, | |
| 91 | must_be_symbol(Name), | |
| 92 | open_paren(declare_fun_params(Name)), | |
| 93 | multiple_times(smt_sort,Parameters), ws, close_paren(declare_fun_params(Name)), ws, | |
| 94 | must_be_smt_sort(Result), close_paren(declare_fun). | |
| 95 | command1(declare_fun(Name,[],Result)) --> "declare-const", ws, !, | |
| 96 | must_be_symbol(Name), ws, | |
| 97 | must_be_smt_sort(Result), close_paren(declare_fun). | |
| 98 | command1(define_fun(Name,Parameters,Result,Term)) --> "define-fun", ws, !, | |
| 99 | must_be_symbol(Name), | |
| 100 | open_paren(define_fun_params(Name)), | |
| 101 | multiple_times(sorted_var,Parameters), close_paren(define_fun_params(Name)), ws, | |
| 102 | must_be_smt_sort(Result), ws, must_be_term(Term), close_paren(define_fun). | |
| 103 | command1(push(Num)) --> "push", !, ws, numeral(Num), close_paren(push). | |
| 104 | command1(pop(Num)) --> "pop", !, ws, numeral(Num), close_paren(pop). | |
| 105 | command1(assert(Term)) --> "assert", ws, !, | |
| 106 | must_be_term(Term), close_paren(assert). | |
| 107 | command1(check_sat) --> "check-sat", !, close_paren(check_sat). | |
| 108 | command1(get_assert) --> "get-assert", !, close_paren(get_assert). | |
| 109 | command1(get_proof) --> "get-proof", !, close_paren(get_proof). | |
| 110 | command1(get_model) --> "get-model", !, close_paren(get_model). | |
| 111 | command1(get_unsat_core) --> "get-unsat-core", !, close_paren(get_unsat_core). | |
| 112 | command1(get_value(Terms)) --> "get-value", !, open_paren(get_value_arguments), at_least_once(term,Terms), | |
| 113 | close_paren(get_value_arguments), close_paren(get_value). | |
| 114 | command1(get_assign) --> "get-assign", !, close_paren(get_assign). | |
| 115 | command1(get_option(Name)) --> "get-option", ws, !, keyword(Name), close_paren(get_option). | |
| 116 | command1(get_info(Name)) --> "get-info", ws, !, keyword(Name), close_paren(get_info). | |
| 117 | command1(exit) --> "exit", !, close_paren(exit). | |
| 118 | command1(_) --> err_message(command). | |
| 119 | ||
| 120 | close_paren(Ctxt) --> real_ws, !, close_paren(Ctxt). | |
| 121 | close_paren(_) --> ")",!. | |
| 122 | close_paren(Ctxt) --> err_message('Missing',closing_parenthesis(Ctxt)). | |
| 123 | ||
| 124 | open_paren(Ctxt) --> real_ws, !, open_paren(Ctxt). | |
| 125 | open_paren(_) --> "(",!. | |
| 126 | open_paren(Ctxt) --> err_message('Missing',opening_parenthesis(Ctxt)). | |
| 127 | ||
| 128 | %command(_,In,_) :- !, atom_codes(A,In), format('Parsing failed on: ~w~n',[A]), !, fail. | |
| 129 | ||
| 130 | symbol(Symbol) --> (simplesymbol(Symbol) -> [] ; asciicomment(Symbol)). | |
| 131 | simplesymbol(Symbol) --> single_symbol(Start), symbols_or_digits(Rest), {atom_codes(Symbol,[Start|Rest])}. | |
| 132 | single_symbol(S) --> [S], {member(S,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-/*=%?!.$_~&^<>@")}. | |
| 133 | single_symbol_or_digit(S) --> single_symbol(S) ; digit(S). | |
| 134 | symbols_or_digits([H|T]) --> single_symbol_or_digit(H), symbols_or_digits(T). | |
| 135 | symbols_or_digits([]) --> "". | |
| 136 | ||
| 137 | asciicomment(string(Symbol)) --> "|", any_chars_but_pipe(Codes), "|", {atom_codes(Symbol,Codes)}. | |
| 138 | ||
| 139 | any_chars_but_pipe([C|T]) --> [C], {C \= 124}, any_chars_but_pipe(T). | |
| 140 | any_chars_but_pipe([]) --> "". | |
| 141 | ||
| 142 | any_chars_but_newline([C|T]) --> [C], {C \= 10}, any_chars_but_newline(T). | |
| 143 | any_chars_but_newline([]) --> "". | |
| 144 | ||
| 145 | attribute(Attr) --> keyword(A), attribute_aux(Attr,A). | |
| 146 | ||
| 147 | attribute_aux(attribute(A,Value),A) --> real_ws, attribute_value(Value),!. | |
| 148 | attribute_aux(attribute(A),A) --> []. | |
| 149 | ||
| 150 | attribute_value(Value) --> symbol(Value). | |
| 151 | attribute_value(Value) --> spec_constant(Value). | |
| 152 | attribute_value(Value) --> "(", at_least_once(sexpr,Value), ")". | |
| 153 | ||
| 154 | signed_numeral(N) --> "-",!, numeral(N1), {N is -N1}. | |
| 155 | signed_numeral(N) --> numeral(N). | |
| 156 | ||
| 157 | numeral(0) --> "0". | |
| 158 | numeral(N) --> digit_no_zero(D1), digits(D), {number_codes(N,[D1|D])}. | |
| 159 | ||
| 160 | digits1([D|T]) --> digit(D),!,digits(T). | |
| 161 | digits([D|T]) --> digit(D),!,digits(T). | |
| 162 | digits([]) --> "". | |
| 163 | hex_digits(Val) --> hex_digits(0,Val). | |
| 164 | hex_digits(ValIn,ValOut) --> hex_digit(_D,ValD),!,{V1 is ValIn*16+ValD}, hex_digits(V1,ValOut). | |
| 165 | hex_digits(Val,Val) --> "". | |
| 166 | ||
| 167 | % hex digits after a point | |
| 168 | hex_digits_after(Val) --> hex_digit(_D,ValD),!, hex_digits_after(ValT), {Val is ValT/16+ValD}. | |
| 169 | hex_digits_after(0.0) --> "". | |
| 170 | ||
| 171 | signed_decimal_numeral(D) --> "-", !, decimal_numeral(D1), {D is -D1}. | |
| 172 | signed_decimal_numeral(N) --> decimal_numeral(N). | |
| 173 | decimal_numeral(DResult) --> numeral(D1), ".", !, digits1(DC2), decimal_exponent(DE), | |
| 174 | {number_codes(D1,DC1), | |
| 175 | append([DC1,".",DC2,DE],Codes), | |
| 176 | number_codes(DResult,Codes)}. | |
| 177 | decimal_numeral(DResult) --> ("0x" ; "+0x"),hex_digits(Val1), hex_numeral_rest1(Val1,DResult). | |
| 178 | ||
| 179 | decimal_exponent([0'e,S|D]) --> ("e" ; "E"), exp_sign(S), !,digits1(D). | |
| 180 | decimal_exponent([]) --> "". | |
| 181 | exp_sign(D) --> [D], {D = 0'- ; D=0'+},!. | |
| 182 | exp_sign(0'+) --> "". | |
| 183 | ||
| 184 | hex_numeral_rest1(V1,DResult) --> ".",hex_digits_after(V2), !, {V is V1+V2},hex_numeral_rest2(V,DResult). | |
| 185 | hex_numeral_rest1(V1,DResult) --> hex_numeral_rest2(V1,DResult). | |
| 186 | hex_numeral_rest2(V,DResult) --> "p+",digits1(ExpS),!, | |
| 187 | {number_codes(Exp,ExpS), DResult is V*2^Exp}. % TODO: process hexadecimal literal | |
| 188 | hex_numeral_rest2(V,V) --> "". | |
| 189 | ||
| 190 | digit(D) --> [D], {D >= 48, D =< 57}. | |
| 191 | digit_no_zero(D) --> [D], {D >= 49, D =< 57}. | |
| 192 | hex_digit(D,Val) --> [D], {D >= 48, (D =< 57, Val is D-38 ; D >= 65, D =< 70, Val is D-65)}. | |
| 193 | ||
| 194 | smt_sort(sort(S)) --> identifier(S). | |
| 195 | smt_sort(sort(S)) --> numeral(S). % not yet supported in Translation, what does it mean? | |
| 196 | smt_sort(sort('BitVec',[Length])) --> "(_ BitVec ", numeral(Length), ")". | |
| 197 | smt_sort(sort(S,Params)) --> "(", identifier(S), ws, !, | |
| 198 | at_least_once(smt_sort,Params), close_paren(sort). | |
| 199 | % like smt_sort, but throw error message: | |
| 200 | must_be_smt_sort(T) --> smt_sort(T),!. | |
| 201 | must_be_smt_sort(_) --> err_message(sort). | |
| 202 | ||
| 203 | sorted_var(svar(Name,Sort)) --> "(", symbol(Name), ws, smt_sort(Sort), ")". | |
| 204 | ||
| 205 | term(let(Bindings,Term)) --> | |
| 206 | "(let", ws, !, "(", at_least_once(binding,Bindings), ")", ws, !, must_be_term(Term), !, close_paren(let). | |
| 207 | term(forall(Vars,Term)) --> | |
| 208 | "(forall", ws, !, "(", at_least_once(sorted_var,Vars), ")", ws, !, must_be_term(Term), !, close_paren(forall). | |
| 209 | term(exists(Vars,Term)) --> | |
| 210 | "(exists", ws, !, "(", at_least_once(sorted_var,Vars), ")", ws, !, must_be_term(Term), !, close_paren(exists). | |
| 211 | term(T) --> spec_constant(T),!. | |
| 212 | term(T) --> qual_identifier(T),!. | |
| 213 | term(attributed_term(Term,Attributes)) --> "(", "!", ws, !, | |
| 214 | must_be_term(Term), ws, !, | |
| 215 | at_least_once(attribute,Attributes), close_paren(attributed_term). | |
| 216 | term(id_term(Id,Terms)) --> "(", ws, qual_identifier(Id), ws, !, at_least_once(term,Terms), close_paren(id_term). | |
| 217 | ||
| 218 | % term which raises error if it cannot be found | |
| 219 | must_be_term(T) --> term(T),!. | |
| 220 | must_be_term(_) --> err_message(term). | |
| 221 | ||
| 222 | binding(bind(Symbol,Term)) --> "(", must_be_symbol(Symbol), ws, !, must_be_term(Term), close_paren(bind). | |
| 223 | % binding which raises error if it cannot be found | |
| 224 | must_be_symbol(T) --> symbol(T),!. | |
| 225 | must_be_symbol(_) --> err_message(symbol). | |
| 226 | ||
| 227 | qual_identifier(id(Id)) --> identifier(Id). | |
| 228 | qual_identifier(id(Id,Sort)) --> "(as ", ws, !, identifier(Id), ws, smt_sort(Sort), ")". | |
| 229 | qual_identifier(id(sign_extend(Parameter))) --> "(_ sign_extend ", !, numeral(Parameter), ")". | |
| 230 | qual_identifier(id(zero_extend(Parameter))) --> "(_ zero_extend ", !, numeral(Parameter), ")". | |
| 231 | qual_identifier(id(extract(P1,P2))) --> "(_ extract ", !, numeral(P1), ws, numeral(P2), ")". | |
| 232 | ||
| 233 | ||
| 234 | identifier(Id) --> symbol(Id). | |
| 235 | %identifier(id_num(Id,Num)) --> "(", "_", symbol(Id), numeral(Num), ")". | |
| 236 | ||
| 237 | keyword(KW) --> ":", symbols_or_digits(L), {atom_codes(KW,L)}. | |
| 238 | ||
| 239 | sexpr(E) --> spec_constant(E). | |
| 240 | sexpr(E) --> symbol(E). | |
| 241 | sexpr(E) --> keyword(E). | |
| 242 | sexpr(E) --> "(", multiple_times(sexpr,E), ")". | |
| 243 | ||
| 244 | spec_constant(decimal(D)) --> signed_decimal_numeral(D). | |
| 245 | spec_constant(integer(I)) --> signed_numeral(I). | |
| 246 | spec_constant(string(S)) --> "\"", any_chars_but_quotations(SCodes), "\"", {atom_codes(S,SCodes)}. | |
| 247 | spec_constant(bitvector(Val,Length)) --> "(_ bv", numeral(Val), " ", numeral(Length), ")". | |
| 248 | spec_constant(bitvector(Binary)) --> "#b", binary_numbers(Binary). | |
| 249 | ||
| 250 | binary_numbers([H|T]) --> binary_number(H), binary_numbers(T). | |
| 251 | binary_numbers([N]) --> binary_number(N). | |
| 252 | binary_number(0) --> "0". | |
| 253 | binary_number(1) --> "1". | |
| 254 | ||
| 255 | any_chars_but_quotations([C|T]) --> [C], {C \= 34}, any_chars_but_quotations(T). | |
| 256 | % two quotations are allowed in smtlib 2.5 | |
| 257 | any_chars_but_quotations([C,C|T]) --> [C,C], {C = 34}, any_chars_but_quotations(T). | |
| 258 | any_chars_but_quotations([]) --> "". | |
| 259 | ||
| 260 | % whitespaces | |
| 261 | ws --> " ", !, ws. | |
| 262 | ws --> [9], !, ws. % tab | |
| 263 | ws --> newline, !, ws. | |
| 264 | ws --> "". | |
| 265 | ||
| 266 | real_ws --> " ", !, ws. | |
| 267 | real_ws --> [9], !, ws. | |
| 268 | real_ws --> newline, ws. |