For this example case I am using DCGs to parse a scientific name. The name is followed by ;
as a delimiter and then the end of the line with \n
. However the name can also contain a ;
and thus when a ;
is found the use of the ;
has to be qualified to determine if it is part of the name or ends the name. Obviously with two characters of lookahead this is easy; if the stream is ;\n
it is a delimiter and if it is not then it is part of the name.
For this I have two different and equally valid predicates that work; they have been tested under real world conditions and work as expected. Also the time difference is almost inconsequential. Now my code has many other predicates for other cases looking for delimiters that have exceptions, so before committing to one way for the many times needed in the code I would like to know if there is a preferred standard used for Prolog?
First:
This one uses one clause with ->/2 and for other parts of the data in the code the conditionals are nested down to 4 levels. See further down for example.
item_code_1(C) -->
(
";\n"
->
{ false }
;
[C]
).
Second
This one uses multiple clauses instead of one clause.
item_code_2(_) -->
";\n",
{ false }, !.
item_code_2(C) -->
[C], !.
Real code with ->
nested 4 levels.
host_name_code(C) -->
(
"."
->
(
"\n"
->
{ false }
;
{ C = 0'. } % '
)
;
(
" "
->
(
"("
->
{ false }
;
{ C = 0'\s } % '
)
;
[C]
)
).