diff options
| author | Cliff Click <cliffc@acm.org> | 2024-10-12 14:02:06 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-11-16 15:19:12 -0800 |
| commit | 4b24919a2cd06dd5debcc0322cbeb314d77aaba1 (patch) | |
| tree | 235e9d9c45da62f1113e4a76f8441e82626a5018 /docs/chapter16/16-grammar.md | |
| parent | e9849f620e246d792700e8d2ac2cb0cb2a9df699 (diff) | |
| download | Simple-4b24919a2cd06dd5debcc0322cbeb314d77aaba1.tar.gz Simple-4b24919a2cd06dd5debcc0322cbeb314d77aaba1.zip | |
Chapter 16: Constructors and Final fields
Co-authored-by: Thalia Archibald <thalia@archibald.dev>
Diffstat (limited to 'docs/chapter16/16-grammar.md')
| -rw-r--r-- | docs/chapter16/16-grammar.md | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/docs/chapter16/16-grammar.md b/docs/chapter16/16-grammar.md new file mode 100644 index 00000000..7b611fbf --- /dev/null +++ b/docs/chapter16/16-grammar.md @@ -0,0 +1,132 @@ +# Grammar for Chapter 16 + +```antlrv4 +grammar SimpleLanguage; + +program : block EOF ; + +block : statement+ ; + + +statement + : '{' block '}' + | returnStatement + | ifStatement + | whileStatement + | breakStatement + | continueStatement + | metaStatement + | structDeclaration + | expressionStatement + ; + + +returnStatement : 'return' expression ';' ; + +ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; + +whileStatement : 'while' '(' expression ')' statement ; + +breakStatement : 'break' ';' ; + +continueStatement : 'continue' ';' ; + +metaStatement : '#showGraph' ';' ; + +structDeclaration : 'struct' IDENTIFIER '{' block '}' ; + +expressionStatement + : type IDENTIFIER ';' + | type IDENTIFIER '=' expression ';' + | IDENTIFIER '=' expression ';' + | expression + ; + +PRIMTYPE + : 'int' + | 'i8' + | 'i16' + | 'i32' + | 'i64' + | 'u8' + | 'u16' + | 'u32' + | 'flt' + | 'f32' + | 'f64' + | 'bool' + ; + +type + : PRIMTYPE + | typeName '?'? + | typeName nestedArray+ + ; + +nestedArray : '?'? '[]'* ; + +typeName : IDENTIFIER ; + + +expression : bitWiseExpression ; + +bitWiseExpression + : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* + ; + +shiftExpression + : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-') unaryExpression + | '!' unaryExpression + | primaryExpression postAssign + ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | '(' expression ')' + | 'true' + | 'false' + | 'null' + | newExpression + | IDENTIFIER + ; + +newExpression + : 'new' IDENTIFIER [ '{' block '}' ] + | 'new' IDENTIFIER '[' expression ']' + ; + +postAssign : postFix [ '=' expression ] | [ '#' ]; + +postFix + : '.' IDENTIFIER postFix + | '[' expression ']' postFix + ; + + +IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; + +INTEGER_LITERAL + : [1-9]DIGIT* + | [0] + ; + +NON_DIGIT: [a-zA-Z_]; +DIGIT: [0-9]; +``` |
