summaryrefslogtreecommitdiff
path: root/chapter14/docs/14-grammar.md
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-10-13 09:11:01 -0700
committerCliff Click <cliffc@acm.org>2024-10-13 09:49:57 -0700
commitdb7a5bd09f5e71de9df8217162bd4e8f2d2a2cc6 (patch)
treedc8d452e8626faaeafadd46fbc7c3cf8c1bcd17b /chapter14/docs/14-grammar.md
parentf4dee3cff700026b0997aac681752257aa03b4bd (diff)
downloadSimple-db7a5bd09f5e71de9df8217162bd4e8f2d2a2cc6.tar.gz
Simple-db7a5bd09f5e71de9df8217162bd4e8f2d2a2cc6.zip
more grammar cleanup
Diffstat (limited to 'chapter14/docs/14-grammar.md')
-rw-r--r--chapter14/docs/14-grammar.md133
1 files changed, 52 insertions, 81 deletions
diff --git a/chapter14/docs/14-grammar.md b/chapter14/docs/14-grammar.md
index d7b11d24..a6d4df42 100644
--- a/chapter14/docs/14-grammar.md
+++ b/chapter14/docs/14-grammar.md
@@ -3,21 +3,48 @@
```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 '{' fields '}' ;
+
+fields : field+ ;
+
+field : type IDENTIFIER ';' ;
+
+
+expressionStatement
+ : type IDENTIFIER ';'
+ | type IDENTIFIER '=' expression ';'
+ | IDENTIFIER '=' expression ';'
+ | expression
;
PRIMTYPE
@@ -35,64 +62,15 @@ PRIMTYPE
| 'bool'
;
-field
- : PRIMTYPE IDENTIFIER ';'
- | structName IDENTIFIER ('?')? ';'
- ;
-
-fields
- : field+
- ;
-
-structDeclaration
- : 'struct' IDENTIFIER '{' fields '}'
- ;
-
-whileStatement
- : 'while' '(' expression ')' statement
+type
+ : PRIMTYPE
+ | typeName ('?')?
;
-breakStatement
- : 'break' ';'
- ;
-
-continueStatement
- : 'continue' ';'
- ;
-
-ifStatement
- : 'if' '(' expression ')' statement ('else' statement)?
- ;
-
-metaStatement
- : '#showGraph' ';'
- ;
+typeName : IDENTIFIER ;
-expressionStatement
- : IDENTIFIER '=' expression ';'
- | fieldExpression '=' expression ';'
- ;
-
-blockStatement
- : '{' statement+ '}'
- ;
-structName
- : IDENTIFIER
- ;
-
-declStatement
- : PRIMTYPE IDENTIFIER '=' expression ';'
- | structName IDENTIFIER ('?')? '=' expression ';'
- ;
-
-returnStatement
- : 'return' expression ';'
- ;
-
-expression
- : bitWiseExpression
- ;
+expression : bitWiseExpression ;
bitWiseExpression
: comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)*
@@ -117,37 +95,30 @@ multiplicativeExpression
unaryExpression
: ('-') unaryExpression
| '!' unaryExpression
- | primaryExpression
- ;
-
-newExpression
- : 'new' IDENTIFIER
- ;
-
-fieldExpression
- : primaryExpresson '.' IDENTIFIER
+ | primaryExpression postFix*
;
primaryExpression
- : IDENTIFIER
- | INTEGER_LITERAL
+ : INTEGER_LITERAL
+ | '(' expression ')'
| 'true'
| 'false'
| 'null'
| newExpression
- | '(' expression ')'
- | fieldExpression
+ | IDENTIFIER
;
+newExpression : 'new' IDENTIFIER ;
+
+postFix : '.' IDENTIFIER [ '=' expression ] ;
+
INTEGER_LITERAL
- : [1-9][0-9]*
+ : [1-9]DIGIT*
| [0]
;
-IDENTIFIER
- : NON_DIGIT (NON_DIGIT | DIGIT)*
- ;
+IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ;
NON_DIGIT: [a-zA-Z_];
-DEC_DIGIT: [0-9];
+DIGIT: [0-9];
```