Prolog - DCG parser with input from file

前端 未结 4 1827
萌比男神i
萌比男神i 2021-02-19 20:11

As part of a project I need to write a parser that can read a file and parse into facts I can use in my program.

The file structure looks as follows:

p         


        
4条回答
  •  花落未央
    2021-02-19 20:50

    I parse the string into a list and then manipulate the list. Using DCG you can convert

    T = (saf>{saf, as13s}>a32s>asf).
    

    to

    S = [saf-0, saf-1, as13s-1, a32s-2, asf-3] .
    

    Note to do:

    1. parseLine(<>,Position) --> parseLine(L,Position), parseLine(R,NewPosition)
    2. parseLine(Item,Pos) --> [Item-Pos].
    

    Here you have 2 patterns to handle those are the (L>R) and the {L,R}. That won't be much complicated and really easy to read.

提交回复
热议问题