0

I am trying to write a rule in order to represent 0 or more using recursion: The rule in EBNF is the following:

translation_unit 
        => external_declaration  { external_declaration}

My guess is the following:

translation_unit(Xs0,Xs) :-                         
         [external_declaration|Xs]. 

Also I would like to represent optionality in another rule which is the following:

declaration_specifiers_next
  a.    : ε
  b.    | declaration_specifiers

My guess would be:

  declaration_specifiers_next(Xs0, Xs)
        :- ε,
           [declaration_specifiers|Xs]

I am also confused about the argument that we give to the title of the rule in that case: (Xs0, Xs) What do they actually mean?

false
  • 10,264
  • 13
  • 101
  • 209
user1680944
  • 537
  • 2
  • 13
  • 23
  • 1
    What do mean by "My guess would be"? Are you trying to create a program by typing symbols at random? How do you want to implement `translation_unit(Xs0, Xs)` if you haven't even defined what the meaning of `(Xs0, Xs)` is? It's your program, if you don't know what you want it to do, how is anyone else supposed to know that? – Andrey Tyukin May 03 '15 at 10:51
  • `ε, [declaration_specifiers|Xs]` isn't right since `,` represents an conjunction in Prolog, not disjunction that your BNF suggests. Also, a list by itself `[declaration_specifiers|Xs]` isn't a valid Prolog query. Are you familiar with the basics of Prolog? You probably want to study it a bit rather than guess. ;) For something like this, a DCG would be ideal: `declaration_specifiers_next --> [] | declaration_specifiers.` and `declaration_specifiers -->...`. – lurker May 03 '15 at 10:51

1 Answers1

1

The body of your first clause is a list and this is taken by Prolog systems as being a call to the predefined predicate consult used to open files and loading their contents as programs. This is not what you want...

The head of the clause seems to indicate you want to have a predicate for each non-terminal (class name) of the EBNF. If that is the case you probably want the predicate to have a clause for each possible alternative, you should convert your EBNF rule to context-free grammar rules like

translation_unit -> ε
translation_unit -> external_declaration translation_unit

and then write appropriate clauses in Prolog for your predicate.

In order to understand what is confusing you, you must first explain what you mean by rule. Is it a Definite-Clause Grammar rule? Being a normal clause, that depends on what information you need to pass to, and return from the call to the predicate: you must know that to be able to write the program.

migfilg
  • 542
  • 2
  • 9