The input to be parsed by the grammar:

`true`: Return 'true'`false`: Return 'false'`add`: Add the values of the two child nodes.`sub`: Substract the values of the two child nodes.`times`: Multiplies the values of the two child nodes.`div`: Divides the values of the two child nodes.`min`: Minus the value of the one child node.`mod`: Modulo of the value of the two child nodes.`sum`: Sums the values of the child nodes.`multiply`: Multiplies the values of the child nodes.`lt`: True if the value of the left node is smaller than the value of the right value.`le`: True if the value of the left node is smaller or equal than the value of the right value.`gt`: True if the value of the left node is greater than the value of the right value.`ge`: True if the value of the left node is greater or equal than the value of the right value.`eq`: True if the value of the left node is equal to the value of the right value.`ne`: True if the value of the left node is not equal to the value of the right value.`and`: True if the values of the two child nodes are true.`or`: True if at least on of the values of the two child nodes is true.`not`: True if the value of the one child node is not true.`ass`: Assigns the value of the second node to the variable named by the first node.`assto`: Assigns the value of the first node to the variable named by the second node.`ifthenelse`: If the value of the first node is true, the second node is evaluated, otherwise the third`whenotherwise`: If the value of the second node is true, the first node is evaluated, otherwise the third.`while`: While the value of the first nodes evaluates to true, the second node is evaluated.`print`: Prints the value of the only child node on a new line.`prompt`: Prompts the user for a value displaying the text value of the only child node.`LIST`: Evaluates the child nodes and returns the value of the last node.`defaultsto`: Returns the value of the first node, unless it is undefined, then the value of the second node is returned.`fndef`: Returns undefined, but defines a function under the name of the first node, with a parameter list represented by the second list and with a body of the third node.`fncall`: Returns the value of the function with the name of the first node and with the parameters of the list of expressions represented by the second node.

An example grammar using the above annotations is:

root : statement SEQ OPT . statement : ident "=" expr ";" [ass] | "if" expr "then" statement SEQ OPT "else" statement SEQ OPT "fi" [ifthenelse] | "while" expr "do" statement SEQ OPT "od" [while] | "print" expr ";" [print] | "function" ident "(" ident LIST OPT ")" "{" statement SEQ OPT "}" [fndef] | expr ";" . primary_expr : ident "(" expr LIST OPT ")" [fncall] | ident | int | char | string | "(" expr ")" . unary_expr : "!" primary_expr [not] | "-" primary_expr [min] | primary_expr . l_expr1 : l_expr1 "*" unary_expr [times] | l_expr1 "/" unary_expr [div] | l_expr1 "%" unary_expr [mod] | unary_expr . l_expr2 : l_expr2 "+" l_expr1 [add] | l_expr2 "-" l_expr1 [sub] | l_expr1 . l_expr3 : l_expr3 "<=" l_expr2 [le] | l_expr3 ">=" l_expr2 [ge] | l_expr3 "<" l_expr2 [lt] | l_expr3 ">" l_expr2 [gt] | l_expr3 "==" l_expr2 [eq] | l_expr3 "!=" l_expr2 [ne] | l_expr2 . l_expr4 : l_expr4 "&&" l_expr3 [land] | l_expr3 . l_expr5 : l_expr5 "||" l_expr4 [lor] | l_expr4 . expr : l_expr5 "?" l_expr5 ":" expr [ifthenelse] | l_expr5 .An example program in this language, for calculating the greatest common divisor of 345 and 555, is:

function gcd(x, y) { while y != 0 do c = x % y; x = y; y = c; od x; } a = 345; b = 555; print gcd(a, b);

Author: Frans

Personal website

email address