diff options
| author | Dibyendu Majumdar <dibyendumajumdar@users.noreply.github.com> | 2024-06-30 01:58:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-30 01:58:08 +0100 |
| commit | 00d1b8eed7406fc8e150dd5f789cfac48db14286 (patch) | |
| tree | 3595692bfd3fd2da88acb2ebb421ede94501cca7 /chapter11/docs/11-grammar.md | |
| parent | fd8442075faa603b3956d51d81486424624c5355 (diff) | |
| download | Simple-00d1b8eed7406fc8e150dd5f789cfac48db14286.tar.gz Simple-00d1b8eed7406fc8e150dd5f789cfac48db14286.zip | |
Cliff's GCM implementation for chapter 11 (#88)
Diffstat (limited to 'chapter11/docs/11-grammar.md')
| -rw-r--r-- | chapter11/docs/11-grammar.md | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/chapter11/docs/11-grammar.md b/chapter11/docs/11-grammar.md new file mode 100644 index 00000000..0aaf0619 --- /dev/null +++ b/chapter11/docs/11-grammar.md @@ -0,0 +1,128 @@ +# Grammar for Chapter 11 + +```antlrv4 +grammar SimpleLanguage; + +program + : statement+ EOF + ; + +statement + : returnStatement + | structDeclaration + | declStatement + | blockStatment + | expressionStatement + | ifStatement + | whileStatement + | breakStatement + | continueStatment + | metaStatement + ; + +field + : 'int' IDENTIFIER ';' + ; + +fields + : field+ + ; + +structDeclaration + : 'struct' IDENTIFIER '{' fields '}' + ; + +whileStatement + : 'while' '(' expression ')' statement + ; + +breakStatement + : 'break' ';' + ; + +continueStatement + : 'continue' ';' + ; + +ifStatement + : 'if' '(' expression ')' statement ('else' statement)? + ; + +metaStatement + : '#showGraph' ';' + ; + +expressionStatement + : IDENTIFIER '=' expression ';' + | fieldExpression '=' expression ';' + ; + +blockStatement + : '{' statement+ '}' + ; + +structName + : IDENTIFIER + ; + +declStatement + : 'int' IDENTIFIER '=' expression ';' + | structName IDENTIFIER ('?')? '=' expression ';' + ; + +returnStatement + : 'return' expression ';' + ; + +expression + : comparisonExpression + ; + +comparisonExpression + : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-') unaryExpression + | primaryExpression + ; + +newExpression + : 'new' IDENTIFIER + ; + +fieldExpression + : primaryExpresson '.' IDENTIFIER + ; + +primaryExpression + : IDENTIFIER + | INTEGER_LITERAL + | 'true' + | 'false' + | 'null' + | newExpression + | '(' expression ')' + | fieldExpression + ; + +INTEGER_LITERAL + : [1-9][0-9]* + | [0] + ; + +IDENTIFIER + : NON_DIGIT (NON_DIGIT | DIGIT)* + ; + +NON_DIGIT: [a-zA-Z_]; +DEC_DIGIT: [0-9]; +```
\ No newline at end of file |
