summaryrefslogtreecommitdiff
path: root/docs/chapter04/04-grammar.md
diff options
context:
space:
mode:
authorDibyendu Majumdar <mobile@majumdar.org.uk>2023-10-20 15:21:08 -0700
committerDibyendu Majumdar <mobile@majumdar.org.uk>2024-11-16 15:19:12 -0800
commit1f5078741096dc2fe52a75fb5a25bde8e92e8a31 (patch)
tree99b287a4f092af90e6abd20e0a0aca53b52ab4c6 /docs/chapter04/04-grammar.md
parent6e6014bf3596e752b64b91588b8c4e85cf5b817b (diff)
downloadSimple-1f5078741096dc2fe52a75fb5a25bde8e92e8a31.tar.gz
Simple-1f5078741096dc2fe52a75fb5a25bde8e92e8a31.zip
Chapter 4: External Argument and Comparisons
Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Yichen Yan <oraluben@outlook.com> Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Tamas Hadhazy <helsgithub15@gmail.com> Co-authored-by: Hels15 <helsgithub15@gmail.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com>
Diffstat (limited to 'docs/chapter04/04-grammar.md')
-rw-r--r--docs/chapter04/04-grammar.md76
1 files changed, 76 insertions, 0 deletions
diff --git a/docs/chapter04/04-grammar.md b/docs/chapter04/04-grammar.md
new file mode 100644
index 00000000..48facd4c
--- /dev/null
+++ b/docs/chapter04/04-grammar.md
@@ -0,0 +1,76 @@
+# Grammar for Chapter 1
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | metaStatement
+ ;
+
+metaStatement
+ : '#showGraph' ';'
+ ;
+
+expressionStatement
+ : IDENTIFIER '=' expression ';'
+ ;
+
+blockStatement
+ : '{' statement+ '}'
+ ;
+
+declStatement
+ : 'int' IDENTIFIER '=' expression ';'
+ ;
+
+returnStatement
+ : 'return' expression ';'
+ ;
+
+expression
+ : comparisonExpression
+ ;
+
+comparisonExpression
+ : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)*
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-') unaryExpression
+ | primaryExpression
+ ;
+
+primaryExpression
+ : IDENTIFIER
+ | INTEGER_LITERAL
+ | '(' expression ')'
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+
+IDENTIFIER
+ : NON_DIGIT (NON_DIGIT | DIGIT)*
+ ;
+
+NON_DIGIT: [a-zA-Z_];
+DEC_DIGIT: [0-9];
+```