summaryrefslogtreecommitdiff
path: root/docs/chapter06/06-grammar.md
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2023-11-11 23:17:22 +0000
committerCliff Click <cliffc@acm.org>2025-01-09 12:19:55 -0800
commit7cdc3a61a6ed9dc6d68d731d1d2777d18ddaba27 (patch)
tree6c317c2c1f2111588a6c6d326a31d6c2737eda1d /docs/chapter06/06-grammar.md
parent723389372abbc0b72759e1c9beaf04ef55788854 (diff)
downloadSimple-7cdc3a61a6ed9dc6d68d731d1d2777d18ddaba27.tar.gz
Simple-7cdc3a61a6ed9dc6d68d731d1d2777d18ddaba27.zip
Chapter 6: Peepholes for If
Co-authored-by: Dibyendu Majumdar <mobile@majumdar.org.uk> Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Yichen Yan <oraluben@outlook.com> Co-authored-by: Tamas Hadhazy <helsgithub15@gmail.com> Co-authored-by: Robert Obkircher <robert.obkircher@gmail.com>
Diffstat (limited to 'docs/chapter06/06-grammar.md')
-rw-r--r--docs/chapter06/06-grammar.md83
1 files changed, 83 insertions, 0 deletions
diff --git a/docs/chapter06/06-grammar.md b/docs/chapter06/06-grammar.md
new file mode 100644
index 00000000..4cf1cac6
--- /dev/null
+++ b/docs/chapter06/06-grammar.md
@@ -0,0 +1,83 @@
+# Grammar for Chapter 6
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | ifStatement
+ | metaStatement
+ ;
+
+ifStatement
+ : 'if' '(' expression ')' statement ('else' statement)?
+ ;
+
+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
+ | 'true'
+ | 'false'
+ | '(' expression ')'
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+
+IDENTIFIER
+ : NON_DIGIT (NON_DIGIT | DIGIT)*
+ ;
+
+NON_DIGIT: [a-zA-Z_];
+DEC_DIGIT: [0-9];
+```