Navigation
Synopsis Parse a Func program from a string or a file.
Description Parsing uses the syntax rules for a given start non-terminnal to parse a string and turn it into a parse tree. The work horse is the Rascal:parse function that is available in the Rascal:ParseTree library.
Examples Here is how to parse Func programs from a string or file:
module demo::lang::Func::Parse

import demo::lang::Func::Func;
import ParseTree;

public Prog parse(loc l) = parse(#Prog, l);
public Prog parse(str s) = parse(#Prog, s);
Let's try this on example F0.func:
fact(n) = if n <= 1 then
             1 
          else 
             n * fact(n-1)
          end
First, we try the version with a string argument:
rascal>import demo::lang::Func::Parse;
ok
rascal>import demo::lang::Func::programs::F0;
ok
rascal>parse(F0);
sort("Prog"): `fact(n) = if n <= 1 then
             1 
          else 
             n * fact(n-1)
          end`
Tree: appl(prod(label("prog",sort("Prog")),[\iter-star-seps(sort("Func"),[layouts("LAYOUTLIST")])],{}),[appl(regular(\iter-star-seps(sort("Func"),[layouts("LAYOUTLIST")])),[appl(prod(label("func",sort("Func")),[label("name",lex("Ident")),layouts("LAYOUTLIST"),lit("("),layouts("LAYOUTLIST"),\iter-star-seps(lex("Ident"),[layouts("LAYOUTLIST"),lit(","),layouts("LAYOUTLIST")]),layouts("LAYOUTLIST"),lit(")"),layouts("LAYOUTLIST"),lit("="),layouts("LAYOUTLIST"),sort("Exp")],{}),[appl(prod(lex("Ident"),[\char-class([range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)])),{\not-follow(\char-class([range(48,57),range(65,90),range(97,122)]))})],{}),[char(102),appl(regular(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)]))),[char(97),char(99),char(116)])[@loc=|file://-|(1,3,<1,1>,<1,4>)]])[@loc=|file://-|(0,4,<1,0>,<1,4>)],appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|file://-|(4,0,<1,4>,<1,4>)]])[@loc=|file://-|(4,0,<1,4>,<1,4>)],appl(prod(lit("("),[\char-class([range(40,40)])],{}),[char(40)]),appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|file://-|(5,0,<1,5>,<1,5>)]])[@loc=|file://-|(5,0,<1,5>,<1,5>)],appl(regular(\iter-star-seps(lex("Ident"),[layouts("LAYOUTLIST"),lit(","),layouts("LAYOUTLIST")])),[appl(prod(lex("Ident"),[\char-class([range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)])),{\not-follow(\char-class([range(48,57),range(65,90),range(97,122)]))})],{}),[char(110),appl(regular(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)]))),[])[@loc=|file://-|(6,0,<1,6>,<1,6>)]])[@loc=|file://-|(5,1,<1,5>,<1,6>)]])[@loc=|file://-|(5,1,<1,5>,<1,6>)],appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|file://-|(6,0,<1,6>,<1,6>)]...
This must be defined as success: we get the original program and its parse tree back. Next, we try the same from a file. We use the scheme std that refers to files that reside in the Rascal library. See Rascal:Location for further details on other schemes.
rascal>parse(|clib-rascal:///demo/lang/Func/programs/F0.func|);
sort("Prog"): `fact(n) = if n <= 1 then
             1 
          else 
             n * fact(n-1)
          end`
Tree: appl(prod(label("prog",sort("Prog")),[\iter-star-seps(sort("Func"),[layouts("LAYOUTLIST")])],{}),[appl(regular(\iter-star-seps(sort("Func"),[layouts("LAYOUTLIST")])),[appl(prod(label("func",sort("Func")),[label("name",lex("Ident")),layouts("LAYOUTLIST"),lit("("),layouts("LAYOUTLIST"),\iter-star-seps(lex("Ident"),[layouts("LAYOUTLIST"),lit(","),layouts("LAYOUTLIST")]),layouts("LAYOUTLIST"),lit(")"),layouts("LAYOUTLIST"),lit("="),layouts("LAYOUTLIST"),sort("Exp")],{}),[appl(prod(lex("Ident"),[\char-class([range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)])),{\not-follow(\char-class([range(48,57),range(65,90),range(97,122)]))})],{}),[char(102),appl(regular(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)]))),[char(97),char(99),char(116)])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(1,3,<1,1>,<1,4>)]])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(0,4,<1,0>,<1,4>)],appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(4,0,<1,4>,<1,4>)]])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(4,0,<1,4>,<1,4>)],appl(prod(lit("("),[\char-class([range(40,40)])],{}),[char(40)]),appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(5,0,<1,5>,<1,5>)]])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(5,0,<1,5>,<1,5>)],appl(regular(\iter-star-seps(lex("Ident"),[layouts("LAYOUTLIST"),lit(","),layouts("LAYOUTLIST")])),[appl(prod(lex("Ident"),[\char-class([range(65,90),range(97,122)]),conditional(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)])),{\not-follow(\char-class([range(48,57),range(65,90),range(97,122)]))})],{}),[char(110),appl(regular(\iter-star(\char-class([range(48,57),range(65,90),range(97,122)]))),[])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(6,0,<1,6>,<1,6>)]])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(5,1,<1,5>,<1,6>)]])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(5,1,<1,5>,<1,6>)],appl(prod(layouts("LAYOUTLIST"),[conditional(\iter-star(lex("LAYOUT")),{\not-follow(\char-class([range(9,10),range(13,13),range(32,32)]))})],{}),[appl(regular(\iter-star(lex("LAYOUT"))),[])[@loc=|clib-rascal:///demo/lang/Func/programs/F0.func|(6,0,<1,6>,<1,6>)]...
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.