File: grammar.md

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (80 lines) | stat: -rw-r--r-- 3,272 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# TINY language grammar (current parser outline)

This document reflects the current definitions in `examples/tiny/tiny_parser.py`.

Ref: https://github.com/a7medayman6/Tiny-Compiler/blob/master/Language-Description.md

Notes
- Terminals appear in single quotes.
- Whitespace is insignificant except inside quoted strings.
- Comments use C-style block comments `/* ... */` and are ignored globally.
- Simple statements are terminated with a trailing semicolon `;`. Control-flow
  statements (`if ... end`, `repeat ... until ...`) do not end with a semicolon.

Lexical tokens
- punctuation: `'(' ')' '{' '}' ',' ';' ':='`
- keywords (reserved): `if then else elseif end repeat until read write return endl int float string main`
- identifier: a letter followed by letters, digits, or `_`, but not a reserved word
- number: integer or floating point
- string: double-quoted with `\` as escape, for example: `"hello"`

Program structure
- Program := { FunctionDefinition } MainFunction
- MainFunction := Datatype 'main' '(' ')' FunctionBody
- FunctionDefinition := FunctionDeclaration FunctionBody
- FunctionDeclaration := Datatype FunctionName '(' [ Parameter { ',' Parameter } ] ')'
- FunctionBody := '{' StmtSeq '}'
- Datatype := 'int' | 'float' | 'string'

Statements
- StmtSeq := one-or-more Statement
- Statement :=
  - DeclarationStatement
  - AssignmentStatement
  - ReadStatement
  - WriteStatement
  - ReturnStatement
  - FunctionCallStatement
  - IfStatement
  - RepeatStatement

Simple statements (each ends with `;`)
- DeclarationStatement := Datatype VarDecl { ',' VarDecl } ';'
  - VarDecl := identifier [ ':=' Expr ]
- AssignmentStatement := identifier ':=' Expr ';'
- ReadStatement := 'read' identifier ';'
- WriteStatement := 'write' ( 'endl' | Expr ) ';'
- ReturnStatement := 'return' Expr ';'
- FunctionCallStatement := FunctionCall ';'

Control flow (no trailing semicolon)
- IfStatement := 'if' Condition 'then' StmtSeq { ElseIfBlock } [ 'else' StmtSeq ] 'end'
  - ElseIfBlock := 'elseif' Condition 'then' StmtSeq
- RepeatStatement := 'repeat' StmtSeq 'until' Condition

Expressions
- Expr := RelExpr
- RelExpr := Arith { RelOp Arith }
- RelOp := '<' | '>' | '=' | '<>'
- Condition := RelExpr { '&&' RelExpr | '||' RelExpr }
- Arith :=
  - Right-assoc prefix `+` on a single operand
  - Then left-assoc `*` `/`
  - Then left-assoc `+` `-`
  (This mirrors the `infix_notation` levels in the parser.)
- Term := number | string | FunctionCall | identifier
- FunctionCall := FunctionName '(' [ Expr { ',' Expr } ] ')'

Additional notes
- The special literal `endl` may be used only in `write` statements (as modeled by the parser), or a general expression can be written instead.
- Comments `/* ... */` may appear between tokens anywhere a statement or expression is allowed; they are ignored by the parser.

Examples
- Declaration and assignment
  - `int x; float y := 2.5, z; string s := "Hello";`
- If/elseif/else (no semicolons after blocks)
  - `if x < 10 then y := y + 1; write y; elseif x = 0 then write 0; else read x; end`
- Repeat/until (no semicolon after `until` line)
  - `repeat x := x - 1; write x; until x = 0`
- Functions and main
  - `int sum(int a, int b){ write a; return a + b; } int main(){ int r; r := sum(2,3); write r; return 0; }`