Skip to main content

module examples::pico::Syntax

rascal-0.40.16
typepal-0.14.8

Usage

import examples::pico::Syntax;

Source code

http://github.com/usethesource/typepal/src/examples/pico/Syntax.rsc

syntax Id

lexical Id  = [a-z][a-z0-9]* !>> [a-z0-9];

syntax Natural

lexical Natural = [0-9]+ ;

syntax String

lexical String = "\"" ![\"]*  "\"";

syntax Layout

layout Layout = WhitespaceAndComment* !>> [\ \t\n\r%];

syntax WhitespaceAndComment

lexical WhitespaceAndComment 
= [\ \t\n\r]
| @category="Comment" ws2: "%" ![%]+ "%"
;

syntax Program

start syntax Program 
= program: "begin" Declarations decls {Statement ";"}* body "end"
;

syntax Declarations

syntax Declarations 
= "declare" {Declaration ","}* decls ";" ;

syntax Declaration

syntax Declaration 
= decl: Id id ":" Type tp
;

syntax Type

syntax Type 
= "natural"
| "string"
;

syntax Statement

syntax Statement 
= Id var ":=" Expression val
| "if" Expression cond "then" {Statement ";"}* thenPart "else" {Statement ";"}* elsePart "fi"
| "while" Expression cond "do" {Statement ";"}* body "od"
;

syntax Expression

syntax Expression 
= Id name
| String string
| Natural natcon
| bracket "(" Expression e ")"
> left ( Expression lhs "+" Expression rhs
| Expression lhs "-" Expression rhs
)
;