diff options
| author | dibyendumajumdar <mobile@majumdar.org.uk> | 2023-08-25 23:26:23 +0100 |
|---|---|---|
| committer | dibyendumajumdar <mobile@majumdar.org.uk> | 2023-08-25 23:26:23 +0100 |
| commit | b8f815a16ebad6675b6305d623196a8210145b75 (patch) | |
| tree | 83ac01235150a525db6c8d73b56e0792c75d5408 | |
| parent | 9a2ab9bb803aa4da2a6007cc2e945d6bc1fe0b39 (diff) | |
| download | Simple-b8f815a16ebad6675b6305d623196a8210145b75.tar.gz Simple-b8f815a16ebad6675b6305d623196a8210145b75.zip | |
Added a grammar definition for chapter 01.
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | chapter01/README.md | 10 | ||||
| -rw-r--r-- | chapter01/docs/01-grammar.md | 39 |
3 files changed, 50 insertions, 1 deletions
@@ -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 |
