summaryrefslogtreecommitdiff
path: root/chapter14/docs/14-grammar.md
diff options
context:
space:
mode:
authordibyendumajumdar <mobile@majumdar.org.uk>2024-08-06 13:09:38 +0100
committerThalia Archibald <thalia@archibald.dev>2024-09-07 10:54:48 -0700
commitf445971ffd36dd58af4e68b43f2e3302fdda42f2 (patch)
treea8471ed91cd1b314e64c2a6eae0ea50ac0847e8d /chapter14/docs/14-grammar.md
parent105c70c2fba46fae77a48198b9578e467953a49a (diff)
downloadSimple-f445971ffd36dd58af4e68b43f2e3302fdda42f2.tar.gz
Simple-f445971ffd36dd58af4e68b43f2e3302fdda42f2.zip
#98 Chapter 14 arrays
Diffstat (limited to 'chapter14/docs/14-grammar.md')
-rw-r--r--chapter14/docs/14-grammar.md129
1 files changed, 129 insertions, 0 deletions
diff --git a/chapter14/docs/14-grammar.md b/chapter14/docs/14-grammar.md
new file mode 100644
index 00000000..65611618
--- /dev/null
+++ b/chapter14/docs/14-grammar.md
@@ -0,0 +1,129 @@
+# Grammar for Chapter 13
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | structDeclaration
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | ifStatement
+ | whileStatement
+ | breakStatement
+ | continueStatment
+ | metaStatement
+ ;
+
+field
+ : 'int' IDENTIFIER ';'
+ ;
+
+fields
+ : field+
+ ;
+
+structDeclaration
+ : 'struct' IDENTIFIER '{' fields '}'
+ ;
+
+whileStatement
+ : 'while' '(' expression ')' statement
+ ;
+
+breakStatement
+ : 'break' ';'
+ ;
+
+continueStatement
+ : 'continue' ';'
+ ;
+
+ifStatement
+ : 'if' '(' expression ')' statement ('else' statement)?
+ ;
+
+metaStatement
+ : '#showGraph' ';'
+ ;
+
+expressionStatement
+ : IDENTIFIER '=' expression ';'
+ | fieldExpression '=' expression ';'
+ ;
+
+blockStatement
+ : '{' statement+ '}'
+ ;
+
+structName
+ : IDENTIFIER
+ ;
+
+declStatement
+ : 'int' IDENTIFIER '=' expression ';'
+ | structName IDENTIFIER ('?')? '=' expression ';'
+ ;
+
+returnStatement
+ : 'return' expression ';'
+ ;
+
+expression
+ : comparisonExpression
+ ;
+
+comparisonExpression
+ : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)*
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-') unaryExpression
+ | '!' unaryExpression
+ | primaryExpression
+ ;
+
+newExpression
+ : 'new' IDENTIFIER
+ ;
+
+fieldExpression
+ : primaryExpresson '.' IDENTIFIER
+ ;
+
+primaryExpression
+ : IDENTIFIER
+ | INTEGER_LITERAL
+ | 'true'
+ | 'false'
+ | 'null'
+ | newExpression
+ | '(' expression ')'
+ | fieldExpression
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+
+IDENTIFIER
+ : NON_DIGIT (NON_DIGIT | DIGIT)*
+ ;
+
+NON_DIGIT: [a-zA-Z_];
+DEC_DIGIT: [0-9];
+```