IParse Studio

Enter the grammar:

The input to be parsed by the grammar:

Abstract syntax tree evaluator

When the 'evaluate' button is clicked, the abstract syntax tree is evaluated according to the annotations of the nodes and the basic values. Below the supported annotations are mentioned and their function is explained: The result of the evaluation (and the result of print statements) is displayed in the output text area.

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