Navigation
Synopsis Parse of a syntactically incorrect string.
Function data RunTimeException = ParseError(loc location);
Usage import Exception;
Description This error is generated when during the execution of a Rascal program the Rascal:parse function is applied to a syntactically incorrect input sentence.

Remedies:
  • Correct the input sentence.
  • Adapt the grammar so that it accepts the inpout sentence.
  • Catch the ParseError yourself, see Rascal:TryCatch.
Examples Define the Non-terminal As that accepts one or more letters a:
rascal>import ParseTree;
ok
rascal>syntax As = "a"+;
ok
It parses a sentences of letters a:
rascal>parse(#As, "aaaaaaaa");
sort("As"): `aaaaaaaa`
Tree: appl(prod(sort("As"),[\iter-seps(lit("a"),[layouts("default")])],{}),[appl(regular(\iter-seps(lit("a"),[layouts("default")])),[appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(1,0,<1,1>,<1,1>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(2,0,<1,2>,<1,2>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(3,0,<1,3>,<1,3>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(4,0,<1,4>,<1,4>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(5,0,<1,5>,<1,5>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(6,0,<1,6>,<1,6>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(7,0,<1,7>,<1,7>)],appl(prod(lit("a"),[\char-class([range(97,97)])],{}),[char(97)])])[@loc=|file://-|(0,8,<1,0>,<1,8>)]])[@loc=|file://-|(0,8,<1,0>,<1,8>)]
But gives an error when parsing an input that it cannot accept:
rascal>parse(#As, "aaaabaaa");
|rascal://ParseTree|(10175,833,<248,0>,<274,60>): ParseError(|file://-|(4,1,<1,4>,<1,5>))
	at *** somewhere ***(|rascal://ParseTree|(10175,833,<248,0>,<274,60>))
	at parse(|stdin:///|(11,10,<1,11>,<1,21>))


We can also catch the ParseError. First import the Rascal exceptions (which are also included in Prelude) and IO:
rascal>import Exception;
ok
rascal>import IO;
ok
rascal>try parse(#As, "aaaabaaa"); catch ParseError(e): println("Your input cannot be parsed: <e>");
Your input cannot be parsed: |file://-|(4,1,<1,4>,<1,5>)
ok
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.