Posted to tcl by abc at Mon Oct 22 10:00:56 GMT 2018view pretty

#!/usr/bin/tclsh
#
# abc @irc.freenode.net
#
# aycock parser following example in
# http://core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/modules/grammar_aycock/aycock.html
#
# modified to output instantiated syntax tree
#

package require grammar::aycock

## modified grammar, symbolic syntax tree output
set p [grammar::aycock::parser {
    start ::= E {}
    E ::= E + T {list [lindex $_ 0] [lindex $_ 2] ADD}
    E ::= T {}
    T ::= T * F {list [lindex $_ 0] [lindex $_ 2] MUL}
    T ::= F {}
    F ::= NUMBER {}
    F ::= ( E ) {lindex $_ 1}
}]

puts [$p parse {( NUMBER + NUMBER ) * ( NUMBER + NUMBER ) }  \
               {{}  2    {}  3  {} {} {}  7    {}   1   {}}]

$p destroy