Navigation
Synopsis Convert a Pico parse tree into a Pico abstract syntax tree.
Examples The mapping between parse tree and abstract sybtax tree is achieved as follows:
module demo::lang::Pico::Load

import Prelude;
import demo::lang::Pico::Syntax;
import demo::lang::Pico::Abstract;

public PROGRAM  load(str txt) = implode(#PROGRAM, parse(#Program, txt));

Notes:
  • The function load takes a string as argument (supposedly the source code of a Pico program) and returns a value of type PROGRAM, the abstract syntax tree of the input program. In case the input program is syntactically incorrect, a ParseError exception will be thrown,see Rascal:RunTimeException.
  • parse(#Program, txt): parse txt according to the non-terminal Program. Note that #Program is a reified type, i.e., the type Program is represented as an ordinary Rascal value and passed as argument to the parse function, see Rascal:ReifiedTypes. The parse function returns a parse tree of the input program.
  • implode(#PROGRAM, parse(#Program, txt)): transform the parse returned by parse into an abstract syntax tree of type PROGRAM. The Rascal:implode function performs the automatic mapping between elements in the parse tree and their counterpart in the abstract syntax.
The function load can be used as follows:
rascal>import demo::lang::Pico::Load;
ok
rascal>load("begin declare x : natural; x := 3 end");
PROGRAM: program(
  [decl(
      "x",
      natural()[
        @location=|file://-|(18,7,<1,18>,<1,25>),
        @comments=()
      ])[
      @location=|file://-|(14,11,<1,14>,<1,25>),
      @comments=()
    ]],
  [asgStat(
      "x",
      natCon(3)[
        @location=|file://-|(32,1,<1,32>,<1,33>),
        @comments=()
      ])[
      @location=|file://-|(27,6,<1,27>,<1,33>),
      @comments=()
    ]])[
  @location=|file://-|(0,37,<1,0>,<1,37>),
  @comments=()
]
Observe how the various parts of the abstract syntax tree are annotated with location attributes.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.