Grammar Specification

Formal grammar for the Tenet programming language in EBNF notation.


Notation

  • | — alternation (choice)
  • * — zero or more
  • + — one or more
  • ? — optional (zero or one)
  • ( ) — grouping
  • " " — terminal (literal token)

Program Structure

program        → declaration* EOF ;

Declarations

declaration    → classDecl
               | funDecl
               | varDecl
               | gameDecl
               | statement ;

classDecl      → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;

funDecl        → "fun" function ;

function       → IDENTIFIER "(" parameters? ")" block ;

parameters     → IDENTIFIER ( "," IDENTIFIER )* ;

varDecl        → "var" IDENTIFIER ( "=" expression )? ";" ;

Game Theory Declarations

gameDecl       → "game" IDENTIFIER "{" gameBody "}" ;

gameBody       → playersDecl strategiesDecl payoffDecl* ;

playersDecl    → "players" identifierList ;

strategiesDecl → "strategies" identifierList ;

identifierList → IDENTIFIER ( "," IDENTIFIER )* ;

payoffDecl     → "payoff" IDENTIFIER "{" payoffRule* "}" ;

payoffRule     → "(" identifierList ")" ":" payoffValue ;

payoffValue    → payoffTerm ;

payoffTerm     → payoffFactor ( ( "-" | "+" ) payoffFactor )* ;

payoffFactor   → payoffUnary ( ( "/" | "*" ) payoffUnary )* ;

payoffUnary    → "-" payoffUnary | payoffPrimary ;

payoffPrimary  → NUMBER | IDENTIFIER ;

Statements

statement      → exprStmt
               | forStmt
               | ifStmt
               | printStmt
               | returnStmt
               | whileStmt
               | solveStmt
               | block ;

exprStmt       → expression ";" ;

forStmt        → "for" "(" ( varDecl | exprStmt | ";" )
                           expression? ";"
                           expression? ")" statement ;

ifStmt         → "if" "(" expression ")" statement
                 ( "else" statement )? ;

printStmt      → "print" expression ";" ;

returnStmt     → "return" expression? ";" ;

whileStmt      → "while" "(" expression ")" statement ;

solveStmt      → "solve" IDENTIFIER ( "using" IDENTIFIER )? ";" ;

block          → "{" declaration* "}" ;

Expressions

expression     → assignment ;

assignment     → ( call "." )? IDENTIFIER "=" assignment
               | logic_or ;

logic_or       → logic_and ( "or" logic_and )* ;

logic_and      → equality ( "and" equality )* ;

equality       → comparison ( ( "!=" | "==" ) comparison )* ;

comparison     → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;

term           → factor ( ( "-" | "+" ) factor )* ;

factor         → unary ( ( "/" | "*" ) unary )* ;

unary          → ( "!" | "-" ) unary | call ;

call           → primary ( "(" arguments? ")" | "." IDENTIFIER )* ;

arguments      → expression ( "," expression )* ;

primary        → "true" | "false" | "nil" | "this"
               | NUMBER | STRING | IDENTIFIER
               | "(" expression ")"
               | "super" "." IDENTIFIER ;

Lexical Grammar

NUMBER         → DIGIT+ ( "." DIGIT+ )? ;

STRING         → "\"" <any char except "\"">* "\"" ;

IDENTIFIER     → ALPHA ( ALPHA | DIGIT )* ;

ALPHA          → "a" ... "z" | "A" ... "Z" | "_" ;

DIGIT          → "0" ... "9" ;

Operator Precedence

From lowest to highest:

PrecedenceOperatorsAssociativity
1orLeft
2andLeft
3== !=Left
4< <= > >=Left
5+ -Left
6* /Left
7! - (unary)Right
8. () (call)Left

Comments

COMMENT        → "//" <any char except newline>* NEWLINE ;

Multi-line comments (/* */) are not supported.


Next Steps