summaryrefslogtreecommitdiff
path: root/chapter01/docs/01-grammar.md
blob: 92ed7357e0383701c93ee21e12af0a4694b77048 (plain)
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
# Grammar for Chapter 1

```antlrv4
grammar SimpleLanguage;

program
    : declaration* statement+ EOF
    ;

declaration
    : type IDENTIFIER ';'
    ;

type
    : 'int'
    ;

statement
    : 'if' '(' expression ')' statement
    | 'if' '(' expression ')' statement 'else' statement
    | 'while' '(' expression ')' statement
    | IDENTIFIER '=' expression ';'
    | '{' declaration* statement+ '}'
    | 'break' ';'
    ;

expression
    : primaryExpression                                             # PrimaryExpression_
    | expression ('*' | '/' ) expression                            # ArithmeticOrLogicalExpression
    | expression ('+' | '-') expression                             # ArithmeticOrLogicalExpression
    ;

primaryExpression
    :
    | INTEGER_LITERAL
    | IDENTIFIER
    | '(' expression ')'
    ;
```