![]() |
|
Navigation |
Synopsis Syntax Definitions allow the definition of parsers for programming languages, domain-specific languages and data formats.
Syntax
Start is either start or nothing, and Alternatives are one of:
Associativity is nothing, or one of assoc , left , right or non-assoc , and Tags are a possibly empty list of tags.
Description Rascal supports full context-free grammars for syntax definition. It generates scannerless parsers from these definitions. These parsers produce ParseTrees that can be further processed by Rascal using ConcreteSyntax fragments in Patterns and Expressions, or they can be imploded to AlgebraicDataTypes.
There are four kinds of non-terminals that can be defined with slightly different characteristics.
| , > and associativity combinators.
The latter two represent Disambiguation constructs that you should read more about. The | is a short-hand for not having to repeat syntax A = for every alternative of A .
Alternatives can be named or not. The names are essential only if:
The start modifier identifies the start of a grammar. The effect of a start modifier is that Rascal will generate an extra syntax definition before generating a parser that allows layout to before and after the start non-terminal. For example: layout L = [\ ]*; start Program = Statement*; will produce syntax start[Program] = L Program top L; . Note that the start[Program] type is now available in your program, and ParseTrees assigned to variable of that type will allow access to the top field.
Examples The following example makes use of practically all of the SyntaxDefinition features, except parse actions.
// layout is lists of whitespace characters layout MyLayout = [\t\n\ \r\f]*; // identifiers are characters of lowercase alphabet letters, // not immediately preceded or followed by those (longest match) // and not any of the reserved keywords lexical Identifier = [a-z] !<< [a-z]+ !>> [a-z] \ MyKeywords; // this defines the reserved keywords used in the definition of Identifier keyword MyKeywords = "if" | "then" | "else" | "fi"; // here is a recursive definition of expressions // using priority and associativity groups. syntax Expression = id: Identifier id | null: "null" | left multi: Expression l "*" Expression r > left ( add: Expression l "+" Expression r | sub: Expression l "-" Expression r ) | bracket "(" Expression ")" ;
Benefits
Pitfalls
![]() |