summaryrefslogtreecommitdiff
path: root/docs/chapter04/04-grammar.md
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2023-10-20 15:21:08 -0700
committerCliff Click <cliffc@acm.org>2026-10-22 15:47:50 -0700
commit4cf0a1705096755a9f5e8bee3b04ad456c9c153c (patch)
tree2e5060818a8f7eb67ba8d9d4e160a638fbc1a14d /docs/chapter04/04-grammar.md
parent487065428a4a834f18f90b91a36d9bff02e55e5b (diff)
downloadSimple-4cf0a1705096755a9f5e8bee3b04ad456c9c153c.tar.gz
Simple-4cf0a1705096755a9f5e8bee3b04ad456c9c153c.zip
Chapter 4: External Argument and Comparisons
Co-authored-by: Dibyendu Majumdar <mobile@majumdar.org.uk> Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> 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> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: tannal <tannal2409@gmail.com> Co-authored-by: hadha <helsgithub15@gmail.com> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: RobertObkircher <robert.obkircher@gmail.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com>
Diffstat (limited to 'docs/chapter04/04-grammar.md')
-rw-r--r--docs/chapter04/04-grammar.md74
1 files changed, 74 insertions, 0 deletions
diff --git a/docs/chapter04/04-grammar.md b/docs/chapter04/04-grammar.md
new file mode 100644
index 00000000..bd01c305
--- /dev/null
+++ b/docs/chapter04/04-grammar.md
@@ -0,0 +1,74 @@
+# Grammar for Chapter 4
+
+English | [日本語](04-grammar.ja.md)
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ ;
+
+
+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];
+```