Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the term grammar #25

Open
rkaminsk opened this issue Oct 6, 2020 · 1 comment
Open

update the term grammar #25

rkaminsk opened this issue Oct 6, 2020 · 1 comment

Comments

@rkaminsk
Copy link
Member

rkaminsk commented Oct 6, 2020

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

  • identifiers and variables prefixed with 's,
  • binary and unary operators,
  • term pools, and
  • external functions.
@rkaminsk
Copy link
Member Author

rkaminsk commented Mar 25, 2022

/******************** 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)*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant