source: proto/pablo/input/grammar/scatter/pablo.scatter.beta @ 2658

Last change on this file since 2658 was 2658, checked in by ksherdy, 6 years ago

Shortened variable names for sanity.

File size: 3.6 KB
Line 
1
2// 1. insert curlies in pythodn code to mark code blocks.
3
4context main {
5 recognizers {
6         Whitespace [IGNORE],
7         Integer [INTEGER_CONST],
8         Identifier [IDENTIFIER],        // note: java identifiers. This also produces LextantTokens for keywords.
9         Comment [IGNORE] "#" "\n",
10         Comment [SQ_STRING] "'" "'",
11         Comment [IGNORE] "/***" "***/",                 // comment for commenting-out sections of code during debugging of this grammar.
12         String [STRING],
13         Punctuator,
14         EndOfInput,
15 }
16
17 tokens {
18         IF,
19         ELSE,
20         ELIF,
21         WHILE,
22         CLASS,
23         IMPORT,
24         DEF,
25         
26         DOT ".",
27         COLON ":",
28         COMMA ",",
29         TERMINATOR ";",
30         
31         LEFTROUND "(", RIGHTROUND ")",
32//               LEFTSQUARE "[", RIGHTSQUARE "]",
33         LEFTCURLY "{", RIGHTCURLY "}",
34
35         OR "|",
36         OR_EQUAL "|=",
37         XOR "^",
38         XOR_EQUAL "^=",
39         AND "&",
40         AND_EQUAL "&=",
41         NOT "~",
42         MINUS "-",
43         PLUS "+",
44         ASSIGN "=",
45 }
46}
47
48
49interfaces {
50//       escapable {boolean escaped = false;},
51}
52
53nodes {
54 binaryOperator {} [expression, expression1, expression3],
55 unaryOperator {} [expression2],
56 assignment {} [assignmentStmtRest],
57 ifStmt {} [ifStmtBulk],
58 FunctionCall {} [FunctionCallRest],
59 ASTNode [assignmentOrFunctionCall, variableOrFunctionCall],
60}
61
62grammar {
63 start → (import | structDeclaration | functionDef )*;
64 
65 import -> IMPORT identifier ;
66 
67 
68 ////////////////////////////////////////////////////////////////////////
69 // classes in python are used for structs in pablo.
70 // We need to put curly braces {} around body of class delcaration.
71 // this grammar only allows simple initialization of fields as class
72 // body Stmts. Verfiy this as a property of the input.
73 ////////////////////////////////////////////////////////////////////////
74 
75 structDeclaration → CLASS structName LEFTROUND RIGHTROUND COLON structBody ;
76 structBody → LEFTCURLY structBodyStmt* RIGHTCURLY ;
77 structBodyStmt ʉ → variableName ASSIGN value TERMINATOR? ;
78 value → INTEGER_CONST ;
79 
80 
81 
82 ////////////////////////////////////////////////////////////////////////
83 // functions.
84 // We need to put curly braces {} around body of function delcaration,
85 // and any if or else or while.
86 //
87 ////////////////////////////////////////////////////////////////////////
88 
89 functionDef → DEF functionName LEFTROUND parameterList RIGHTROUND COLON blockStmt ;
90 parameterList → identifier ( COMMA identifier )* ;
91 
92 Stmt ʉ → (assignmentOrFunctionCall | ifStmt | whileStmt | blockStmt) TERMINATOR?;
93 blockStmt → LEFTCURLY Stmt* RIGHTCURLY ;
94 
95 assignmentOrFunctionCall → compoundVariable ( assignmentStmtRest ▲ | FunctionCallRest ▲ ) ;
96 assignmentStmtRest → assignOp^! expression ;
97 assignOp -> AND_EQUAL | OR_EQUAL | XOR_EQUAL | ASSIGN ;
98 
99 ifStmt ʉ → IF ifStmtBulk ;
100 ifStmtBulk → expression COLON? blockStmt (ELIF COLON? ifStmtBulk | ELSE COLON? blockStmt)? ;
101 whileStmt → WHILE expression COLON? blockStmt ;
102 expressionStmt → expression;
103 
104 expression ʉ → expression1 ( (OR^ | XOR^) expression )? ;    // lowest precedence: OR, XOR
105 expression1 ʉ → expression2 ( AND^ expression1 )? ;          //      AND
106 expression2 ʉ → NOT^^? expression3 ;
107 expression3 ʉ → expression4 ( (MINUS^ | PLUS^) expression3 )? ◄;
108 expression4 ʉ → LEFTROUND expression RIGHTROUND
109 
110              | variableOrFunctionCall
111 
112              | constant ;
113 
114 variableOrFunctionCall ʉ → compoundVariable (FunctionCallRest ▲)? ;
115 FunctionCallRest → LEFTROUND^ expression (COMMA expression)* RIGHTROUND ;
116 
117 compoundVariable ʉ → identifier (DOT^ identifier)* ;
118 constant -> STRING | SQ_STRING | INTEGER_CONST ;
119 
120 structName ʉ → identifier ;
121 variableName ʉ → identifier ;
122 functionName ʉ → identifier ;
123 identifier → IDENTIFIER ;
124 
125}
Note: See TracBrowser for help on using the repository browser.