potassco / guide

🦮 An introduction to our Answer Set Programming tools focusing on gringo, clingo, and clasp.
Creative Commons Attribution Share Alike 4.0 International
75 stars 13 forks source link

update the term grammar #25

Open rkaminsk opened 3 years ago

rkaminsk commented 3 years ago

The term grammar in the guide does not completely capture the syntax of terms in gringo. It should be updated to include

rkaminsk commented 2 years ago
/******************** CLINGO GRAMMAR **********************/

/* Variable, Identifier, Number, String, and Operator are
   tokens. */
Variable ::= [_']* [A-Z] [a-zA-Z0-9_']*
/* Identifier does not match the string 'not' */
Identifier ::= [_']* [a-z] [a-zA-Z0-9_']*
Number ::= '0' | [1-9] [0-9]*
String ::= '"' ([^\\"] | '\"' | '\\' | '\n')* '"'
/* Operator matches until whitespace, a comment, or the end
   of input. It does not match ',', ';', ':', or '.'. */
Operator ::= [/<=>+\-*\\?&@|:;~^.!]+ | 'not'

/************************ TERMS ***************************/

Term ::= DotTerm

/* terms with precedence and associativity */
DotTerm ::= XorTerm  | DotTerm  '..'              XorTerm
XorTerm ::= OrTerm   | XorTerm  '^'               OrTerm
OrTerm  ::= AndTerm  | OrTerm   '?'               AndTerm
AndTerm ::= SumTerm  | AndTerm  '&'               SumTerm
SumTerm ::= MulTerm  | SumTerm  ('+' | '-')       MulTerm
MulTerm ::= ExpTerm  | MulTerm  ('*' | '/' | '\') ExpTerm
ExpTerm ::= RootTerm | RootTerm '**'              ExpTerm

RootTerm ::= '#sup' | '#supremum'
           | '#inf' | '#infimum'
           | Number                             /* number */
           | String                             /* string */
           | Variable                         /* variable */
           | '_'                    /* anonymous variable */
           | TupleTerm                           /* tuple */
           | '@'? Identifier Pool?            /* function */
           | ('-' | '~') RootTerm     /* unary operations */
           | '|' Term (';' Term)* '|'         /* absolute */

Tuple ::= Term (',' Term)*
Pool ::= '(' Tuple? (';' Tuple?)* ')'
TupleTerm ::= '(' Tuple? ','? (';' Tuple? ','?)* ')'

/************************* ATOMS **************************/

Relation ::= '<' | '<=' | '>' | '>=' | '=' | '==' | '!='
SymAtom ::= '-'? Identifier Pool?
Atom ::= '#true' | '#false'
       | SymAtom
       | Term Relation Term

/*********************** LITERALS *************************/

Sign ::= ('not' 'not'?)?
Literal ::= Sign Atom
Condition ::= Literal (',' Literal)*
CondLiteral ::= Literal (':' Condition?)?

/********************* THEORY ATOMS ***********************/

Operators ::= Operator Operator*
TheoryTuple ::= TheoryTerm (',' TheoryTerm)*
TheoryRoot ::= '(' TheoryTuple? ','? ')'
             | '{' TheoryTuple? '}'
             | '[' TheoryTuple? ']'
             | Identifier ('(' TheoryTuple? ')')?
             | '#sup' | '#supremum' | '#inf' | '#infimum'
             | Number | String | Variable | '_'
TheoryTerm ::= Operators? TheoryRoot (Operators TheoryRoot)*

Name ::= Identifier Pool?
Guard ::= Operator TheoryTerm
TheoryElem ::= TheoryTuple (':' Condition?)?
          | ':' Condition?
TheoryElems ::= TheoryElem (';' TheoryElem)*
TheoryAtom ::= '&' Name '{' TheoryElems? '}' Guard?

/********************** AGGREGATES ************************/

Function ::= '#sum' | '#sum+' | '#count' | '#min' | '#max'
LGuard ::= (Term Relation?)?
RGuard ::= (Relation? Term)?

SetElems ::= CondLiteral (';' CondLiteral)*
SetAggregate ::= LGuard '{' SetElems? '}' RGuard

BodyElem ::= Tuple (':' Condition?)? | ':' Condition?
BodyElems ::= BodyElem (';' BodyElem)*
BodyAggregate ::= LGuard Function '{' BodyElems? '}' RGuard

HeadElem ::= Tuple? ':' Literal (':' Condition?)?
HeadElems ::= HeadElem (';' HeadElem)*
HeadAggregate ::= LGuard Function '{' HeadElems? '}' RGuard

/***************** HEAD AND BODY LITERALS *****************/

/* ambiguities are resolved in favor of the condition */
HeadLiteral ::= CondLiteral ((';' | '|' | ',')
                             CondLiteral)*
              | TheoryAtom
              | SetAggregate
              | HeadAggregate

BodyLiteral ::= CondLiteral
              | Sign (TheoryAtom | SetAggregate |
                      BodyAggregate)

/************************* RULES **************************/

/* ambiguities are resolved in favor of the condition */
Body ::= BodyLiteral ((';' | ',') BodyLiteral)*
Rule ::= HeadLiteral? ':-' Body? '.'
       | HeadLiteral '.'

/**************** OPTIMIZATION STATEMENTS ****************/

OptFunction ::= '#minimize' | '#minimise'
              | '#maximize' | '#maximise'
OptTuple ::= Term ('@' Term)? (',' Term)*
OptElem ::= OptTuple (':' Condition?)?
OptElems ::= OptElem (';' OptElem)*
Optimize ::= ':~' Body? '.' '[' OptTuple ']'
           | OptFunction '{' OptElems? '}' '.'

/******************** SHOW STATEMENTS *********************/

/* amibguities are resolved favoring the first case */
Show ::= '#show' '-'? Identifier '/' Number '.'
       | '#show' Term (':' Body?)? '.'

/******************* DEFINED STATEMENTS *******************/

Defined ::= '#defined' '-'? Identifier '/' Number '.'

/******************** EDGE STATEMENTS *********************/

Pair ::= Term ',' Term
Edge ::= '#edge' '(' Pair (';' Pair)* ')' (':' Body?)? '.'

/****************** HEURISTIC STATEMENTS ******************/

Heuristic ::= '#heuristic' SymAtom (':' Body?)? '.'
              '[' Term ('@' Term)? ',' Term ']'

/***************** PROJECTION STATEMENTS ******************/

Project ::= '#project' '-'? Identifier '/' Number '.'
          | '#project' SymAtom (':' Body?)? '.'

/******************** CONST STATEMENTS ********************/

/* like Term excluding variables, pools, and intervals */
ConstTerm ::= /* ... */
Const ::= '#const' Identifier '=' ConstTerm '.'
          ('[' ('default' | 'override') ']')?

/******************* SCRIPT STATEMENTS ********************/

/* Script is parsed as a token and WS matches an arbitrary
   amount of comments and whitespace. */
Script ::= '#script' WS '(' WS Identifier WS ')'
           ([^#] | '#' [^e] | '#e' [^n] | '#en' [^d])*
           '#end' WS '.'

/****************** INCLUDE STATEMENTS ********************/

Include ::= '#include' String '.'
          | '#include' '<' Identifier '>' '.'

/******************* BLOCK STATEMENTS *********************/

Params ::= Identifier (',' Identifier)?
Block ::= '#program' Identifier ('(' Params? ')')? '.'

/***************** EXTERNAL STATEMENTS ********************/

External ::= '#external' SymAtom (':' Body?)? '.'
             ('[' Term ']')?

/****************** THEORY DEFINITIONS ********************/

OpDef ::= Operator ':' Number ',' 'unary'
        | Operator ':' Number ',' 'binary' ',' 'left'
        | Operator ':' Number ',' 'binary' ',' 'right'
TermDef ::= Identifier '{' (OpDef (';' OpDef)*)? '}'
TermDefs ::= TermDef (';' TermDef)*

GuardRels ::= Operator (',' Operator)*
GuardDef ::= '{' GuardRels? '}' ',' Identifier
AtomType ::= 'head' | 'body' | 'any' | 'directive'
AtomDef ::= Identifier '/' Number ':' Identifier
            (',' GuardDef)? ',' AtomType
AtomDefs ::= AtomDef (';' AtomDef)*

Defs ::= TermDefs ';' AtomDefs
       | TermDefs | AtomDefs
Theory ::= '#theory' '(' Identifier ')' '{' Defs? '}' '.'

/*********************** PROGRAMS *************************/

Program ::= (Rule   | Optimize  | Show    | Defined  |
             Edge   | Heuristic | Project | Const    |
             Script | Include   | Block   | External |
             Theory)*