summaryrefslogtreecommitdiff
path: root/docs/chapter02/02-grammar.md
diff options
context:
space:
mode:
authorDibyendu Majumdar <mobile@majumdar.org.uk>2023-09-26 17:43:36 +0100
committerDibyendu Majumdar <mobile@majumdar.org.uk>2024-11-27 11:29:33 -0800
commit3b02b5bdffaf2e3191d55fbb468dc765eb439025 (patch)
tree1421f23f6835f41b3ea97fe6c5222368a337eec2 /docs/chapter02/02-grammar.md
parentd63e09a8edf606fea89c6036bc5ae50a331bfd90 (diff)
downloadSimple-3b02b5bdffaf2e3191d55fbb468dc765eb439025.tar.gz
Simple-3b02b5bdffaf2e3191d55fbb468dc765eb439025.zip
Chapter 2: Arithmetic
Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Yichen Yan <oraluben@outlook.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com>
Diffstat (limited to 'docs/chapter02/02-grammar.md')
-rw-r--r--docs/chapter02/02-grammar.md40
1 files changed, 40 insertions, 0 deletions
diff --git a/docs/chapter02/02-grammar.md b/docs/chapter02/02-grammar.md
new file mode 100644
index 00000000..ce875eda
--- /dev/null
+++ b/docs/chapter02/02-grammar.md
@@ -0,0 +1,40 @@
+# Grammar for Chapter 1
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement EOF
+ ;
+
+statement
+ : 'return' expression ';'
+ ;
+
+expression
+ : additiveExpression
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-') unaryExpression
+ | primaryExpression
+ ;
+
+primaryExpression
+ : INTEGER_LITERAL
+ | '(' expression ')'
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+```