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>2026-08-10 11:11:18 -0700
commitdca50fde64f64cd105c35651e12ea09f81db2cc3 (patch)
tree4f8316733e7e5eab2d63daadcb29feddf62622d3 /docs/chapter02/02-grammar.md
parenta7818ec324de3031462e5c4d4cb055bf7fde4ebb (diff)
downloadSimple-dca50fde64f64cd105c35651e12ea09f81db2cc3.tar.gz
Simple-dca50fde64f64cd105c35651e12ea09f81db2cc3.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: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: hadha <helsgithub15@gmail.com> Co-authored-by: RobertObkircher <robert.obkircher@gmail.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]
+ ;
+```