Punkt (pronounced pυηkt) means dot, point or period in German.
Token type |
REGEX |
keyword |
bool call char const else for function if int main print return string var void |
identifier |
[ _ a -z A -Z ] [ _ a -z A -Z 0 -9 ]* |
booleanLiteral |
true false |
characterLiteral |
' <printable-char> ' |
integerLiteral |
[ 0 -9 ]+ |
stringLiteral |
"[ ^ " \n ]*" |
Arithmetic operators |
+ - * / |
Comparison operators |
== != > >= < <= |
Punctuators |
{ } ( ) = . , < > -> |
This is a description of Punkt grammar, simplified slightly for better readability. The grammar that the Punkt parser actually implements is entirely unambiguous.
$$\begin{aligned}
Program \rightarrow & \enspace FunctionDefinition^* \enspace \texttt{main} \enspace CodeBlock \\[16pt]
FunctionDefinition \rightarrow & \enspace \texttt{function} \enspace identifier \enspace Lambda \\[16pt]
Lambda \rightarrow & \enspace \texttt{<} \enspace ParameterList^? \enspace \texttt{>} \enspace \texttt{->} \enspace Type \enspace CodeBlock \\[16pt]
ParameterList \rightarrow & \enspace Type \enspace identifier \enspace (\texttt{,} \enspace Type \enspace identifier)^* \\[16pt]
CodeBlock \rightarrow & \enspace \texttt{\{} \enspace Statement^* \enspace \texttt{\}} \\[16pt]
Statement \rightarrow & \enspace CodeBlock \\\
| & \enspace DeclarationStatement \\\
| & \enspace ExpressionStatement \\\
| & \enspace IfStatement \\\
| & \enspace WhileStatement \\\
| & \enspace ForStatement \\\
| & \enspace ReturnStatement \\\
| & \enspace PrintStatement \\[16pt]
DeclarationStatement \rightarrow & \enspace \texttt{const} \enspace identifier \enspace \texttt{=} \enspace Expression \enspace \texttt{.} \\\
| & \enspace \texttt{var} \enspace identifier \enspace \texttt{=} \enspace Expression \enspace \texttt{.} \\[16pt]
ExpressionStatement \rightarrow & \enspace Expression \enspace \texttt{.} \\[16pt]
IfStatement \rightarrow & \enspace \texttt{if} \enspace Expression \enspace (Statement \enspace | \enspace CodeBlock) \enspace (\texttt{else} \enspace (Statement \enspace | \enspace CodeBlock))^? \\[16pt]
WhileStatement \rightarrow & \enspace \texttt{while} \enspace Expression \enspace (Statement \enspace | \enspace CodeBlock) \\[16pt]
ForStatement \rightarrow & \enspace \texttt{for} \enspace (DeclarationStatement \enspace | \enspace Expression) \enspace \texttt{,} \enspace Expression \enspace \texttt{,} \enspace Expression \enspace CodeBlock \\[16pt]
ReturnStatement \rightarrow & \enspace \texttt{return} \enspace Expression^? \enspace \texttt{.} \\[16pt]
PrintStatement \rightarrow & \enspace \texttt{print} \enspace PrintExpressionList \enspace \texttt{.} \\\
| & \enspace \texttt{println} \enspace PrintExpressionList \enspace \texttt{.} \\[16pt]
PrintExpressionList \rightarrow & \enspace Expression \enspace (\texttt{,} \enspace Expression)^* \\[16pt]
Type \rightarrow & \enspace BaseType \\\
| & \enspace ArrayType \\\
| & \enspace LambdaType \\[16pt]
BaseType \rightarrow & \enspace \texttt{void} \\\
| & \enspace \texttt{bool} \\\
| & \enspace \texttt{char} \\\
| & \enspace \texttt{int} \\\
| & \enspace \texttt{string} \\[16pt]
ArrayType \rightarrow & \enspace \texttt{[} \enspace Type \enspace \texttt{]} \\[16pt]
LambdaType \rightarrow & \enspace \texttt{<} \enspace TypeList^? \enspace \texttt{>} \enspace \texttt{->} \enspace Type \\[16pt]
TypeList \rightarrow & \enspace Type \enspace (\texttt{,} \enspace Type)^*
\end{aligned}$$
- Comments start with
#
and continue until either the next #
or the end of the line.
- Maximum identifier length is 32 characters.
- Non-void functions with no return statement cause a runtime error.
- Declaration statements that are part of a for-loop should not end with a terminator (
.
).