English 中文(简体)
争取道歉
原标题:String tokenization in prolog

我有以下背景,在文稿中可以免费打克。 txt

S ::= a S b
S ::= []

I m opening this file and able to read each line in prolog. Now i want to tokenize each line and generate a list such as

L=[[ S , ::= , a , S , b ],[ S , ::= , # ]]  ( #  represents empty)

如何做到这一点?

最佳回答

撰写《发展中国家概况》。 我给你基本的(未测试的),你需要加以完善。

parse_grammar([Rule|Rules]) -->
 parse_rule(Rule),
 parse_grammar(Rules).
parse_grammar([]) --> [].

parse_rule([NT,  ::=  | Body]) -->
  parse_symbol(NT),
  skip_space,
  "::=",
  skip_space,
  parse_symbols(Body),
  skip_space, !.  % the cut is required if you use findall/3 (see below)

parse_symbols([S|Rest]) -->
  parse_symbol(S),
  skip_space,
  parse_symbols(Rest).
parse_symbols([]) --> [].

parse_symbol(S) -->
  [C], {code_type(C, alpha), atom_codes(S, [C])}.

skip_space -->
  [C], {code_type(C, space)}, skip_space.
skip_space --> [].

利用这一顶级:

  ...,
  read_file_to_codes( grammar.txt , Codes),
  phrase(parse_grammar(Grammar), Codes, [])).

你说,你当时读到档案1行:然后使用

  ...
  findall(R, (get_line(L), phrase(parse_rule(R), L, [])), Grammar).

HTH

问题回答

暂无回答




相关问题
Split Strings and arrange db to display products in PHP

I m new in php. Could you please help me to find the way to properly arrange following task: Table "Products" id - details 1 - 1-30,2-134:6:0;;2-7:55:0;;1-2,2-8:25:0 - where this string can be ...

Lucene Query WITHOUT Operators

I am trying to use Lucene to search for names in a database. However, some of the names contain words like "NOT" and "OR" and even "-" minus symbols. I still want the different tokens inside the names ...

Google-like search query tokenization & string splitting

I m looking to tokenize a search query similar to how Google does it. For instance, if I have the following search query: the quick "brown fox" jumps over the "lazy dog" I would like to have a ...

Recursive woes - reducing an input string

I m working on a portion of code that is essentially trying to reduce a list of strings down to a single string recursively. I have an internal database built up of matching string arrays of varying ...

Tokenize from a textfile reading into an array in C

How do you tokenize when you read from a file in C? textfile: PES 2009;Konami;DVD 3;500.25; 6 Assasins Creed;Ubisoft;DVD;598.25; 3 Inferno;EA;DVD 2;650.25; 7 char *tokenPtr; fileT = fopen("DATA2....

tokenize a string keeping delimiters in Python

Is there any equivalent to str.split in Python that also returns the delimiters? I need to preserve the whitespace layout for my output after processing some of the tokens. Example: >>> s="...

C tokenize polynomial coefficients

I m trying to put the coefficients of polynomials from a char array into an int array I have this: char string[] = "-4x^0 + x^1 + 4x^3 - 3x^4"; and can tokenize it by the space into -4x^0 x^1 4x^3 ...

Approaching Text Parsing in Scala

I m making an application that will parse commands in Scala. An example of a command would be: todo get milk for friday So the plan is to have a pretty smart parser break the line apart and ...

热门标签