summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordibyendumajumdar <mobile@majumdar.org.uk>2023-08-25 23:26:23 +0100
committerdibyendumajumdar <mobile@majumdar.org.uk>2023-08-25 23:26:23 +0100
commitb8f815a16ebad6675b6305d623196a8210145b75 (patch)
tree83ac01235150a525db6c8d73b56e0792c75d5408
parent9a2ab9bb803aa4da2a6007cc2e945d6bc1fe0b39 (diff)
downloadSimple-b8f815a16ebad6675b6305d623196a8210145b75.tar.gz
Simple-b8f815a16ebad6675b6305d623196a8210145b75.zip
Added a grammar definition for chapter 01.
-rw-r--r--README.md2
-rw-r--r--chapter01/README.md10
-rw-r--r--chapter01/docs/01-grammar.md39
3 files changed, 50 insertions, 1 deletions
diff --git a/README.md b/README.md
index f764099c..78db3c08 100644
--- a/README.md
+++ b/README.md
@@ -28,7 +28,7 @@ Sea Of Nodes intermediate representation.
The simple language will be a small subset of C.
* [Common](common/README.md): Common utilities including a lexer.
-* [Language](language/README.md): definition.
+* [Language](language/README.md): Simple language definition.
* [Chapter 1](chapter01/README.md): Simple control flow statements, `if` and `while`. Variables of `int` type. Simple
arithmetic such as addition, subtraction, multiplication, division. Non-zero values will be truthy.
* Chapter 2: Function calls.
diff --git a/chapter01/README.md b/chapter01/README.md
index e69de29b..d21a55e8 100644
--- a/chapter01/README.md
+++ b/chapter01/README.md
@@ -0,0 +1,10 @@
+# Chapter 1
+Simple control flow statements
+
+The chapter will implement:
+* Control flow statements, `if` and `while`.
+* Variables of `int` type.
+* Simple arithmetic such as addition, subtraction, multiplication, division.
+* Non-zero values will be truthy.
+
+* [Grammar](docs/01-grammar.md) \ No newline at end of file
diff --git a/chapter01/docs/01-grammar.md b/chapter01/docs/01-grammar.md
index e69de29b..92ed7357 100644
--- a/chapter01/docs/01-grammar.md
+++ b/chapter01/docs/01-grammar.md
@@ -0,0 +1,39 @@
+# Grammar for Chapter 1
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : declaration* statement+ EOF
+ ;
+
+declaration
+ : type IDENTIFIER ';'
+ ;
+
+type
+ : 'int'
+ ;
+
+statement
+ : 'if' '(' expression ')' statement
+ | 'if' '(' expression ')' statement 'else' statement
+ | 'while' '(' expression ')' statement
+ | IDENTIFIER '=' expression ';'
+ | '{' declaration* statement+ '}'
+ | 'break' ';'
+ ;
+
+expression
+ : primaryExpression # PrimaryExpression_
+ | expression ('*' | '/' ) expression # ArithmeticOrLogicalExpression
+ | expression ('+' | '-') expression # ArithmeticOrLogicalExpression
+ ;
+
+primaryExpression
+ :
+ | INTEGER_LITERAL
+ | IDENTIFIER
+ | '(' expression ')'
+ ;
+``` \ No newline at end of file