Skip to content

Commit 59ae5a4

Browse files
committed
some notes regarding prolog and working with DCG grammars
1 parent 0047911 commit 59ae5a4

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

notes.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# DCG / Parsing notes
2+
3+
These are various notes and snippets from several sources. Such as clocksin/mellish, tokenize swi-prolog package, dcg_tut by anne hogdon and amzi prolog tutorial.
4+
5+
##
6+
7+
DCGs or what they are converted to are basically difference lists such as `lex([lex, fsdf, sd,"."]-X).` DCGs are a nicer/better notation for that.
8+
9+
10+
11+
## Alternatives in DCGS
12+
13+
* The `;` operator allows alternatives, kind of like a switch statement(?)
14+
* `{Var = "somevalue"}` to unify to a certain value if not unified by some other predicate
15+
* `optional(:Match, :Default)//`` https://www.swi-prolog.org/pldoc/man?predicate=optional//2
16+
17+
## Conversion between a predicate with arguments to a list (or reverse)
18+
19+
`=..` univ - converts predicate and args to list or reverse (list to predicate and args)
20+
21+
## Lookahead
22+
23+
`look_ahead(T), [T] --> [T].`
24+
`phrase(look_ahead(T), [a], Rest).`
25+
26+
This is actually first removing it then putitng it back.
27+
28+
29+
## Trees
30+
31+
* How to map to tree?
32+
* Recursion but then - probably an extra state or variable than carries tree state... or just look at examples from Anne.
33+
* How to build parse tree https://cs.union.edu/~striegnk/learn-prolog-now/html/node67.html
34+
35+
### Sorted tree dictionary
36+
37+
* chpt 7 clocksin/mellish
38+
* Examples of trees?
39+
40+
## For formatting output
41+
42+
`try_literals(X) :- phrase(cliche, X) ,format('~s~n', [X]).`
43+
44+
45+
46+
## Helper
47+
48+
* `atomic(X).` test of atom or number
49+
50+
## For lists
51+
52+
* `efface` - removes first occurence
53+
* `delete` - removes all occurences
54+
* `last()` - last list element
55+
* first?
56+
* `nextto(X, Y, L)` (X and Y are consecutive elements in L)
57+
* findall
58+
59+
60+
## Reading and writing files
61+
62+
* `read_file_to_codes(File, Codes, [encoding(utf8)]),`
63+
* `open(Filename, read, Output).`
64+
* `close(Output).`
65+
66+
## One or more of some
67+
68+
names ---> [name, conj, names].
69+
names ---> [name].
70+
names ---> [].
71+
72+
##
73+
74+
* Unify for one additional Var - `noun(thing,X) --> [X], {location(X,_)}.`
75+
76+
N to count terms
77+
AST to add nodes ?
78+
79+
wordlist([X|Y]) --> word(X), whitespace, wordlist(Y).
80+
wordlist([X]) --> whitespace, wordlist(X).
81+
wordlist([X]) --> word(X).
82+
wordlist([X]) --> word(X), whitespace.
83+
84+
word(W) --> charlist(X), {name(W,X)}.
85+
86+
charlist([X|Y]) --> chr(X), charlist(Y).
87+
charlist([X]) --> chr(X).
88+
89+
chr(X) --> [X],{X>=48}.

0 commit comments

Comments
 (0)