Skip to main content

module demo::lang::Pico::Syntax

rascal-0.40.17

Usage

import demo::lang::Pico::Syntax;

Dependencies

import ParseTree;

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: "%" ![%]+ "%"
| @category="Comment" ws3: "%%" ![\n]* $
;

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:"natural"
| string :"string"
;

syntax Statement

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

syntax Expression

syntax Expression        
= id: Id name
| strCon: String string
| natCon: Natural natcon
| bracket "(" Expression e ")"
> left conc: Expression lhs "||" Expression rhs
> left ( add: Expression lhs "+" Expression rhs
| sub: Expression lhs "-" Expression rhs
)
;

function program

start[Program] program(str s) {
return parse(#start[Program], s);
}

start[Program] program(str s, loc l) {
return parse(#start[Program], s, l);
}