From b8f815a16ebad6675b6305d623196a8210145b75 Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Fri, 25 Aug 2023 23:26:23 +0100 Subject: Added a grammar definition for chapter 01. --- README.md | 2 +- chapter01/README.md | 10 ++++++++++ chapter01/docs/01-grammar.md | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3