summaryrefslogtreecommitdiff
path: root/chapter16/docs
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-10-14 10:31:40 -0700
committerCliff Click <cliffc@acm.org>2024-10-21 08:52:01 -0700
commit94e557c9de68e7b3f5595d75e7330b63df76c323 (patch)
treef16fca7e4b49f82f98f73c073b7495abedafd195 /chapter16/docs
parent475f66adc7c1c9f1cd479c4492999eae4c060c5b (diff)
downloadSimple-94e557c9de68e7b3f5595d75e7330b63df76c323.tar.gz
Simple-94e557c9de68e7b3f5595d75e7330b63df76c323.zip
final fields and multi defs
`int x,y;` works `int !x = 3;` works Structs defs are statements, require a trailing `;`
Diffstat (limited to 'chapter16/docs')
-rw-r--r--chapter16/docs/16-grammar.md143
1 files changed, 60 insertions, 83 deletions
diff --git a/chapter16/docs/16-grammar.md b/chapter16/docs/16-grammar.md
index a7f69133..7b611fbf 100644
--- a/chapter16/docs/16-grammar.md
+++ b/chapter16/docs/16-grammar.md
@@ -3,21 +3,43 @@
```antlrv4
grammar SimpleLanguage;
-program
- : statement+ EOF
- ;
+program : block EOF ;
+
+block : statement+ ;
+
statement
- : returnStatement
- | structDeclaration
- | declStatement
- | blockStatment
- | expressionStatement
+ : '{' block '}'
+ | returnStatement
| ifStatement
| whileStatement
| breakStatement
- | continueStatment
+ | continueStatement
| metaStatement
+ | structDeclaration
+ | expressionStatement
+ ;
+
+
+returnStatement : 'return' expression ';' ;
+
+ifStatement : 'if' '(' expression ')' statement ('else' statement)? ;
+
+whileStatement : 'while' '(' expression ')' statement ;
+
+breakStatement : 'break' ';' ;
+
+continueStatement : 'continue' ';' ;
+
+metaStatement : '#showGraph' ';' ;
+
+structDeclaration : 'struct' IDENTIFIER '{' block '}' ;
+
+expressionStatement
+ : type IDENTIFIER ';'
+ | type IDENTIFIER '=' expression ';'
+ | IDENTIFIER '=' expression ';'
+ | expression
;
PRIMTYPE
@@ -35,64 +57,18 @@ PRIMTYPE
| 'bool'
;
-field
- : PRIMTYPE IDENTIFIER ';'
- | structName IDENTIFIER ('?')? ';'
- ;
-
-fields
- : field+
- ;
-
-structDeclaration
- : 'struct' IDENTIFIER '{' fields '}'
- ;
-
-whileStatement
- : 'while' '(' expression ')' statement
- ;
-
-breakStatement
- : 'break' ';'
- ;
-
-continueStatement
- : 'continue' ';'
+type
+ : PRIMTYPE
+ | typeName '?'?
+ | typeName nestedArray+
;
-ifStatement
- : 'if' '(' expression ')' statement ('else' statement)?
- ;
-
-metaStatement
- : '#showGraph' ';'
- ;
-
-expressionStatement
- : IDENTIFIER '=' expression ';'
- | fieldExpression '=' expression ';'
- ;
-
-blockStatement
- : '{' statement+ '}'
- ;
+nestedArray : '?'? '[]'* ;
-structName
- : IDENTIFIER
- ;
+typeName : IDENTIFIER ;
-declStatement
- : PRIMTYPE IDENTIFIER '=' expression ';'
- | structName IDENTIFIER ('?')? '=' expression ';'
- ;
-returnStatement
- : 'return' expression ';'
- ;
-
-expression
- : bitWiseExpression
- ;
+expression : bitWiseExpression ;
bitWiseExpression
: comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)*
@@ -117,39 +93,40 @@ multiplicativeExpression
unaryExpression
: ('-') unaryExpression
| '!' unaryExpression
- | primaryExpression
- ;
-
-newExpression
- : 'new' IDENTIFIER blockStatement
- : 'new' IDENTIFIER '[' expression ']'
- ;
-
-fieldExpression
- : primaryExpresson '.' IDENTIFIER
- | primaryExpresson '[' expression ']'
+ | primaryExpression postAssign
;
primaryExpression
- : IDENTIFIER
- | INTEGER_LITERAL
+ : INTEGER_LITERAL
+ | FLOAT_LITERAL
+ | '(' expression ')'
| 'true'
| 'false'
| 'null'
| newExpression
- | '(' expression ')'
- | fieldExpression
+ | IDENTIFIER
;
-INTEGER_LITERAL
- : [1-9][0-9]*
- | [0]
+newExpression
+ : 'new' IDENTIFIER [ '{' block '}' ]
+ | 'new' IDENTIFIER '[' expression ']'
;
-IDENTIFIER
- : NON_DIGIT (NON_DIGIT | DIGIT)*
+postAssign : postFix [ '=' expression ] | [ '#' ];
+
+postFix
+ : '.' IDENTIFIER postFix
+ | '[' expression ']' postFix
+ ;
+
+
+IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ;
+
+INTEGER_LITERAL
+ : [1-9]DIGIT*
+ | [0]
;
NON_DIGIT: [a-zA-Z_];
-DEC_DIGIT: [0-9];
+DIGIT: [0-9];
```