diff options
| author | Dibyendu Majumdar <mobile@majumdar.org.uk> | 2023-11-11 23:17:22 +0000 |
|---|---|---|
| committer | Dibyendu Majumdar <mobile@majumdar.org.uk> | 2024-10-23 17:51:09 -0700 |
| commit | ff11dd42bb845ad4a979b8924c678addb8681801 (patch) | |
| tree | e225854c8474cee5e6eb0e041a99c84bea1478ba /docs/chapter06/06-grammar.md | |
| parent | 369268fe6f3bd1dcd59117c8f831f04f4e3b0560 (diff) | |
| download | Simple-linear-chapter06.tar.gz Simple-linear-chapter06.zip | |
Chapter 6: Peepholes for Iflinear-chapter06
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: Tamas Hadhazy <helsgithub15@gmail.com>
Co-authored-by: Robert Obkircher <robert.obkircher@gmail.com>
Diffstat (limited to 'docs/chapter06/06-grammar.md')
| -rw-r--r-- | docs/chapter06/06-grammar.md | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/docs/chapter06/06-grammar.md b/docs/chapter06/06-grammar.md new file mode 100644 index 00000000..4cf1cac6 --- /dev/null +++ b/docs/chapter06/06-grammar.md @@ -0,0 +1,83 @@ +# Grammar for Chapter 6 + +```antlrv4 +grammar SimpleLanguage; + +program + : statement+ EOF + ; + +statement + : returnStatement + | declStatement + | blockStatment + | expressionStatement + | ifStatement + | metaStatement + ; + +ifStatement + : 'if' '(' expression ')' statement ('else' statement)? + ; + +metaStatement + : '#showGraph' ';' + ; + +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 + | 'true' + | 'false' + | '(' expression ')' + ; + +INTEGER_LITERAL + : [1-9][0-9]* + | [0] + ; + +IDENTIFIER + : NON_DIGIT (NON_DIGIT | DIGIT)* + ; + +NON_DIGIT: [a-zA-Z_]; +DEC_DIGIT: [0-9]; +``` |
