Debian Codegraph @

examples/tiny/docs/grammar.md

raw
1 # TINY language grammar (current parser outline)
2
3 This document reflects the current definitions in `examples/tiny/tiny_parser.py`.
4
5 Ref: https://github.com/a7medayman6/Tiny-Compiler/blob/master/Language-Description.md
6
7 Notes
8 - Terminals appear in single quotes.
9 - Whitespace is insignificant except inside quoted strings.
10 - Comments use C-style block comments `/* ... */` and are ignored globally.
11 - Simple statements are terminated with a trailing semicolon `;`. Control-flow
12 statements (`if ... end`, `repeat ... until ...`) do not end with a semicolon.
13
14 Lexical tokens
15 - punctuation: `'(' ')' '{' '}' ',' ';' ':='`
16 - keywords (reserved): `if then else elseif end repeat until read write return endl int float string main`
17 - identifier: a letter followed by letters, digits, or `_`, but not a reserved word
18 - number: integer or floating point
19 - string: double-quoted with `\` as escape, for example: `"hello"`
20
21 Program structure
22 - Program := { FunctionDefinition } MainFunction
23 - MainFunction := Datatype 'main' '(' ')' FunctionBody
24 - FunctionDefinition := FunctionDeclaration FunctionBody
25 - FunctionDeclaration := Datatype FunctionName '(' [ Parameter { ',' Parameter } ] ')'
26 - FunctionBody := '{' StmtSeq '}'
27 - Datatype := 'int' | 'float' | 'string'
28
29 Statements
30 - StmtSeq := one-or-more Statement
31 - Statement :=
32 - DeclarationStatement
33 - AssignmentStatement
34 - ReadStatement
35 - WriteStatement
36 - ReturnStatement
37 - FunctionCallStatement
38 - IfStatement
39 - RepeatStatement
40
41 Simple statements (each ends with `;`)
42 - DeclarationStatement := Datatype VarDecl { ',' VarDecl } ';'
43 - VarDecl := identifier [ ':=' Expr ]
44 - AssignmentStatement := identifier ':=' Expr ';'
45 - ReadStatement := 'read' identifier ';'
46 - WriteStatement := 'write' ( 'endl' | Expr ) ';'
47 - ReturnStatement := 'return' Expr ';'
48 - FunctionCallStatement := FunctionCall ';'
49
50 Control flow (no trailing semicolon)
51 - IfStatement := 'if' Condition 'then' StmtSeq { ElseIfBlock } [ 'else' StmtSeq ] 'end'
52 - ElseIfBlock := 'elseif' Condition 'then' StmtSeq
53 - RepeatStatement := 'repeat' StmtSeq 'until' Condition
54
55 Expressions
56 - Expr := RelExpr
57 - RelExpr := Arith { RelOp Arith }
58 - RelOp := '<' | '>' | '=' | '<>'
59 - Condition := RelExpr { '&&' RelExpr | '||' RelExpr }
60 - Arith :=
61 - Right-assoc prefix `+` on a single operand
62 - Then left-assoc `*` `/`
63 - Then left-assoc `+` `-`
64 (This mirrors the `infix_notation` levels in the parser.)
65 - Term := number | string | FunctionCall | identifier
66 - FunctionCall := FunctionName '(' [ Expr { ',' Expr } ] ')'
67
68 Additional notes
69 - The special literal `endl` may be used only in `write` statements (as modeled by the parser), or a general expression can be written instead.
70 - Comments `/* ... */` may appear between tokens anywhere a statement or expression is allowed; they are ignored by the parser.
71
72 Examples
73 - Declaration and assignment
74 - `int x; float y := 2.5, z; string s := "Hello";`
75 - If/elseif/else (no semicolons after blocks)
76 - `if x < 10 then y := y + 1; write y; elseif x = 0 then write 0; else read x; end`
77 - Repeat/until (no semicolon after `until` line)
78 - `repeat x := x - 1; write x; until x = 0`
79 - Functions and main
80 - `int sum(int a, int b){ write a; return a + b; } int main(){ int r; r := sum(2,3); write r; return 0; }`